Skip to content

Commit 26d4a58

Browse files
committed
add test for group invite
1 parent faea616 commit 26d4a58

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

src/controllers/groupInvite.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import config from "../../config/config";
22
import { env } from "../typeDefinitions/default.types";
33
import { discordTextResponse } from "../utils/discordResponse";
4-
import { fetchDiscordGroups } from "../utils/fetchDiscordGroups";
4+
import * as DiscordGroups from "../utils/fetchDiscordGroups";
5+
import JSONResponse from "../utils/JsonResponse";
56

6-
export async function groupInvite(userId: string, roleId: string, env: env) {
7-
const response = await fetchDiscordGroups(env);
7+
export async function groupInvite(
8+
userId: string,
9+
roleId: string,
10+
env: env
11+
): Promise<JSONResponse> {
12+
const response = await DiscordGroups.fetchDiscordGroups(env);
813
const group = response.groups.find((group) => group.roleid === roleId);
914

1015
if (!group) {

src/typeDefinitions/group.types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ export type GroupType = {
1313
};
1414

1515
export type GroupResponseType = {
16-
message?: string;
16+
message: string;
1717
groups: GroupType[];
1818
};

tests/fixtures/groups.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { GroupResponseType } from "../../src/typeDefinitions/group.types";
2+
3+
export const groups: GroupResponseType = {
4+
message: "Roles fetched successfully!",
5+
groups: [
6+
{
7+
id: "27EdauP9UmxCTgAMDPpl",
8+
date: {
9+
_seconds: 1719334613,
10+
_nanoseconds: 934000000,
11+
},
12+
createdBy: "zXQpimWaGWOFF2sLyrFt",
13+
rolename: "group-testing 3",
14+
roleid: "1255205109340573782",
15+
description: "for testing",
16+
memberCount: 0,
17+
isMember: false,
18+
},
19+
{
20+
id: "ELjCeNZxhHupn8qU5pWI",
21+
date: {
22+
_seconds: 1718771669,
23+
_nanoseconds: 27000000,
24+
},
25+
createdBy: "zXQpimWaGWOFF2sLyrFt",
26+
rolename: "group-testing",
27+
roleid: "1252843931306164298",
28+
description: "for testing",
29+
memberCount: 0,
30+
isMember: false,
31+
},
32+
{
33+
id: "tO4vZe5CC690yOb9Txlh",
34+
date: {
35+
_seconds: 1718771969,
36+
_nanoseconds: 680000000,
37+
},
38+
createdBy: "zXQpimWaGWOFF2sLyrFt",
39+
rolename: "group-testinge",
40+
roleid: "1252845191472087050",
41+
description: "",
42+
memberCount: 0,
43+
isMember: false,
44+
},
45+
],
46+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { environment } from "../../fixtures/config";
2+
import * as DiscordGroups from "../../../src/utils/fetchDiscordGroups";
3+
import JSONResponse from "../../../src/utils/JsonResponse";
4+
import { groupInvite } from "../../../src/controllers/groupInvite";
5+
import { groups } from "../../fixtures/groups";
6+
import { discordTextResponse } from "../../../src/utils/discordResponse";
7+
8+
describe("Test /group-invite command", () => {
9+
beforeEach(() => {
10+
jest
11+
.spyOn(DiscordGroups, "fetchDiscordGroups")
12+
.mockImplementation(() => Promise.resolve(groups));
13+
});
14+
15+
afterEach(() => {
16+
jest.resetAllMocks();
17+
jest.restoreAllMocks();
18+
});
19+
20+
it("Should be an instance of JSONResponse", async () => {
21+
const response = await groupInvite(
22+
"1",
23+
groups.groups[0].roleid,
24+
environment[0]
25+
);
26+
27+
expect(response).toBeInstanceOf(JSONResponse);
28+
});
29+
30+
it("Should return a discordTextResponse if group is not found", async () => {
31+
const invalidRoleId = "invalidRoleId";
32+
const expectedResponse = discordTextResponse(
33+
`<@&${invalidRoleId}> is not a valid group.`
34+
);
35+
36+
const response = await groupInvite("1", invalidRoleId, environment[0]);
37+
38+
expect(await response.json()).toEqual(await expectedResponse.json());
39+
});
40+
});

0 commit comments

Comments
 (0)