@@ -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,123 @@ 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 . fn ( ) . mockResolvedValue ( { providerId : "upgraded" , user : { uid : "upgraded-user" } } as UserCredential ) ;
311
+ const mockRequireDisplayNameBehavior = vi . fn ( ) . mockResolvedValue ( undefined ) ;
312
+ const credential = EmailAuthProvider . credential ( email , password ) ;
313
+
314
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( _ , behavior ) => {
315
+ if ( behavior === "requireDisplayName" ) return true ;
316
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return true ;
317
+ return false ;
318
+ } ) ;
319
+
320
+ vi . mocked ( getBehavior ) . mockImplementation ( ( _ , behavior ) => {
321
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return mockAutoUpgradeBehavior ;
322
+ if ( behavior === "requireDisplayName" ) return mockRequireDisplayNameBehavior ;
323
+ return vi . fn ( ) ;
324
+ } ) ;
325
+
326
+ vi . mocked ( EmailAuthProvider . credential ) . mockReturnValue ( credential ) ;
327
+
328
+ const result = await createUserWithEmailAndPassword ( mockUI , email , password , displayName ) ;
329
+
330
+ expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "requireDisplayName" ) ;
331
+ expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "autoUpgradeAnonymousCredential" ) ;
332
+ expect ( mockAutoUpgradeBehavior ) . toHaveBeenCalledWith ( mockUI , credential ) ;
333
+ expect ( mockRequireDisplayNameBehavior ) . toHaveBeenCalledWith ( mockUI , { uid : "upgraded-user" } , displayName ) ;
334
+ expect ( result ) . toEqual ( { providerId : "upgraded" , user : { uid : "upgraded-user" } } ) ;
335
+ } ) ;
336
+
337
+ it ( "should not call requireDisplayName behavior when not enabled" , async ( ) => {
338
+ const mockUI = createMockUI ( ) ;
339
+ const email = "[email protected] " ;
340
+ const password = "password123" ;
341
+ const displayName = "John Doe" ;
342
+
343
+ const mockResult = { providerId : "password" , user : { uid : "user123" } } as UserCredential ;
344
+
345
+ vi . mocked ( hasBehavior ) . mockReturnValue ( false ) ;
346
+ vi . mocked ( _createUserWithEmailAndPassword ) . mockResolvedValue ( mockResult ) ;
347
+
348
+ const result = await createUserWithEmailAndPassword ( mockUI , email , password , displayName ) ;
349
+
350
+ expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "requireDisplayName" ) ;
351
+ expect ( getBehavior ) . not . toHaveBeenCalledWith ( mockUI , "requireDisplayName" ) ;
352
+ expect ( result ) . toBe ( mockResult ) ;
353
+ } ) ;
354
+
355
+ it ( "should handle requireDisplayName behavior errors" , async ( ) => {
356
+ const mockUI = createMockUI ( ) ;
357
+ const email = "[email protected] " ;
358
+ const password = "password123" ;
359
+ const displayName = "John Doe" ;
360
+
361
+ const mockRequireDisplayNameBehavior = vi . fn ( ) . mockRejectedValue ( new Error ( "Display name update failed" ) ) ;
362
+ const mockResult = { providerId : "password" , user : { uid : "user123" } } as UserCredential ;
363
+
364
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( _ , behavior ) => {
365
+ if ( behavior === "requireDisplayName" ) return true ;
366
+ if ( behavior === "autoUpgradeAnonymousCredential" ) return false ;
367
+ return false ;
368
+ } ) ;
369
+ vi . mocked ( getBehavior ) . mockReturnValue ( mockRequireDisplayNameBehavior ) ;
370
+ vi . mocked ( _createUserWithEmailAndPassword ) . mockResolvedValue ( mockResult ) ;
371
+
372
+ await createUserWithEmailAndPassword ( mockUI , email , password , displayName ) ;
373
+
374
+ expect ( mockRequireDisplayNameBehavior ) . toHaveBeenCalledWith ( mockUI , mockResult . user , displayName ) ;
375
+ expect ( handleFirebaseError ) . toHaveBeenCalled ( ) ;
376
+ } ) ;
252
377
} ) ;
253
378
254
379
describe ( "signInWithPhoneNumber" , ( ) => {
0 commit comments