|
| 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 | + test("removes users successfully", async () => { |
| 13 | + const usersWithMatchingRole = ["<@userId1>", "<@userId2>"]; |
| 14 | + |
| 15 | + jest |
| 16 | + .spyOn(global, "fetch") |
| 17 | + .mockImplementation(() => |
| 18 | + Promise.resolve(new Response(null, { status: 204 })) |
| 19 | + ); |
| 20 | + await removeUsers(mockEnv, usersWithMatchingRole); |
| 21 | + |
| 22 | + expect(fetch).toHaveBeenCalledTimes(2); |
| 23 | + expect(fetch).toHaveBeenCalledWith( |
| 24 | + `${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/userId1`, |
| 25 | + { |
| 26 | + method: "DELETE", |
| 27 | + headers: { |
| 28 | + "Content-Type": "application/json", |
| 29 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 30 | + }, |
| 31 | + } |
| 32 | + ); |
| 33 | + expect(fetch).toHaveBeenCalledWith( |
| 34 | + `${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/userId2`, |
| 35 | + { |
| 36 | + method: "DELETE", |
| 37 | + headers: { |
| 38 | + "Content-Type": "application/json", |
| 39 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 40 | + }, |
| 41 | + } |
| 42 | + ); |
| 43 | + }); |
| 44 | + test("handles errors", async () => { |
| 45 | + const usersWithMatchingRole = ["<@userId1>"]; |
| 46 | + |
| 47 | + // Mocking the fetch function to simulate a rejected promise with a 404 error response |
| 48 | + jest |
| 49 | + .spyOn(global, "fetch") |
| 50 | + .mockImplementation(() => |
| 51 | + Promise.reject(new Response(null, { status: 404 })) |
| 52 | + ); |
| 53 | + |
| 54 | + // Calling the function under test |
| 55 | + await removeUsers(mockEnv, usersWithMatchingRole); |
| 56 | + |
| 57 | + // Expectations |
| 58 | + expect(fetch).toHaveBeenCalledTimes(3); |
| 59 | + expect(fetch).toHaveBeenCalledWith( |
| 60 | + `${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/userId1`, |
| 61 | + { |
| 62 | + method: "DELETE", |
| 63 | + headers: { |
| 64 | + "Content-Type": "application/json", |
| 65 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 66 | + }, |
| 67 | + } |
| 68 | + ); |
| 69 | + }); |
| 70 | +}); |
0 commit comments