Skip to content

Commit fc3fd55

Browse files
committed
fix: clear mocks before each test and refactor tests
1 parent 1075e40 commit fc3fd55

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

tests/unit/utils/guildRole.test.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ describe("deleteGuildRole", () => {
3131
},
3232
};
3333

34+
beforeEach(() => {
35+
jest.clearAllMocks();
36+
});
37+
afterEach(() => {
38+
jest.restoreAllMocks();
39+
});
40+
3441
it("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => {
3542
jest
3643
.spyOn(global, "fetch")
@@ -44,33 +51,22 @@ describe("deleteGuildRole", () => {
4451
);
4552
});
4653

47-
it("should return an empty response with 204 status", async () => {
54+
it("should return ROLE_REMOVED when response is ok", async () => {
4855
const mockResponse = new Response(null, {
4956
status: 204,
5057
});
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-
);
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);
6162
});
6263

6364
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));
65+
const mockErrorResponse = new Response(response.INTERNAL_SERVER_ERROR);
66+
jest.spyOn(global, "fetch").mockRejectedValue(mockErrorResponse);
6867
const result = await deleteGuildRole(guildEnv, roleId);
69-
expect(result).toEqual(mockErrorResponse);
70-
expect(global.fetch).toHaveBeenCalledWith(
71-
deleteGuildRoleUrl,
72-
mockRequestInit
73-
);
68+
expect(result).toEqual(response.INTERNAL_SERVER_ERROR);
69+
expect(global.fetch).toHaveBeenCalledTimes(1);
7470
});
7571
});
7672

0 commit comments

Comments
 (0)