@@ -17,11 +17,60 @@ import {
1717 rolesMock ,
1818} from "../../fixtures/fixture" ;
1919import { DiscordMessageResponse } from "../../../src/typeDefinitions/discordMessage.types" ;
20+ import { DISCORD_BASE_URL } from "../../../src/constants/urls" ;
2021
2122describe ( "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