@@ -14,10 +14,8 @@ import {
1414} from '../../../src/actions/auth'
1515let functionSpy
1616let dispatchSpy
17- const dispatch = ( ) => {
18- console . log ( 'dispatch' )
19- }
20-
17+ const dispatch = sinon . spy ( )
18+ const fakeLogin = { email :
'[email protected] ' , password :
'asdfasdf' } 2119const fakeFirebase = {
2220 _ : {
2321 authUid : '123' ,
@@ -40,8 +38,10 @@ const fakeFirebase = {
4038 } ,
4139 signOut : ( ) =>
4240 Promise . resolve ( { } ) ,
43- createUserWithEmailAndPassword : ( ) =>
44- Promise . resolve ( { uid :
'123' , email :
'[email protected] ' , providerData :
[ { } ] } ) , 41+ createUserWithEmailAndPassword : ( email , password ) =>
42+ email === 'error'
43+ ? Promise . reject ( { code : 'asdfasdf' } )
44+ :
Promise . resolve ( { uid :
'123' , email :
'[email protected] ' , providerData :
[ { } ] } ) , 4545 signInWithCustomToken : ( ) => {
4646 return Promise . resolve ( {
4747 toJSON : ( ) => ( {
@@ -51,7 +51,19 @@ const fakeFirebase = {
5151 uid : 'asdfasdfsdf'
5252 } )
5353 } )
54- }
54+ } ,
55+ signInWithEmailAndPassword : ( email , password ) =>
56+ email . indexOf ( 'error2' ) !== - 1
57+ ? Promise . reject ( { code : 'asdfasdf' } )
58+ : email === 'error3'
59+ ? Promise . reject ( { code : 'auth/user-not-found' } )
60+ :
Promise . resolve ( { uid :
'123' , email :
'[email protected] ' , providerData :
[ { } ] } ) , 61+ sendPasswordResetEmail : ( email ) =>
62+ email === 'error'
63+ ? Promise . reject ( { code : 'auth/user-not-found' } )
64+ : email === 'error2'
65+ ? Promise . reject ( { code : 'asdfasdf' } )
66+ : Promise . resolve ( { some : 'val' } )
5567 } )
5668}
5769
@@ -87,6 +99,9 @@ describe('Actions: Auth', () => {
8799 } )
88100
89101 describe ( 'watchUserProfile' , ( ) => {
102+ beforeEach ( ( ) => {
103+ functionSpy = sinon . spy ( )
104+ } )
90105 it ( 'calls profile unwatch' , ( ) => {
91106 watchUserProfile ( dispatch , fakeFirebase )
92107 expect ( firebase . _ . profileWatch ) . to . be . a . function
@@ -95,6 +110,17 @@ describe('Actions: Auth', () => {
95110 watchUserProfile ( dispatch , firebase )
96111 expect ( firebase . _ . profileWatch ) . to . be . a . function
97112 } )
113+ it ( 'sets populates using array' , ( ) => {
114+ firebase . _ . config . profileParamsToPopulate = [ 'asdfasdf' ]
115+ watchUserProfile ( dispatch , firebase )
116+ expect ( firebase . _ . profileWatch ) . to . be . a . function
117+ } )
118+ it ( 'sets populates using string' , ( ) => {
119+ firebase . _ . config . profileParamsToPopulate = 'asdfasdf'
120+ watchUserProfile ( dispatch , firebase )
121+ expect ( firebase . _ . profileWatch ) . to . be . a . function
122+ } )
123+
98124 } )
99125
100126 describe ( 'createUserProfile' , ( ) => {
@@ -108,7 +134,7 @@ describe('Actions: Auth', () => {
108134
109135 describe ( 'login' , ( ) => {
110136 it ( 'handles invalid email login' , ( ) => {
111- return login ( dispatch , firebase , { email : '[email protected] ' , password : 'asdfasdf' } ) 137+ return login ( dispatch , firebase , fakeLogin )
112138 . catch ( ( err ) => {
113139 expect ( err . code ) . to . equal ( 'auth/user-not-found' )
114140 } )
@@ -141,10 +167,7 @@ describe('Actions: Auth', () => {
141167 expect ( functionSpy ) . to . have . been . calledOnce
142168 } )
143169 } )
144- it ( 'calls firebase.auth().signOut()' , ( ) => {
145- logout ( dispatch , firebase )
146- expect ( functionSpy ) . to . have . been . calledOnce
147- } )
170+
148171 it ( 'sets authUid to null' , ( ) => {
149172 fakeFirebase . _ . authUid = 'asdfasdf'
150173 return logout ( dispatch , fakeFirebase )
@@ -162,41 +185,68 @@ describe('Actions: Auth', () => {
162185 } )
163186
164187 describe ( 'createUser' , ( ) => {
165- // Skipped because of TypeError: Cannot read property 'apply' of undefined
166- it . skip ( 'creates user' , ( ) => {
167- return createUser ( dispatch , fakeFirebase , { email :
'[email protected] ' , password :
'asdf' } , { email :
'[email protected] ' , password :
'asdf' } ) 188+ it ( 'creates user' , ( ) =>
189+ createUser ( dispatch , fakeFirebase , fakeLogin , fakeLogin )
168190 . then ( userData => {
169191 expect ( userData ) . to . be . an . object
170192 } )
171- } )
172- it ( 'handles no email' , ( ) => {
173- return createUser ( dispatch , fakeFirebase , { password : 'asdf' } )
193+ )
194+ it ( 'creates user without profile' , ( ) =>
195+ createUser ( dispatch , fakeFirebase , fakeLogin )
196+ . then ( userData => {
197+ expect ( userData ) . to . be . an . object
198+ } )
199+ )
200+ it ( 'handles no email' , ( ) =>
201+ createUser ( dispatch , fakeFirebase , { password : fakeLogin . password } )
174202 . catch ( ( err ) => {
175- expect ( err ) . to . be . a . string
203+ expect ( err ) . to . be . an . object
176204 } )
177- } )
178- it ( 'handles no password' , ( ) => {
179- return createUser ( dispatch , fakeFirebase , { email :
'[email protected] ' } ) 205+ )
206+ it ( 'handles no password' , ( ) =>
207+ createUser ( dispatch , fakeFirebase , { email : fakeLogin . email } )
180208 . catch ( ( err ) => {
181- expect ( err ) . to . be . a . string
209+ expect ( err ) . to . be . an . object
182210 } )
183- } )
184- } , 4000 )
211+ )
212+ it ( 'handles error with createUserWithEmailAndPassword' , ( ) =>
213+ createUser ( dispatch , fakeFirebase , { email : 'error' , password : 'error' } )
214+ . catch ( ( err ) => {
215+ expect ( err ) . to . be . an . object
216+ } )
217+ )
218+ it ( 'handles error with login' , ( ) =>
219+ createUser ( dispatch , fakeFirebase , { email : 'error2' , password : 'error2' } )
220+ . catch ( ( err ) => {
221+ expect ( err ) . to . be . an . object
222+ } )
223+ )
224+ it ( 'handles user-not-found error' , ( ) =>
225+ createUser ( dispatch , fakeFirebase , { email : 'error3' , password : 'error2' } )
226+ . catch ( ( err ) => {
227+ expect ( err ) . to . be . an . object
228+ } )
229+ )
230+ } )
185231
186232 describe ( 'resetPassword' , ( ) => {
187- beforeEach ( ( ) => {
188- functionSpy = sinon . spy ( firebase . auth ( ) , 'sendPasswordResetEmail ')
189- } )
190- afterEach ( ( ) => {
191- firebase . auth ( ) . sendPasswordResetEmail . restore ( )
233+ it ( 'resets password for real user' , ( ) => {
234+ return resetPassword ( dispatch , fakeFirebase , '[email protected] ') 235+ . catch ( ( err ) => {
236+ expect ( err . code ) . to . equal ( 'auth/user-not-found' )
237+ } )
192238 } )
193239 it ( 'dispatches error for invalid user' , ( ) => {
194- return resetPassword ( dispatch , firebase , '[email protected] ' ) 240+ return resetPassword ( dispatch , fakeFirebase , 'error ' )
195241 . catch ( ( err ) => {
196- console . log ( 'error' , err )
197242 expect ( err . code ) . to . equal ( 'auth/user-not-found' )
198- expect ( functionSpy ) . to . have . been . calledOnce
199243 } )
200- } , 4000 )
244+ } )
245+ it ( 'dispatches for all other errors' , ( ) => {
246+ return resetPassword ( dispatch , fakeFirebase , 'error2' )
247+ . catch ( ( err ) => {
248+ expect ( err . code ) . to . be . a . string
249+ } )
250+ } )
201251 } )
202252} )
0 commit comments