Skip to content

Commit

Permalink
fix: change field and expectation message and test name as it was fai…
Browse files Browse the repository at this point in the history
…ling
  • Loading branch information
pankajjs committed Dec 28, 2024
1 parent a614514 commit 65ff578
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
6 changes: 3 additions & 3 deletions tests/unit/handlers/onboardingExtension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("onboardingExtensionCommand", () => {
jest.restoreAllMocks();
});
const discordId =
transformedArgsForOnboardingExtension.member.user.id.toString();
transformedArgsForOnboardingExtension.memberObj.user.id.toString();

it("should return Feature not implemented", async () => {
const expectedRes = await onboardingExtensionCommand(
Expand All @@ -32,7 +32,7 @@ describe("onboardingExtensionCommand", () => {
});

it("should return initial response", async () => {
transformedArgsForOnboardingExtension.dev.value = true;
transformedArgsForOnboardingExtension.devObj.value = true;
const expectedRes = await onboardingExtensionCommand(
transformedArgsForOnboardingExtension,
guildEnv,
Expand All @@ -48,7 +48,7 @@ describe("onboardingExtensionCommand", () => {

it("should call fetch", async () => {
jest.spyOn(utils, "createOnboardingExtension");
transformedArgsForOnboardingExtension.dev.value = true;
transformedArgsForOnboardingExtension.devObj.value = true;
await onboardingExtensionCommand(
transformedArgsForOnboardingExtension,
guildEnv,
Expand Down
38 changes: 12 additions & 26 deletions tests/unit/utils/onboardingExtension.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import config from "../../../config/config";
import { UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST } from "../../../src/constants/responses";
import { DISCORD_BASE_URL } from "../../../src/constants/urls";
import { generateDiscordAuthToken } from "../../../src/utils/authTokenGenerator";
import {
Expand All @@ -18,7 +19,6 @@ describe("createOnboaringExtension", () => {
};
const discordReplyUrl = `${DISCORD_BASE_URL}/channels/${args.channelId}/messages`;
const base_url = config(env).RDS_BASE_API_URL;
const errorContent = `<@${args.discordId}> Error occurred while creating onboarding extension request.`;
const createOnboardingExtensionUrl = `${base_url}/requests?dev=true`;
let fetchSpy: jest.SpyInstance;
let authToken: string;
Expand All @@ -27,7 +27,6 @@ describe("createOnboaringExtension", () => {
userId: args.userId,
type: "ONBOARDING",
numberOfDays: args.numberOfDays,
requestedBy: args.discordId,
reason: args.reason,
};

Expand All @@ -47,33 +46,21 @@ describe("createOnboaringExtension", () => {
jest.restoreAllMocks();
});

it("should call fetch with error response", async () => {
fetchSpy.mockImplementation(() => {
throw new Error();
});
try {
await createOnboardingExtension(args, guildEnv);
} catch (err) {
expect(global.fetch).toHaveBeenCalledTimes(2);
expect(global.fetch).toHaveBeenCalledWith(createOnboardingExtensionUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
},
body: JSON.stringify(requestData),
});
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledTimes(1);
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledWith(
discordReplyUrl,
errorContent,
guildEnv
);
}
it("should call sendReplyInDiscordChannel with error content", async () => {
await createOnboardingExtension(args, guildEnv);
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledTimes(1);
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledWith(
discordReplyUrl,
`<@${args.discordId}> ${UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST}`,
guildEnv
);
});

it("should call fetch with success response", async () => {
args.userId = undefined;
requestData.userId = args.discordId;
await createOnboardingExtension(args, guildEnv);

expect(global.fetch).toHaveBeenCalledTimes(2);
expect(global.fetch).toHaveBeenCalledWith(createOnboardingExtensionUrl, {
method: "POST",
Expand All @@ -83,6 +70,5 @@ describe("createOnboaringExtension", () => {
},
body: JSON.stringify(requestData),
});
expect(utils.sendReplyInDiscordChannel).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 65ff578

Please sign in to comment.