|
| 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 | +}); |
0 commit comments