@@ -4,6 +4,7 @@ import { authenticateUser } from '../ducks/actions';
44import authClient from '../authClient' ;
55import { C4CState , initialStoreState , ThunkExtraArgs } from '../../store' ;
66import tokenService from '../token' ;
7+ import { AxiosError } from 'axios' ;
78
89export const generateState = ( partialState : Partial < C4CState > ) : C4CState => ( {
910 ...initialStoreState ,
@@ -12,7 +13,7 @@ export const generateState = (partialState: Partial<C4CState>): C4CState => ({
1213
1314describe ( 'User Authentication Thunks' , ( ) => {
1415 describe ( 'login' , ( ) => {
15- it ( 'dispatches an AuthSuccess action after login' , async ( ) => {
16+ it ( 'dispatches an authenticateUser.loaded() action after login' , async ( ) => {
1617 const getState = ( ) => generateState ( { } ) ;
1718 const mockDispatch = jest . fn ( ) ;
1819 const mockLogin = jest . fn ( ) ;
@@ -52,7 +53,44 @@ describe('User Authentication Thunks', () => {
5253 ) ;
5354 } ) ;
5455
55- it ( 'dispatches an AuthSuccess action after signup' , async ( ) => {
56+ it ( 'dispatches authenticateUser.failed() action when API fails' , async ( ) => {
57+ const getState = ( ) => generateState ( { } ) ;
58+ const mockDispatch = jest . fn ( ) ;
59+ const mockLogin = jest . fn ( ) ;
60+ const mockSetRefreshToken = jest . fn ( ) ;
61+ const mockAPIError = {
62+ response : {
63+ data : 'Unauthenticated user' ,
64+ } ,
65+ } ;
66+ mockLogin . mockRejectedValue ( mockAPIError ) ;
67+ const mockExtraArgs : ThunkExtraArgs = {
68+ authClient : {
69+ ...authClient ,
70+ login : mockLogin ,
71+ } ,
72+ tokenService : {
73+ ...tokenService ,
74+ setRefreshToken : mockSetRefreshToken ,
75+ } ,
76+ } ;
77+
78+ await login ( {
79+ email : 'Jack Blanc' ,
80+ password : 'password' ,
81+ } ) ( mockDispatch , getState , mockExtraArgs ) ;
82+
83+ expect ( mockDispatch ) . toHaveBeenCalledTimes ( 1 ) ;
84+ expect ( mockDispatch ) . toHaveBeenNthCalledWith (
85+ 1 ,
86+ authenticateUser . failed ( mockAPIError . response . data ) ,
87+ ) ;
88+ expect ( mockLogin ) . toBeCalledTimes ( 1 ) ;
89+ } ) ;
90+ } ) ;
91+
92+ describe ( 'signup' , ( ) => {
93+ it ( 'dispatches an authenticateUser.loaded() action after signup' , async ( ) => {
5694 const getState = ( ) => generateState ( { } ) ;
5795 const mockDispatch = jest . fn ( ) ;
5896 const mockSignup = jest . fn ( ) ;
@@ -93,5 +131,41 @@ describe('User Authentication Thunks', () => {
93131 mockTokenResponse . refreshToken ,
94132 ) ;
95133 } ) ;
134+ it ( 'dispatches authenticateUser.failed() action when API fails' , async ( ) => {
135+ const getState = ( ) => generateState ( { } ) ;
136+ const mockDispatch = jest . fn ( ) ;
137+ const mockSignup = jest . fn ( ) ;
138+ const mockSetRefreshToken = jest . fn ( ) ;
139+ const mockAPIError = {
140+ response : {
141+ data : 'Unauthenticated user' ,
142+ } ,
143+ } ;
144+ mockSignup . mockRejectedValue ( mockAPIError ) ;
145+ const mockExtraArgs : ThunkExtraArgs = {
146+ authClient : {
147+ ...authClient ,
148+ signup : mockSignup ,
149+ } ,
150+ tokenService : {
151+ ...tokenService ,
152+ setRefreshToken : mockSetRefreshToken ,
153+ } ,
154+ } ;
155+
156+ await signup ( {
157+ email : 'jblanc222@gmail.com' ,
158+ password : 'password' ,
159+ firstName : 'Jack' ,
160+ lastName : 'Blanc' ,
161+ } ) ( mockDispatch , getState , mockExtraArgs ) ;
162+
163+ expect ( mockDispatch ) . toHaveBeenCalledTimes ( 1 ) ;
164+ expect ( mockDispatch ) . toHaveBeenNthCalledWith (
165+ 1 ,
166+ authenticateUser . failed ( mockAPIError . response . data ) ,
167+ ) ;
168+ expect ( mockSignup ) . toBeCalledTimes ( 1 ) ;
169+ } ) ;
96170 } ) ;
97171} ) ;
0 commit comments