1
- import { createNewRole } from "../../src/typeDefinitions/discordMessage.types" ;
2
1
import JSONResponse from "../../src/utils/JsonResponse" ;
3
2
import * as response from "../../src/constants/responses" ;
4
3
import { createGuildRole , addGroupRole } from "../../src/utils/guildRole" ;
4
+ import {
5
+ dummyAddRoleBody ,
6
+ dummyCreateBody ,
7
+ guildEnv ,
8
+ } from "../fixtures/fixture" ;
5
9
6
10
describe ( "createGuildRole" , ( ) => {
7
11
test ( "should return INTERNAL_SERVER_ERROR when response is not ok" , async ( ) => {
8
- const mockEnv = {
9
- DISCORD_GUILD_ID : "1234" ,
10
- DISCORD_TOKEN : "abcd" ,
11
- } ;
12
- const mockBody : createNewRole = {
13
- rolename : "test role" ,
14
- mentionable : true ,
15
- } ;
16
12
const mockResponse = response . INTERNAL_SERVER_ERROR ;
17
13
jest
18
14
. spyOn ( global , "fetch" )
19
15
. mockImplementation ( ( ) =>
20
16
Promise . resolve ( new JSONResponse ( mockResponse ) )
21
17
) ;
22
18
23
- const result = await createGuildRole ( mockBody , mockEnv ) ;
19
+ const result = await createGuildRole ( dummyCreateBody , guildEnv ) ;
24
20
25
21
expect ( result ) . toEqual ( response . INTERNAL_SERVER_ERROR ) ;
26
22
expect ( global . fetch ) . toHaveBeenCalledWith (
27
- `https://discord.com/api/v10/guilds/${ mockEnv . DISCORD_GUILD_ID } /roles` ,
23
+ `https://discord.com/api/v10/guilds/${ guildEnv . DISCORD_GUILD_ID } /roles` ,
28
24
{
29
25
method : "POST" ,
30
26
headers : {
31
27
"Content-Type" : "application/json" ,
32
- Authorization : `Bot ${ mockEnv . DISCORD_TOKEN } ` ,
28
+ Authorization : `Bot ${ guildEnv . DISCORD_TOKEN } ` ,
33
29
} ,
34
30
body : JSON . stringify ( {
35
- ...mockBody ,
36
- name : mockBody . rolename ,
31
+ ...dummyCreateBody ,
32
+ name : dummyCreateBody . rolename ,
37
33
} ) ,
38
34
}
39
35
) ;
40
36
} ) ;
41
37
42
38
test ( "should return JSON response when response is ok" , async ( ) => {
43
- const mockEnv = {
44
- DISCORD_GUILD_ID : "1234" ,
45
- DISCORD_TOKEN : "abcd" ,
46
- } ;
47
- const mockBody : createNewRole = {
48
- rolename : "test role" ,
49
- mentionable : true ,
50
- } ;
51
39
const mockResponse = { } ;
52
40
jest
53
41
. spyOn ( global , "fetch" )
54
42
. mockImplementation ( ( ) =>
55
43
Promise . resolve ( new JSONResponse ( mockResponse ) )
56
44
) ;
57
45
58
- const result = await createGuildRole ( mockBody , mockEnv ) ;
46
+ const result = await createGuildRole ( dummyCreateBody , guildEnv ) ;
59
47
60
48
expect ( result ) . toEqual ( { } ) ;
61
49
expect ( global . fetch ) . toHaveBeenCalledWith (
62
- `https://discord.com/api/v10/guilds/${ mockEnv . DISCORD_GUILD_ID } /roles` ,
50
+ `https://discord.com/api/v10/guilds/${ guildEnv . DISCORD_GUILD_ID } /roles` ,
63
51
{
64
52
method : "POST" ,
65
53
headers : {
66
54
"Content-Type" : "application/json" ,
67
- Authorization : `Bot ${ mockEnv . DISCORD_TOKEN } ` ,
55
+ Authorization : `Bot ${ guildEnv . DISCORD_TOKEN } ` ,
68
56
} ,
69
57
body : JSON . stringify ( {
70
- ...mockBody ,
71
- name : mockBody . rolename ,
58
+ ...dummyCreateBody ,
59
+ name : dummyCreateBody . rolename ,
72
60
} ) ,
73
61
}
74
62
) ;
@@ -77,14 +65,6 @@ describe("createGuildRole", () => {
77
65
78
66
describe ( "addGroupRole" , ( ) => {
79
67
test ( "should return success message when response is ok" , async ( ) => {
80
- const mockEnv = {
81
- DISCORD_GUILD_ID : "1234" ,
82
- DISCORD_TOKEN : "abcd" ,
83
- } ;
84
- const mockBody = {
85
- userid : "abcd1234" ,
86
- roleid : "defg5678" ,
87
- } ;
88
68
const mockResponse = {
89
69
ok : true ,
90
70
} ;
@@ -94,16 +74,16 @@ describe("addGroupRole", () => {
94
74
Promise . resolve ( new JSONResponse ( mockResponse ) )
95
75
) ;
96
76
97
- const result = await addGroupRole ( mockBody , mockEnv ) ;
77
+ const result = await addGroupRole ( dummyAddRoleBody , guildEnv ) ;
98
78
99
79
expect ( result ) . toEqual ( { message : "Role added successfully" } ) ;
100
80
expect ( global . fetch ) . toHaveBeenCalledWith (
101
- `https://discord.com/api/v10/guilds/${ mockEnv . DISCORD_GUILD_ID } /members/${ mockBody . userid } /roles/${ mockBody . roleid } ` ,
81
+ `https://discord.com/api/v10/guilds/${ guildEnv . DISCORD_GUILD_ID } /members/${ dummyAddRoleBody . userid } /roles/${ dummyAddRoleBody . roleid } ` ,
102
82
{
103
83
method : "PUT" ,
104
84
headers : {
105
85
"Content-Type" : "application/json" ,
106
- Authorization : `Bot ${ mockEnv . DISCORD_TOKEN } ` ,
86
+ Authorization : `Bot ${ guildEnv . DISCORD_TOKEN } ` ,
107
87
} ,
108
88
}
109
89
) ;
0 commit comments