|
| 1 | +import { DISCORD_BASE_URL } from "../../../src/constants/urls"; |
| 2 | +import { getGuildMemberDetails } from "../../../src/utils/getGuildMemberDetails"; |
| 3 | +import { dummyGuildMemberDetails } from "../../fixtures/fixture"; |
| 4 | + |
| 5 | +describe("getGuildMemberDetails", () => { |
| 6 | + const mockEnv = { |
| 7 | + BOT_PUBLIC_KEY: "xyz", |
| 8 | + DISCORD_GUILD_ID: "123", |
| 9 | + DISCORD_TOKEN: "abc", |
| 10 | + }; |
| 11 | + const mockUserId = "12345678"; |
| 12 | + const GET_GUILD_MEMBER_URL = `${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/${mockUserId}`; |
| 13 | + beforeEach(() => { |
| 14 | + jest.resetAllMocks(); |
| 15 | + }); |
| 16 | + |
| 17 | + test("returns member details on success", async () => { |
| 18 | + jest.spyOn(global, "fetch").mockResolvedValueOnce({ |
| 19 | + ok: true, |
| 20 | + json: jest.fn().mockResolvedValueOnce(dummyGuildMemberDetails), |
| 21 | + } as unknown as Response); |
| 22 | + |
| 23 | + const result = await getGuildMemberDetails(mockUserId, mockEnv); |
| 24 | + |
| 25 | + expect(global.fetch).toHaveBeenCalledWith(GET_GUILD_MEMBER_URL, { |
| 26 | + method: "GET", |
| 27 | + headers: { |
| 28 | + "Content-Type": "application/json", |
| 29 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + expect(result).toEqual(dummyGuildMemberDetails); |
| 34 | + }); |
| 35 | + |
| 36 | + test("returns INTERNAL_SERVER_ERROR on non-ok response", async () => { |
| 37 | + jest.spyOn(global, "fetch").mockResolvedValueOnce({ |
| 38 | + ok: false, |
| 39 | + } as unknown as Response); |
| 40 | + |
| 41 | + const result = await getGuildMemberDetails(mockUserId, mockEnv); |
| 42 | + |
| 43 | + expect(global.fetch).toHaveBeenCalledWith(GET_GUILD_MEMBER_URL, { |
| 44 | + method: "GET", |
| 45 | + headers: { |
| 46 | + "Content-Type": "application/json", |
| 47 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + expect(result).toEqual( |
| 52 | + "Oops! We have encountered an internal Server Error" |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + test("returns INTERNAL_SERVER_ERROR on fetch error", async () => { |
| 57 | + jest |
| 58 | + .spyOn(global, "fetch") |
| 59 | + .mockRejectedValueOnce(new Error("Network error")); |
| 60 | + |
| 61 | + const result = await getGuildMemberDetails(mockUserId, mockEnv); |
| 62 | + |
| 63 | + expect(global.fetch).toHaveBeenCalledWith(GET_GUILD_MEMBER_URL, { |
| 64 | + method: "GET", |
| 65 | + headers: { |
| 66 | + "Content-Type": "application/json", |
| 67 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + expect(result).toEqual( |
| 72 | + "Oops! We have encountered an internal Server Error" |
| 73 | + ); |
| 74 | + }); |
| 75 | +}); |
0 commit comments