@@ -193,7 +193,11 @@ describe("createUserWithEmailAndPassword", () => {
193
193
const password = "password123" ;
194
194
195
195
const credential = EmailAuthProvider . credential ( email , password ) ;
196
- vi . mocked ( hasBehavior ) . mockReturnValue ( true ) ;
196
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( ui , behavior ) => {
197
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return true ;
198
+ if ( behavior === "requireDisplayName" ) return false ;
199
+ return false ;
200
+ } ) ;
197
201
const mockBehavior = vi . fn ( ) . mockResolvedValue ( { providerId : "password" } as UserCredential ) ;
198
202
vi . mocked ( getBehavior ) . mockReturnValue ( mockBehavior ) ;
199
203
@@ -215,7 +219,11 @@ describe("createUserWithEmailAndPassword", () => {
215
219
const password = "password123" ;
216
220
217
221
const credential = EmailAuthProvider . credential ( email , password ) ;
218
- vi . mocked ( hasBehavior ) . mockReturnValue ( true ) ;
222
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( ui , behavior ) => {
223
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return true ;
224
+ if ( behavior === "requireDisplayName" ) return false ;
225
+ return false ;
226
+ } ) ;
219
227
const mockBehavior = vi . fn ( ) . mockResolvedValue ( undefined ) ;
220
228
vi . mocked ( getBehavior ) . mockReturnValue ( mockBehavior ) ;
221
229
@@ -249,6 +257,125 @@ describe("createUserWithEmailAndPassword", () => {
249
257
expect ( handleFirebaseError ) . toHaveBeenCalledWith ( mockUI , error ) ;
250
258
expect ( vi . mocked ( mockUI . setState ) . mock . calls ) . toEqual ( [ [ "pending" ] , [ "idle" ] ] ) ;
251
259
} ) ;
260
+
261
+ it ( "should call handleFirebaseError when requireDisplayName behavior is enabled but no displayName provided" , async ( ) => {
262
+ const mockUI = createMockUI ( ) ;
263
+ const email = "[email protected] " ;
264
+ const password = "password123" ;
265
+
266
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( _ , behavior ) => {
267
+ if ( behavior === "requireDisplayName" ) return true ;
268
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return false ;
269
+ return false ;
270
+ } ) ;
271
+
272
+ await createUserWithEmailAndPassword ( mockUI , email , password ) ;
273
+
274
+ expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "requireDisplayName" ) ;
275
+ expect ( _createUserWithEmailAndPassword ) . not . toHaveBeenCalled ( ) ;
276
+ expect ( handleFirebaseError ) . toHaveBeenCalled ( ) ;
277
+ } ) ;
278
+
279
+ it ( "should call requireDisplayName behavior when enabled and displayName provided" , async ( ) => {
280
+ const mockUI = createMockUI ( ) ;
281
+ const email = "[email protected] " ;
282
+ const password = "password123" ;
283
+ const displayName = "John Doe" ;
284
+
285
+ const mockRequireDisplayNameBehavior = vi . fn ( ) . mockResolvedValue ( undefined ) ;
286
+ const mockResult = { providerId : "password" , user : { uid : "user123" } } as UserCredential ;
287
+
288
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( _ , behavior ) => {
289
+ if ( behavior === "requireDisplayName" ) return true ;
290
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return false ;
291
+ return false ;
292
+ } ) ;
293
+ vi . mocked ( getBehavior ) . mockReturnValue ( mockRequireDisplayNameBehavior ) ;
294
+ vi . mocked ( _createUserWithEmailAndPassword ) . mockResolvedValue ( mockResult ) ;
295
+
296
+ const result = await createUserWithEmailAndPassword ( mockUI , email , password , displayName ) ;
297
+
298
+ expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "requireDisplayName" ) ;
299
+ expect ( getBehavior ) . toHaveBeenCalledWith ( mockUI , "requireDisplayName" ) ;
300
+ expect ( mockRequireDisplayNameBehavior ) . toHaveBeenCalledWith ( mockUI , mockResult . user , displayName ) ;
301
+ expect ( result ) . toBe ( mockResult ) ;
302
+ } ) ;
303
+
304
+ it ( "should call requireDisplayName behavior after autoUpgradeAnonymousCredential when both enabled" , async ( ) => {
305
+ const mockUI = createMockUI ( ) ;
306
+ const email = "[email protected] " ;
307
+ const password = "password123" ;
308
+ const displayName = "John Doe" ;
309
+
310
+ const mockAutoUpgradeBehavior = vi
311
+ . fn ( )
312
+ . mockResolvedValue ( { providerId : "upgraded" , user : { uid : "upgraded-user" } } as UserCredential ) ;
313
+ const mockRequireDisplayNameBehavior = vi . fn ( ) . mockResolvedValue ( undefined ) ;
314
+ const credential = EmailAuthProvider . credential ( email , password ) ;
315
+
316
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( _ , behavior ) => {
317
+ if ( behavior === "requireDisplayName" ) return true ;
318
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return true ;
319
+ return false ;
320
+ } ) ;
321
+
322
+ vi . mocked ( getBehavior ) . mockImplementation ( ( _ , behavior ) => {
323
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return mockAutoUpgradeBehavior ;
324
+ if ( behavior === "requireDisplayName" ) return mockRequireDisplayNameBehavior ;
325
+ return vi . fn ( ) ;
326
+ } ) ;
327
+
328
+ vi . mocked ( EmailAuthProvider . credential ) . mockReturnValue ( credential ) ;
329
+
330
+ const result = await createUserWithEmailAndPassword ( mockUI , email , password , displayName ) ;
331
+
332
+ expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "requireDisplayName" ) ;
333
+ expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "autoUpgradeAnonymousCredential" ) ;
334
+ expect ( mockAutoUpgradeBehavior ) . toHaveBeenCalledWith ( mockUI , credential ) ;
335
+ expect ( mockRequireDisplayNameBehavior ) . toHaveBeenCalledWith ( mockUI , { uid : "upgraded-user" } , displayName ) ;
336
+ expect ( result ) . toEqual ( { providerId : "upgraded" , user : { uid : "upgraded-user" } } ) ;
337
+ } ) ;
338
+
339
+ it ( "should not call requireDisplayName behavior when not enabled" , async ( ) => {
340
+ const mockUI = createMockUI ( ) ;
341
+ const email = "[email protected] " ;
342
+ const password = "password123" ;
343
+ const displayName = "John Doe" ;
344
+
345
+ const mockResult = { providerId : "password" , user : { uid : "user123" } } as UserCredential ;
346
+
347
+ vi . mocked ( hasBehavior ) . mockReturnValue ( false ) ;
348
+ vi . mocked ( _createUserWithEmailAndPassword ) . mockResolvedValue ( mockResult ) ;
349
+
350
+ const result = await createUserWithEmailAndPassword ( mockUI , email , password , displayName ) ;
351
+
352
+ expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "requireDisplayName" ) ;
353
+ expect ( getBehavior ) . not . toHaveBeenCalledWith ( mockUI , "requireDisplayName" ) ;
354
+ expect ( result ) . toBe ( mockResult ) ;
355
+ } ) ;
356
+
357
+ it ( "should handle requireDisplayName behavior errors" , async ( ) => {
358
+ const mockUI = createMockUI ( ) ;
359
+ const email = "[email protected] " ;
360
+ const password = "password123" ;
361
+ const displayName = "John Doe" ;
362
+
363
+ const mockRequireDisplayNameBehavior = vi . fn ( ) . mockRejectedValue ( new Error ( "Display name update failed" ) ) ;
364
+ const mockResult = { providerId : "password" , user : { uid : "user123" } } as UserCredential ;
365
+
366
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( _ , behavior ) => {
367
+ if ( behavior === "requireDisplayName" ) return true ;
368
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return false ;
369
+ return false ;
370
+ } ) ;
371
+ vi . mocked ( getBehavior ) . mockReturnValue ( mockRequireDisplayNameBehavior ) ;
372
+ vi . mocked ( _createUserWithEmailAndPassword ) . mockResolvedValue ( mockResult ) ;
373
+
374
+ await createUserWithEmailAndPassword ( mockUI , email , password , displayName ) ;
375
+
376
+ expect ( mockRequireDisplayNameBehavior ) . toHaveBeenCalledWith ( mockUI , mockResult . user , displayName ) ;
377
+ expect ( handleFirebaseError ) . toHaveBeenCalled ( ) ;
378
+ } ) ;
252
379
} ) ;
253
380
254
381
describe ( "signInWithPhoneNumber" , ( ) => {
0 commit comments