@@ -12,12 +12,13 @@ import {
12
12
createUser ,
13
13
resetPassword
14
14
} from '../../../src/actions/auth'
15
-
15
+ let functionSpy
16
+ let dispatchSpy
16
17
const dispatch = ( ) => {
17
18
console . log ( 'dispatch' )
18
19
}
19
20
20
- const firebase = {
21
+ const fakeFirebase = {
21
22
_ : {
22
23
authUid : '123' ,
23
24
config : {
@@ -34,12 +35,23 @@ const firebase = {
34
35
} )
35
36
} ) ,
36
37
auth : ( ) => ( {
37
- onAuthStateChanged : ( ) => {
38
-
38
+ onAuthStateChanged : ( f ) => {
39
+ f ( { uid : 'asdfasdf' } )
39
40
} ,
40
- signOut : ( ) => { } ,
41
- createUserWithEmailAndPassword : ( ) => Promise . resolve ( ) ,
42
- sendPasswordResetEmail : ( ) => Promise . resolve ( )
41
+ signOut : ( ) =>
42
+ Promise . resolve ( { } ) ,
43
+ createUserWithEmailAndPassword : ( ) =>
44
+ Promise . resolve ( { uid : '123' , email : 'test@test.com' , providerData : [ { } ] } ) ,
45
+ signInWithCustomToken : ( ) => {
46
+ return Promise . resolve ( {
47
+ toJSON : ( ) => ( {
48
+ stsTokenManager : {
49
+ accessToken : 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJjbGFpbXMiOnsiZGlzcGxheU5hbWUiOiJFZHdhcmQgV3UiLCJlbWFpbCI6ImVkZGlld3U4MEBnbWFpbC5jb20iLCJvcmlnaW5hbElkIjoiSlFYQ2dRTkxEU1lSMFEzdjlwY3FjTDZJeGRUMiIsInByb3ZpZGVyRGF0YSI6W3siZGlzcGxheU5hbWUiOiJFZHdhcmQgV3UiLCJlbWFpbCI6ImVkZGlld3U4MEBnbWFpbC5jb20ifV19LCJ1aWQiOiJqaTh3c1BDVW5PYUhvUGZBalNCS2ZReU1pTmkxIiwiaWF0IjoxNDc1NDM3MDMyLCJleHAiOjE0NzU0NDA2MzIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHl0b29sa2l0Lmdvb2dsZWFwaXMuY29tL2dvb2dsZS5pZGVudGl0eS5pZGVudGl0eXRvb2xraXQudjEuSWRlbnRpdHlUb29sa2l0IiwiaXNzIjoicmVzaWRlLXByb2RAcmVzaWRlLXByb2QuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJzdWIiOiJyZXNpZGUtcHJvZEByZXNpZGUtcHJvZC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSJ9.aOXOCCAL-lI5AVnd8MVlc86exvCGNySq8X7DM4Gr7PG0ek5mh_8qnFfLuzw2gfv6mHNVgY2UngUmG0qETaBdox7l3wBo1GdP9hB1bM8NltCYffXwxyw7sN36BFWD3l-cz4rlxfmfzosCLj3XtDK8ARDQ76pAXxsK-rRBvMG6N-wgE_ZLf17FVvwB95e1DAmL39fp6dRVxoPflG--m4QEKVk8xIeDx4ol9HJw512gMGtTkRDMEPWVJEdaEAp6L6Lo2-Bk-TxBCHs8gpb7b7eidWMUEXObk0UPQIz2DRh-3olbruimzL_SgPNg4Pz0uUYSn11-Mx_HxxiVtyQj1ufoLA'
50
+ } ,
51
+ uid : 'asdfasdfsdf'
52
+ } )
53
+ } )
54
+ }
43
55
} )
44
56
}
45
57
@@ -49,51 +61,142 @@ describe('Actions: Auth', () => {
49
61
expect ( dispatchLoginError ( dispatch , { some : 'error' } ) )
50
62
} )
51
63
} )
64
+
52
65
describe ( 'dispatchUnauthorizedError' , ( ) => {
53
66
it ( 'calls dispatch with error' , ( ) => {
54
67
expect ( dispatchUnauthorizedError ( dispatch , { some : 'error' } ) )
55
68
} )
56
69
} )
70
+
57
71
describe ( 'dispatchLogin' , ( ) => {
58
72
it ( 'calls dispatch' , ( ) => {
59
73
expect ( dispatchLogin ( dispatch , { some : 'error' } ) )
60
74
} )
61
75
} )
76
+
62
77
describe ( 'init' , ( ) => {
63
- it . skip ( 'calls firebases onAuthStateChanged' , ( ) => {
64
- // TODO: Check that mock onAuthStateChanged function is called using spy
65
- expect ( init ( dispatch , firebase ) )
78
+ it ( 'calls firebases onAuthStateChanged' , ( ) => {
79
+ init ( dispatch , fakeFirebase )
66
80
} )
67
81
} )
82
+
68
83
describe ( 'unWatchUserProfile' , ( ) => {
69
84
it ( 'calls profile unwatch' , ( ) => {
70
85
expect ( unWatchUserProfile ( firebase ) )
71
86
} )
72
87
} )
88
+
73
89
describe ( 'watchUserProfile' , ( ) => {
74
90
it ( 'calls profile unwatch' , ( ) => {
75
- expect ( watchUserProfile ( dispatch , firebase ) )
91
+ watchUserProfile ( dispatch , fakeFirebase )
92
+ expect ( firebase . _ . profileWatch ) . to . be . a . function
93
+ } )
94
+ it ( 'sets profile watch function' , ( ) => {
95
+ watchUserProfile ( dispatch , firebase )
96
+ expect ( firebase . _ . profileWatch ) . to . be . a . function
76
97
} )
77
98
} )
99
+
100
+ describe ( 'createUserProfile' , ( ) => {
101
+ it ( 'creates profile if config is enabled' , ( ) => {
102
+ return createUserProfile ( dispatch , firebase , { uid : '123' , email : 'test@test.com' , providerData : [ { } ] } , { some : 'asdf' } )
103
+ . then ( ( profile ) => {
104
+ expect ( profile ) . to . be . an . object
105
+ } )
106
+ } , 4000 )
107
+ } )
108
+
78
109
describe ( 'login' , ( ) => {
79
- // skipped due to TypeError: Cannot read property 'apply' of undefined
80
- it . skip ( 'logs user into Firebase' , ( ) => {
81
- expect ( login ( dispatch , firebase , { email : 'test@tst.com' , password : 'asdfasdf' } ) )
82
- } )
110
+ it ( 'handles invalid email login' , ( ) => {
111
+ return login ( dispatch , firebase , { email : 'test@tst.com' , password : 'asdfasdf' } )
112
+ . catch ( ( err ) => {
113
+ expect ( err . code ) . to . equal ( 'auth/user-not-found' )
114
+ } )
115
+ } , 4000 )
116
+ it ( 'handles invalid token login' , ( ) => {
117
+ return login ( dispatch , firebase , { token : 'test@tst.com' } )
118
+ . catch ( ( err ) => {
119
+ expect ( err . code ) . to . equal ( 'auth/invalid-custom-token' )
120
+ } )
121
+ } , 4000 )
122
+ it ( 'handles token login' , ( ) => {
123
+ const token = 'asdfasdf'
124
+ return login ( dispatch , fakeFirebase , { token } , { uid : 'asdfasdf' } )
125
+ . then ( ( authData ) => {
126
+ expect ( authData ) . to . be . an . object
127
+ } )
128
+ } , 4000 )
83
129
} )
130
+
84
131
describe ( 'logout' , ( ) => {
85
- it ( 'logs user out of Firebase' , ( ) => {
86
- expect ( logout ( dispatch , firebase ) ) . to . eventually . be . fullfilled
132
+ beforeEach ( ( ) => {
133
+ functionSpy = sinon . spy ( firebase . auth ( ) , 'signOut' )
134
+ } )
135
+ afterEach ( ( ) => {
136
+ firebase . auth ( ) . signOut . restore ( )
137
+ } )
138
+ it ( 'calls firebase.auth().signOut()' , ( ) => {
139
+ return logout ( dispatch , firebase )
140
+ . then ( ( ) => {
141
+ expect ( functionSpy ) . to . have . been . calledOnce
142
+ } )
143
+ } )
144
+ it ( 'calls firebase.auth().signOut()' , ( ) => {
145
+ logout ( dispatch , firebase )
146
+ expect ( functionSpy ) . to . have . been . calledOnce
147
+ } )
148
+ it ( 'sets authUid to null' , ( ) => {
149
+ fakeFirebase . _ . authUid = 'asdfasdf'
150
+ return logout ( dispatch , fakeFirebase )
151
+ . then ( ( ) => {
152
+ expect ( fakeFirebase . _ . authUid ) . to . be . null
153
+ } )
154
+ } )
155
+ it . skip ( 'calls dispatch' , ( ) => {
156
+ dispatchSpy = sinon . spy ( dispatch )
157
+ return logout ( dispatch , firebase )
158
+ . then ( ( ) => {
159
+ expect ( dispatchSpy ) . to . have . been . calledOnce
160
+ } )
87
161
} )
88
162
} )
163
+
89
164
describe ( 'createUser' , ( ) => {
90
- it ( 'creates user' , ( ) => {
91
- expect ( createUser ( dispatch , firebase , { email : 'test@test.com' , password : 'asdf' } ) )
165
+ // Skipped because of TypeError: Cannot read property 'apply' of undefined
166
+ it . skip ( 'creates user' , ( ) => {
167
+ return createUser ( dispatch , fakeFirebase , { email : 'test@test.com' , password : 'asdf' } , { email : 'test@test.com' , password : 'asdf' } )
168
+ . then ( userData => {
169
+ expect ( userData ) . to . be . an . object
170
+ } )
92
171
} )
93
- } )
172
+ it ( 'handles no email' , ( ) => {
173
+ return createUser ( dispatch , fakeFirebase , { password : 'asdf' } )
174
+ . catch ( ( err ) => {
175
+ expect ( err ) . to . be . a . string
176
+ } )
177
+ } )
178
+ it ( 'handles no password' , ( ) => {
179
+ return createUser ( dispatch , fakeFirebase , { email : 'asdf@asdf.com' } )
180
+ . catch ( ( err ) => {
181
+ expect ( err ) . to . be . a . string
182
+ } )
183
+ } )
184
+ } , 4000 )
185
+
94
186
describe ( 'resetPassword' , ( ) => {
95
- it ( 'calls to reset user password' , ( ) => {
96
- expect ( resetPassword ( dispatch , firebase , 'test@test.com' ) ) . to . eventually . be . fulfilled
187
+ beforeEach ( ( ) => {
188
+ functionSpy = sinon . spy ( firebase . auth ( ) , 'sendPasswordResetEmail' )
189
+ } )
190
+ afterEach ( ( ) => {
191
+ firebase . auth ( ) . sendPasswordResetEmail . restore ( )
97
192
} )
193
+ it ( 'dispatches error for invalid user' , ( ) => {
194
+ return resetPassword ( dispatch , firebase , 'test@test.com' )
195
+ . catch ( ( err ) => {
196
+ console . log ( 'error' , err )
197
+ expect ( err . code ) . to . equal ( 'auth/user-not-found' )
198
+ expect ( functionSpy ) . to . have . been . calledOnce
199
+ } )
200
+ } , 4000 )
98
201
} )
99
202
} )
0 commit comments