@@ -137,8 +137,6 @@ describe('W3SSdk > Apple OAuth', () => {
137137 }
138138 } ) ( )
139139
140- Object . defineProperty ( window , 'localStorage' , { value : localStorageMock } )
141-
142140 let sdk : W3SSdk
143141 const configs : Configs = {
144142 appSettings : {
@@ -159,20 +157,24 @@ describe('W3SSdk > Apple OAuth', () => {
159157 }
160158
161159 beforeEach ( ( ) => {
162- jest . clearAllMocks ( )
160+ jest . resetAllMocks ( )
161+ Object . defineProperty ( window , 'localStorage' , { value : localStorageMock } )
163162 window . localStorage . clear ( )
164- } )
165163
166- it ( 'should perform Apple login successfully' , async ( ) => {
167164 const onLoginComplete = jest . fn ( )
168165 sdk = new W3SSdk ( configs , onLoginComplete )
169166
167+ // Simulate firebaseApp being initialized
170168 const mockFirebaseApp = { }
171169 Object . defineProperty ( sdk , 'firebaseApp' , {
172170 get : jest . fn ( ( ) => mockFirebaseApp ) ,
173171 configurable : true ,
174172 } )
175173
174+ sdk = new W3SSdk ( configs , onLoginComplete )
175+ } )
176+
177+ it ( 'should perform Apple login successfully' , async ( ) => {
176178 // Mock signInWithPopup to resolve to a UserCredential
177179 const userCredentialMock = {
178180 user : { uid : 'test-uid' } ,
@@ -202,16 +204,6 @@ describe('W3SSdk > Apple OAuth', () => {
202204 } )
203205
204206 it ( 'should handle signInWithPopup error during Apple login' , async ( ) => {
205- const onLoginComplete = jest . fn ( )
206- sdk = new W3SSdk ( configs , onLoginComplete )
207-
208- // Simulate firebaseApp being initialized
209- const mockFirebaseApp = { }
210- Object . defineProperty ( sdk , 'firebaseApp' , {
211- get : jest . fn ( ( ) => mockFirebaseApp ) ,
212- configurable : true ,
213- } )
214-
215207 // Mock getAuth
216208 const mockAuth = { getProvider : jest . fn ( ) }
217209 ; ( firebaseAuth . getAuth as jest . Mock ) . mockReturnValue ( mockAuth )
@@ -232,16 +224,6 @@ describe('W3SSdk > Apple OAuth', () => {
232224 } )
233225
234226 it ( 'should not handle signInWithPopup auth/cancelled-popup-request error during Apple login' , async ( ) => {
235- const onLoginComplete = jest . fn ( )
236- sdk = new W3SSdk ( configs , onLoginComplete )
237-
238- // Simulate firebaseApp being initialized
239- const mockFirebaseApp = { }
240- Object . defineProperty ( sdk , 'firebaseApp' , {
241- get : jest . fn ( ( ) => mockFirebaseApp ) ,
242- configurable : true ,
243- } )
244-
245227 // Mock getAuth
246228 const mockAuth = { getProvider : jest . fn ( ) }
247229 ; ( firebaseAuth . getAuth as jest . Mock ) . mockReturnValue ( mockAuth )
@@ -265,16 +247,6 @@ describe('W3SSdk > Apple OAuth', () => {
265247 } )
266248
267249 it ( 'should not handle signInWithPopup auth/popup-closed-by-user error during Apple login' , async ( ) => {
268- const onLoginComplete = jest . fn ( )
269- sdk = new W3SSdk ( configs , onLoginComplete )
270-
271- // Simulate firebaseApp being initialized
272- const mockFirebaseApp = { }
273- Object . defineProperty ( sdk , 'firebaseApp' , {
274- get : jest . fn ( ( ) => mockFirebaseApp ) ,
275- configurable : true ,
276- } )
277-
278250 // Mock getAuth
279251 const mockAuth = { getProvider : jest . fn ( ) }
280252 ; ( firebaseAuth . getAuth as jest . Mock ) . mockReturnValue ( mockAuth )
@@ -296,4 +268,47 @@ describe('W3SSdk > Apple OAuth', () => {
296268 expect ( firebaseAuth . signInWithPopup ) . toHaveBeenCalled ( )
297269 expect ( handleLoginFailureSpy ) . toHaveBeenCalledTimes ( 0 )
298270 } )
271+
272+ it ( 'should handle other signInWithPopup Firebase errors during Apple login gracefully' , async ( ) => {
273+ // Mock getAuth
274+ const mockAuth = { getProvider : jest . fn ( ) }
275+ ; ( firebaseAuth . getAuth as jest . Mock ) . mockReturnValue ( mockAuth )
276+
277+ // Mock signInWithPopup to reject
278+ const error = new FirebaseError (
279+ 'auth/user-cancelled' ,
280+ 'Firebase: Error (auth/user-cancelled).' ,
281+ )
282+ ; ( firebaseAuth . signInWithPopup as jest . Mock ) . mockRejectedValueOnce ( error )
283+
284+ // Mock handleLoginFailure
285+ const handleLoginFailureSpy = jest
286+ . spyOn ( sdk as any , 'handleFirebaseFailure' )
287+ . mockImplementation ( ( ) => { } )
288+
289+ await sdk . performLogin ( SocialLoginProvider . APPLE )
290+
291+ expect ( firebaseAuth . signInWithPopup ) . toHaveBeenCalled ( )
292+ expect ( handleLoginFailureSpy ) . toHaveBeenCalledWith ( error )
293+ } )
294+
295+ it ( 'should handle general errors during Apple login' , async ( ) => {
296+ // Mock getAuth
297+ const mockAuth = { getProvider : jest . fn ( ) }
298+ ; ( firebaseAuth . getAuth as jest . Mock ) . mockReturnValue ( mockAuth )
299+
300+ // Mock signInWithPopup to reject
301+ const error = new Error ( 'general error' )
302+ ; ( firebaseAuth . signInWithPopup as jest . Mock ) . mockRejectedValueOnce ( error )
303+
304+ // Mock handleLoginFailure
305+ const handleLoginFailureSpy = jest
306+ . spyOn ( sdk as any , 'handleLoginFailure' )
307+ . mockImplementation ( ( ) => { } )
308+
309+ await sdk . performLogin ( SocialLoginProvider . APPLE )
310+
311+ expect ( firebaseAuth . signInWithPopup ) . toHaveBeenCalled ( )
312+ expect ( handleLoginFailureSpy ) . toHaveBeenCalledTimes ( 1 )
313+ } )
299314} )
0 commit comments