Skip to content

Commit 15e41cc

Browse files
committed
feat: added tests to support onboarding-extension-command
1 parent 0aef740 commit 15e41cc

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

tests/fixtures/fixture.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,21 @@ export const testDataWithDevTitle = {
383383
value: true,
384384
},
385385
};
386+
387+
export const transformedArgsForOnboardingExtension = {
388+
member: {
389+
user: {
390+
id: 134672111,
391+
username: "username",
392+
discriminator: "<discriminator>",
393+
avatar: "<avatar>",
394+
},
395+
nick: "<nick>",
396+
joined_at: "<joined_at>",
397+
},
398+
userId: { name: "userId", type: 6, value: "1545562672", options: [] },
399+
numberOfDays: { value: "20", name: "numberOfDays", type: 3, options: [] },
400+
reason: { value: "reason", name: "reason", type: 3, options: [] },
401+
channelId: 6754321,
402+
dev: { value: false, name: "dev", type: 5 },
403+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { onboardingExtensionCommand } from "../../../src/controllers/onboardingExtensionCommand";
2+
import { discordTextResponse } from "../../../src/utils/discordResponse";
3+
import {
4+
ctx,
5+
guildEnv,
6+
transformedArgsForOnboardingExtension,
7+
} from "../../fixtures/fixture";
8+
import * as utils from "../../../src/utils/onboardingExtension";
9+
10+
describe("onboardingExtensionCommand", () => {
11+
beforeEach(() => {
12+
jest.clearAllMocks();
13+
});
14+
afterEach(() => {
15+
jest.restoreAllMocks();
16+
});
17+
const discordId =
18+
transformedArgsForOnboardingExtension.member.user.id.toString();
19+
20+
it("should return Feature not implemented", async () => {
21+
const expectedRes = await onboardingExtensionCommand(
22+
transformedArgsForOnboardingExtension,
23+
guildEnv,
24+
ctx
25+
);
26+
const jsonResponse = await expectedRes.json();
27+
const mockResponse = discordTextResponse(
28+
`<@${discordId}> Feature not implemented`
29+
);
30+
const mockJsonResponse = await mockResponse.json();
31+
expect(jsonResponse).toStrictEqual(mockJsonResponse);
32+
});
33+
34+
it("should return initial response", async () => {
35+
transformedArgsForOnboardingExtension.dev.value = true;
36+
const expectedRes = await onboardingExtensionCommand(
37+
transformedArgsForOnboardingExtension,
38+
guildEnv,
39+
ctx
40+
);
41+
const jsonResponse = await expectedRes.json();
42+
const mockResponse = discordTextResponse(
43+
`<@${discordId}> Processing your request for onboarding extension`
44+
);
45+
const mockJsonResponse = await mockResponse.json();
46+
expect(jsonResponse).toStrictEqual(mockJsonResponse);
47+
});
48+
49+
it("should call fetch", async () => {
50+
jest.spyOn(utils, "createOnboardingExtension");
51+
transformedArgsForOnboardingExtension.dev.value = true;
52+
await onboardingExtensionCommand(
53+
transformedArgsForOnboardingExtension,
54+
guildEnv,
55+
ctx
56+
);
57+
expect(utils.createOnboardingExtension).toHaveBeenCalledTimes(1);
58+
});
59+
});
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import config from "../../../config/config";
2+
import { DISCORD_BASE_URL } from "../../../src/constants/urls";
3+
import { generateDiscordAuthToken } from "../../../src/utils/authTokenGenerator";
4+
import {
5+
createOnboardingExtension,
6+
CreateOnboardingExtensionArgs,
7+
} from "../../../src/utils/onboardingExtension";
8+
import * as utils from "../../../src/utils/sendReplyInDiscordChannel";
9+
import { env, guildEnv } from "../../fixtures/fixture";
10+
11+
describe("createOnboaringExtension", () => {
12+
const args: CreateOnboardingExtensionArgs = {
13+
channelId: 123456789,
14+
discordId: "4574263",
15+
numberOfDays: 2,
16+
reason: "This is reason",
17+
userId: "1462465462546",
18+
};
19+
const discordReplyUrl = `${DISCORD_BASE_URL}/channels/${args.channelId}/messages`;
20+
const base_url = config(env).RDS_BASE_API_URL;
21+
const errorContent = `<@${args.discordId}> Error occurred while creating onboarding extension request.`;
22+
const createOnboardingExtensionUrl = `${base_url}/requests?dev=true`;
23+
let fetchSpy: jest.SpyInstance;
24+
let authToken: string;
25+
26+
const requestData = {
27+
userId: args.userId,
28+
type: "ONBOARDING",
29+
numberOfDays: args.numberOfDays,
30+
requestedBy: args.discordId,
31+
reason: args.reason,
32+
};
33+
34+
beforeEach(async () => {
35+
jest.clearAllMocks();
36+
fetchSpy = jest.spyOn(global, "fetch");
37+
jest.spyOn(utils, "sendReplyInDiscordChannel");
38+
authToken = await generateDiscordAuthToken(
39+
"Cloudflare Worker",
40+
Math.floor(Date.now() / 1000) + 2,
41+
guildEnv.DISCORD_TOKEN,
42+
"RS256"
43+
);
44+
});
45+
46+
afterEach(() => {
47+
jest.restoreAllMocks();
48+
});
49+
50+
it("should call fetch with error response", async () => {
51+
fetchSpy.mockImplementation(() => {
52+
throw new Error();
53+
});
54+
try {
55+
await createOnboardingExtension(args, guildEnv);
56+
} catch (err) {
57+
expect(global.fetch).toHaveBeenCalledTimes(2);
58+
expect(global.fetch).toHaveBeenCalledWith(createOnboardingExtensionUrl, {
59+
method: "POST",
60+
headers: {
61+
"Content-Type": "application/json",
62+
Authorization: `Bearer ${authToken}`,
63+
},
64+
body: JSON.stringify(requestData),
65+
});
66+
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledTimes(1);
67+
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledWith(
68+
discordReplyUrl,
69+
errorContent,
70+
guildEnv
71+
);
72+
}
73+
});
74+
75+
it("should call fetch with success response", async () => {
76+
await createOnboardingExtension(args, guildEnv);
77+
expect(global.fetch).toHaveBeenCalledTimes(2);
78+
expect(global.fetch).toHaveBeenCalledWith(createOnboardingExtensionUrl, {
79+
method: "POST",
80+
headers: {
81+
"Content-Type": "application/json",
82+
Authorization: `Bearer ${authToken}`,
83+
},
84+
body: JSON.stringify(requestData),
85+
});
86+
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledTimes(1);
87+
});
88+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { sendReplyInDiscordChannel } from "../../../src/utils/sendReplyInDiscordChannel";
2+
3+
describe("sendReplyInDiscordChannel", () => {
4+
const discordReplyUrl = "<disocrd-url>";
5+
const content = "<dicord-content>;";
6+
const mockEnv = { DISCORD_TOKEN: "mockToken" };
7+
8+
beforeEach(() => {
9+
jest.restoreAllMocks();
10+
jest.clearAllMocks();
11+
});
12+
13+
it("should call fetch", async () => {
14+
jest.spyOn(global, "fetch").mockResolvedValue(new Response(content));
15+
16+
await sendReplyInDiscordChannel(discordReplyUrl, content, mockEnv);
17+
18+
expect(global.fetch).toHaveBeenCalledWith(discordReplyUrl, {
19+
method: "POST",
20+
headers: {
21+
"Content-Type": "application/json",
22+
Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`,
23+
},
24+
body: JSON.stringify({
25+
content: content,
26+
}),
27+
});
28+
});
29+
});

0 commit comments

Comments
 (0)