Skip to content

Commit 9c216b0

Browse files
committed
fix: fix deleteGuildRole util and tests
1 parent e893d8f commit 9c216b0

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/utils/guildRole.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,19 @@ export async function deleteGuildRole(
2828
roleId: string,
2929
reason?: string
3030
) {
31-
return undefined;
31+
const deleteGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/roles/${roleId}`;
32+
const headers: HeadersInit = createDiscordHeaders({
33+
token: env.DISCORD_TOKEN,
34+
reason: reason,
35+
});
36+
try {
37+
return await fetch(deleteGuildRoleUrl, {
38+
method: "DELETE",
39+
headers,
40+
});
41+
} catch (err) {
42+
return INTERNAL_SERVER_ERROR;
43+
}
3244
}
3345

3446
export async function createGuildRole(

tests/unit/utils/guildRole.test.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,60 @@ import {
1717
rolesMock,
1818
} from "../../fixtures/fixture";
1919
import { DiscordMessageResponse } from "../../../src/typeDefinitions/discordMessage.types";
20+
import { DISCORD_BASE_URL } from "../../../src/constants/urls";
2021

2122
describe("deleteGuildRole", () => {
22-
it("should return undefined", async () => {
23-
const response = await deleteGuildRole({}, "100");
24-
expect(response).toEqual(undefined);
23+
const roleId = "1A32BEX04";
24+
const deleteGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${guildEnv.DISCORD_GUILD_ID}/roles/${roleId}`;
25+
const mockRequestInit = {
26+
method: "DELETE",
27+
headers: {
28+
"Content-Type": "application/json",
29+
Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`,
30+
"X-Audit-Log-Reason": "This is reason for this action",
31+
},
32+
};
33+
34+
it("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => {
35+
jest
36+
.spyOn(global, "fetch")
37+
.mockImplementation((inp) => Promise.resolve(new JSONResponse(inp)));
38+
39+
await deleteGuildRole(guildEnv, roleId, "This is reason for this action");
40+
41+
expect(global.fetch).toHaveBeenCalledWith(
42+
deleteGuildRoleUrl,
43+
mockRequestInit
44+
);
45+
});
46+
47+
it("should return an empty response with 204 status", async () => {
48+
const mockResponse = new Response(null, {
49+
status: 204,
50+
});
51+
jest
52+
.spyOn(global, "fetch")
53+
.mockImplementation(() => Promise.resolve(mockResponse));
54+
const response = (await deleteGuildRole(guildEnv, roleId)) as Response;
55+
expect(response).toEqual(mockResponse);
56+
expect(response.status).toEqual(mockResponse.status);
57+
expect(global.fetch).toHaveBeenCalledWith(
58+
deleteGuildRoleUrl,
59+
mockRequestInit
60+
);
61+
});
62+
63+
it("should return INTERNAL_SERVER_ERROR when response is not ok", async () => {
64+
const mockErrorResponse = new JSONResponse(response.INTERNAL_SERVER_ERROR);
65+
jest
66+
.spyOn(global, "fetch")
67+
.mockImplementation(() => Promise.resolve(mockErrorResponse));
68+
const result = await deleteGuildRole(guildEnv, roleId);
69+
expect(result).toEqual(mockErrorResponse);
70+
expect(global.fetch).toHaveBeenCalledWith(
71+
deleteGuildRoleUrl,
72+
mockRequestInit
73+
);
2574
});
2675
});
2776

0 commit comments

Comments
 (0)