|
1 | 1 | import JSONResponse from "../../../src/utils/JsonResponse";
|
2 | 2 | import * as response from "../../../src/constants/responses";
|
3 |
| -import { createGuildRole, addGroupRole } from "../../../src/utils/guildRole"; |
| 3 | +import { |
| 4 | + createGuildRole, |
| 5 | + addGroupRole, |
| 6 | + removeGuildRole, |
| 7 | +} from "../../../src/utils/guildRole"; |
4 | 8 | import {
|
5 | 9 | dummyAddRoleBody,
|
6 | 10 | dummyCreateBody,
|
@@ -89,3 +93,39 @@ describe("addGroupRole", () => {
|
89 | 93 | );
|
90 | 94 | });
|
91 | 95 | });
|
| 96 | + |
| 97 | +describe("removeGuildRole", () => { |
| 98 | + test("Should return success message on proper response", async () => { |
| 99 | + const mockResponse = { |
| 100 | + ok: true, |
| 101 | + }; |
| 102 | + jest |
| 103 | + .spyOn(global, "fetch") |
| 104 | + .mockImplementation(() => |
| 105 | + Promise.resolve(new JSONResponse(mockResponse)) |
| 106 | + ); |
| 107 | + const result = await removeGuildRole(dummyAddRoleBody, guildEnv); |
| 108 | + expect(global.fetch).toHaveBeenCalledWith( |
| 109 | + `https://discord.com/api/v10/guilds/${guildEnv.DISCORD_GUILD_ID}/members/${dummyAddRoleBody.userid}/roles/${dummyAddRoleBody.roleid}`, |
| 110 | + { |
| 111 | + method: "DELETE", |
| 112 | + headers: { |
| 113 | + "Content-Type": "application/json", |
| 114 | + Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, |
| 115 | + }, |
| 116 | + } |
| 117 | + ); |
| 118 | + expect(result).toEqual({ |
| 119 | + message: response.ROLE_REMOVED, |
| 120 | + userAffected: { |
| 121 | + userid: dummyAddRoleBody.userid, |
| 122 | + roleid: dummyAddRoleBody.roleid, |
| 123 | + }, |
| 124 | + }); |
| 125 | + }); |
| 126 | + test("Should return internal error response on api failure", async () => { |
| 127 | + jest.spyOn(global, "fetch").mockRejectedValue("Oops some error"); |
| 128 | + const result = await removeGuildRole(dummyAddRoleBody, guildEnv); |
| 129 | + expect(result).toEqual(response.INTERNAL_SERVER_ERROR); |
| 130 | + }); |
| 131 | +}); |
0 commit comments