Skip to content

Commit 4fd48bf

Browse files
committed
refactor: refactor deleteGuildRole util and tests in separate file
1 parent fc3fd55 commit 4fd48bf

File tree

4 files changed

+85
-78
lines changed

4 files changed

+85
-78
lines changed

src/utils/deleteGuildRole.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { INTERNAL_SERVER_ERROR, ROLE_REMOVED } from "../constants/responses";
2+
import { DISCORD_BASE_URL } from "../constants/urls";
3+
import { env } from "../typeDefinitions/default.types";
4+
import createDiscordHeaders from "./createDiscordHeaders";
5+
6+
export async function deleteGuildRole(
7+
env: env,
8+
roleId: string,
9+
reason?: string
10+
) {
11+
const deleteGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/roles/${roleId}`;
12+
const headers: HeadersInit = createDiscordHeaders({
13+
token: env.DISCORD_TOKEN,
14+
reason: reason,
15+
});
16+
try {
17+
const response = await fetch(deleteGuildRoleUrl, {
18+
method: "DELETE",
19+
headers,
20+
});
21+
if (response.ok) {
22+
return ROLE_REMOVED;
23+
} else {
24+
return INTERNAL_SERVER_ERROR;
25+
}
26+
} catch (err) {
27+
return INTERNAL_SERVER_ERROR;
28+
}
29+
}

src/utils/guildRole.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,6 @@ import { GuildRole, Role } from "../typeDefinitions/role.types";
2323
import createDiscordHeaders from "./createDiscordHeaders";
2424
import { sleep } from "./sleep";
2525

26-
export async function deleteGuildRole(
27-
env: env,
28-
roleId: string,
29-
reason?: string
30-
) {
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-
const response = await fetch(deleteGuildRoleUrl, {
38-
method: "DELETE",
39-
headers,
40-
});
41-
if (response.ok) {
42-
return ROLE_REMOVED;
43-
} else {
44-
return INTERNAL_SERVER_ERROR;
45-
}
46-
} catch (err) {
47-
return INTERNAL_SERVER_ERROR;
48-
}
49-
}
50-
5126
export async function createGuildRole(
5227
body: createNewRole,
5328
env: env,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { DISCORD_BASE_URL } from "../../../src/constants/urls";
2+
import { deleteGuildRole } from "../../../src/utils/deleteGuildRole";
3+
import JSONResponse from "../../../src/utils/JsonResponse";
4+
import { guildEnv } from "../../fixtures/fixture";
5+
import * as response from "../../../src/constants/responses";
6+
7+
describe("deleteGuildRole", () => {
8+
const roleId = "1A32BEX04";
9+
const deleteGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${guildEnv.DISCORD_GUILD_ID}/roles/${roleId}`;
10+
const mockRequestInit = {
11+
method: "DELETE",
12+
headers: {
13+
"Content-Type": "application/json",
14+
Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`,
15+
"X-Audit-Log-Reason": "This is reason for this action",
16+
},
17+
};
18+
19+
beforeEach(() => {
20+
jest.clearAllMocks();
21+
});
22+
afterEach(() => {
23+
jest.restoreAllMocks();
24+
});
25+
26+
it("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => {
27+
jest
28+
.spyOn(global, "fetch")
29+
.mockImplementation((inp) => Promise.resolve(new JSONResponse(inp)));
30+
31+
await deleteGuildRole(guildEnv, roleId, "This is reason for this action");
32+
33+
expect(global.fetch).toHaveBeenCalledWith(
34+
deleteGuildRoleUrl,
35+
mockRequestInit
36+
);
37+
});
38+
39+
it("should return ROLE_REMOVED when response is ok", async () => {
40+
const mockResponse = new Response(null, {
41+
status: 204,
42+
});
43+
jest.spyOn(global, "fetch").mockResolvedValue(mockResponse);
44+
const result = await deleteGuildRole(guildEnv, roleId);
45+
expect(result).toEqual(response.ROLE_REMOVED);
46+
expect(global.fetch).toHaveBeenCalledTimes(1);
47+
});
48+
49+
it("should return INTERNAL_SERVER_ERROR when response is not ok", async () => {
50+
const mockErrorResponse = new Response(response.INTERNAL_SERVER_ERROR);
51+
jest.spyOn(global, "fetch").mockRejectedValue(mockErrorResponse);
52+
const result = await deleteGuildRole(guildEnv, roleId);
53+
expect(result).toEqual(response.INTERNAL_SERVER_ERROR);
54+
expect(global.fetch).toHaveBeenCalledTimes(1);
55+
});
56+
});

tests/unit/utils/guildRole.test.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
getGuildRoles,
88
getGuildRoleByName,
99
mentionEachUserInMessage,
10-
deleteGuildRole,
1110
} from "../../../src/utils/guildRole";
1211
import {
1312
dummyAddRoleBody,
@@ -17,58 +16,6 @@ import {
1716
rolesMock,
1817
} from "../../fixtures/fixture";
1918
import { DiscordMessageResponse } from "../../../src/typeDefinitions/discordMessage.types";
20-
import { DISCORD_BASE_URL } from "../../../src/constants/urls";
21-
22-
describe("deleteGuildRole", () => {
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-
beforeEach(() => {
35-
jest.clearAllMocks();
36-
});
37-
afterEach(() => {
38-
jest.restoreAllMocks();
39-
});
40-
41-
it("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => {
42-
jest
43-
.spyOn(global, "fetch")
44-
.mockImplementation((inp) => Promise.resolve(new JSONResponse(inp)));
45-
46-
await deleteGuildRole(guildEnv, roleId, "This is reason for this action");
47-
48-
expect(global.fetch).toHaveBeenCalledWith(
49-
deleteGuildRoleUrl,
50-
mockRequestInit
51-
);
52-
});
53-
54-
it("should return ROLE_REMOVED when response is ok", async () => {
55-
const mockResponse = new Response(null, {
56-
status: 204,
57-
});
58-
jest.spyOn(global, "fetch").mockResolvedValue(mockResponse);
59-
const result = await deleteGuildRole(guildEnv, roleId);
60-
expect(result).toEqual(response.ROLE_REMOVED);
61-
expect(global.fetch).toHaveBeenCalledTimes(1);
62-
});
63-
64-
it("should return INTERNAL_SERVER_ERROR when response is not ok", async () => {
65-
const mockErrorResponse = new Response(response.INTERNAL_SERVER_ERROR);
66-
jest.spyOn(global, "fetch").mockRejectedValue(mockErrorResponse);
67-
const result = await deleteGuildRole(guildEnv, roleId);
68-
expect(result).toEqual(response.INTERNAL_SERVER_ERROR);
69-
expect(global.fetch).toHaveBeenCalledTimes(1);
70-
});
71-
});
7219

7320
describe("createGuildRole", () => {
7421
it("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => {

0 commit comments

Comments
 (0)