Skip to content

Commit 4a3bdc8

Browse files
committed
fix: add tests to handle edge cases and fix existing failing tests
1 parent 6ecae54 commit 4a3bdc8

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/utils/onboardingExtension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const createOnboardingExtension = async (
3232

3333
if (args.userId && args.discordId !== args.userId) {
3434
const userResponse = await getUserDetails(args.discordId);
35-
if (!userResponse.user?.roles?.super_user) {
35+
if (!userResponse?.user?.roles?.super_user) {
3636
content = `<@${args.discordId}> ${UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST}`;
3737
return await sendReplyInDiscordChannel(discordReplyUrl, content, env);
3838
}

tests/unit/utils/onboardingExtension.test.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import config from "../../../config/config";
22
import { UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST } from "../../../src/constants/responses";
33
import { DISCORD_BASE_URL } from "../../../src/constants/urls";
44
import { generateDiscordAuthToken } from "../../../src/utils/authTokenGenerator";
5+
import JSONResponse from "../../../src/utils/JsonResponse";
56
import {
67
createOnboardingExtension,
78
CreateOnboardingExtensionArgs,
@@ -46,8 +47,10 @@ describe("createOnboaringExtension", () => {
4647
jest.restoreAllMocks();
4748
});
4849

49-
it("should call sendReplyInDiscordChannel with error content", async () => {
50+
it("should call sendReplyInDicordChannel when user is not a super user", async () => {
51+
fetchSpy.mockImplementation(() => new JSONResponse(null));
5052
await createOnboardingExtension(args, guildEnv);
53+
expect(fetchSpy).toHaveBeenCalledTimes(2);
5154
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledTimes(1);
5255
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledWith(
5356
discordReplyUrl,
@@ -56,7 +59,46 @@ describe("createOnboaringExtension", () => {
5659
);
5760
});
5861

59-
it("should call fetch with success response", async () => {
62+
it("should call sendReplyInDiscordChannel with error response for invalid request body", async () => {
63+
const message = "numberOfDays must be positive";
64+
const errorContent = `<@${args.discordId}> ${message}`;
65+
const response = new JSONResponse({ message });
66+
67+
fetchSpy.mockImplementation(() => response);
68+
69+
args.numberOfDays = -1;
70+
args.userId = undefined;
71+
requestData.numberOfDays = args.numberOfDays;
72+
73+
await createOnboardingExtension(args, guildEnv);
74+
expect(fetchSpy).toHaveBeenCalledTimes(2);
75+
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledTimes(1);
76+
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledWith(
77+
discordReplyUrl,
78+
errorContent,
79+
guildEnv
80+
);
81+
});
82+
83+
it("should call sendReplyInDiscordChannel with error content for unexpected error", async () => {
84+
fetchSpy.mockImplementation(() => {
85+
throw new Error("Unexpected Error");
86+
});
87+
try {
88+
await createOnboardingExtension(args, guildEnv);
89+
} catch (error) {
90+
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledTimes(1);
91+
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledWith(
92+
discordReplyUrl,
93+
`<@${
94+
args.discordId
95+
}> ${"Error occurred while creating onboarding extension request."}`,
96+
guildEnv
97+
);
98+
}
99+
});
100+
101+
it("should call sendReplyInDiscordChannel with success response", async () => {
60102
args.userId = undefined;
61103
requestData.userId = args.discordId;
62104
await createOnboardingExtension(args, guildEnv);

0 commit comments

Comments
 (0)