@@ -2,6 +2,7 @@ import config from "../../../config/config";
2
2
import { UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST } from "../../../src/constants/responses" ;
3
3
import { DISCORD_BASE_URL } from "../../../src/constants/urls" ;
4
4
import { generateDiscordAuthToken } from "../../../src/utils/authTokenGenerator" ;
5
+ import JSONResponse from "../../../src/utils/JsonResponse" ;
5
6
import {
6
7
createOnboardingExtension ,
7
8
CreateOnboardingExtensionArgs ,
@@ -46,8 +47,10 @@ describe("createOnboaringExtension", () => {
46
47
jest . restoreAllMocks ( ) ;
47
48
} ) ;
48
49
49
- it ( "should call sendReplyInDiscordChannel with error content" , async ( ) => {
50
+ it ( "should call sendReplyInDicordChannel when user is not a super user" , async ( ) => {
51
+ fetchSpy . mockImplementation ( ( ) => new JSONResponse ( null ) ) ;
50
52
await createOnboardingExtension ( args , guildEnv ) ;
53
+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 2 ) ;
51
54
expect ( utils . sendReplyInDiscordChannel ) . toHaveBeenCalledTimes ( 1 ) ;
52
55
expect ( utils . sendReplyInDiscordChannel ) . toHaveBeenCalledWith (
53
56
discordReplyUrl ,
@@ -56,7 +59,46 @@ describe("createOnboaringExtension", () => {
56
59
) ;
57
60
} ) ;
58
61
59
- it ( "should call fetch with success response" , async ( ) => {
62
+ it ( "should call sendReplyInDiscordChannel with error response for invalid request body" , async ( ) => {
63
+ const message = "numberOfDays must be positive" ;
64
+ const errorContent = `<@${ args . discordId } > ${ message } ` ;
65
+ const response = new JSONResponse ( { message } ) ;
66
+
67
+ fetchSpy . mockImplementation ( ( ) => response ) ;
68
+
69
+ args . numberOfDays = - 1 ;
70
+ args . userId = undefined ;
71
+ requestData . numberOfDays = args . numberOfDays ;
72
+
73
+ await createOnboardingExtension ( args , guildEnv ) ;
74
+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 2 ) ;
75
+ expect ( utils . sendReplyInDiscordChannel ) . toHaveBeenCalledTimes ( 1 ) ;
76
+ expect ( utils . sendReplyInDiscordChannel ) . toHaveBeenCalledWith (
77
+ discordReplyUrl ,
78
+ errorContent ,
79
+ guildEnv
80
+ ) ;
81
+ } ) ;
82
+
83
+ it ( "should call sendReplyInDiscordChannel with error content for unexpected error" , async ( ) => {
84
+ fetchSpy . mockImplementation ( ( ) => {
85
+ throw new Error ( "Unexpected Error" ) ;
86
+ } ) ;
87
+ try {
88
+ await createOnboardingExtension ( args , guildEnv ) ;
89
+ } catch ( error ) {
90
+ expect ( utils . sendReplyInDiscordChannel ) . toHaveBeenCalledTimes ( 1 ) ;
91
+ expect ( utils . sendReplyInDiscordChannel ) . toHaveBeenCalledWith (
92
+ discordReplyUrl ,
93
+ `<@${
94
+ args . discordId
95
+ } > ${ "Error occurred while creating onboarding extension request." } `,
96
+ guildEnv
97
+ ) ;
98
+ }
99
+ } ) ;
100
+
101
+ it ( "should call sendReplyInDiscordChannel with success response" , async ( ) => {
60
102
args . userId = undefined ;
61
103
requestData . userId = args . discordId ;
62
104
await createOnboardingExtension ( args , guildEnv ) ;
0 commit comments