@@ -2,6 +2,7 @@ import config from "../../../config/config";
22import { UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST } from "../../../src/constants/responses" ;
33import { DISCORD_BASE_URL } from "../../../src/constants/urls" ;
44import { generateDiscordAuthToken } from "../../../src/utils/authTokenGenerator" ;
5+ import JSONResponse from "../../../src/utils/JsonResponse" ;
56import {
67 createOnboardingExtension ,
78 CreateOnboardingExtensionArgs ,
@@ -46,8 +47,10 @@ describe("createOnboaringExtension", () => {
4647 jest . restoreAllMocks ( ) ;
4748 } ) ;
4849
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 ) ) ;
5052 await createOnboardingExtension ( args , guildEnv ) ;
53+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 2 ) ;
5154 expect ( utils . sendReplyInDiscordChannel ) . toHaveBeenCalledTimes ( 1 ) ;
5255 expect ( utils . sendReplyInDiscordChannel ) . toHaveBeenCalledWith (
5356 discordReplyUrl ,
@@ -56,7 +59,46 @@ describe("createOnboaringExtension", () => {
5659 ) ;
5760 } ) ;
5861
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 ( ) => {
60102 args . userId = undefined ;
61103 requestData . userId = args . discordId ;
62104 await createOnboardingExtension ( args , guildEnv ) ;
0 commit comments