|
| 1 | +import { DISCORD_BASE_URL } from "../../../src/constants/urls"; |
| 2 | +import JSONResponse from "../../../src/utils/JsonResponse"; |
| 3 | +import { removeUsers } from "../../../src/utils/removeUsers"; |
| 4 | + |
| 5 | +describe("removeUsers", () => { |
| 6 | + const mockEnv = { |
| 7 | + BOT_PUBLIC_KEY: "xyz", |
| 8 | + DISCORD_GUILD_ID: "123", |
| 9 | + DISCORD_TOKEN: "abc", |
| 10 | + }; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + }); |
| 15 | + afterEach(() => { |
| 16 | + jest.clearAllMocks(); |
| 17 | + jest.resetAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + test("removes users successfully", async () => { |
| 21 | + const usersWithMatchingRole = ["<@userId1>", "<@userId2>"]; |
| 22 | + |
| 23 | + jest |
| 24 | + .spyOn(global, "fetch") |
| 25 | + .mockImplementation(() => |
| 26 | + Promise.resolve(new Response(null, { status: 204 })) |
| 27 | + ); |
| 28 | + await removeUsers(mockEnv, usersWithMatchingRole, 21121); |
| 29 | + |
| 30 | + expect(fetch).toHaveBeenCalledTimes(2); |
| 31 | + expect(fetch).toHaveBeenCalledWith( |
| 32 | + `${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/userId1`, |
| 33 | + { |
| 34 | + method: "DELETE", |
| 35 | + headers: { |
| 36 | + "Content-Type": "application/json", |
| 37 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 38 | + }, |
| 39 | + } |
| 40 | + ); |
| 41 | + expect(fetch).toHaveBeenCalledWith( |
| 42 | + `${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/userId2`, |
| 43 | + { |
| 44 | + method: "DELETE", |
| 45 | + headers: { |
| 46 | + "Content-Type": "application/json", |
| 47 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 48 | + }, |
| 49 | + } |
| 50 | + ); |
| 51 | + }); |
| 52 | + test("handles errors", async () => { |
| 53 | + const usersWithMatchingRole = ["<@userId1>"]; |
| 54 | + |
| 55 | + // Mocking the fetch function to simulate a rejected promise with a 404 error response |
| 56 | + jest |
| 57 | + .spyOn(global, "fetch") |
| 58 | + .mockImplementation(() => |
| 59 | + Promise.reject(new Response(null, { status: 404 })) |
| 60 | + ); |
| 61 | + |
| 62 | + // Calling the function under test |
| 63 | + await removeUsers(mockEnv, usersWithMatchingRole, 23231); |
| 64 | + |
| 65 | + // Expectations |
| 66 | + expect(fetch).toHaveBeenCalledWith( |
| 67 | + `${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/userId1`, |
| 68 | + { |
| 69 | + method: "DELETE", |
| 70 | + headers: { |
| 71 | + "Content-Type": "application/json", |
| 72 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 73 | + }, |
| 74 | + } |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should send a message of failed api calls at the end", async () => { |
| 79 | + let fetchCallCount = 0; |
| 80 | + const usersWithMatchingRole = ["<@userId1>", "<@userId2>", "<@userId3>"]; |
| 81 | + |
| 82 | + jest.spyOn(global, "fetch").mockImplementation(async () => { |
| 83 | + if (fetchCallCount < 3) { |
| 84 | + fetchCallCount++; |
| 85 | + return Promise.resolve(new JSONResponse({ message: "404: Not Found" })); |
| 86 | + } else { |
| 87 | + return Promise.resolve(new JSONResponse({ ok: true })); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + await removeUsers(mockEnv, usersWithMatchingRole, 23231); |
| 92 | + expect(fetch).toHaveBeenCalledTimes(4); // should send a message of failed api calls at the end |
| 93 | + }); |
| 94 | +}); |
0 commit comments