@@ -2,7 +2,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
2
import {
3
3
signInWithEmailAndPassword ,
4
4
createUserWithEmailAndPassword ,
5
- signInWithPhoneNumber ,
5
+ verifyPhoneNumber ,
6
6
confirmPhoneNumber ,
7
7
sendPasswordResetEmail ,
8
8
sendSignInLinkToEmail ,
@@ -16,7 +16,6 @@ import {
16
16
vi . mock ( "firebase/auth" , ( ) => ( {
17
17
signInWithCredential : vi . fn ( ) ,
18
18
createUserWithEmailAndPassword : vi . fn ( ) ,
19
- signInWithPhoneNumber : vi . fn ( ) ,
20
19
sendPasswordResetEmail : vi . fn ( ) ,
21
20
sendSignInLinkToEmail : vi . fn ( ) ,
22
21
signInAnonymously : vi . fn ( ) ,
@@ -26,9 +25,9 @@ vi.mock("firebase/auth", () => ({
26
25
credential : vi . fn ( ) ,
27
26
credentialWithLink : vi . fn ( ) ,
28
27
} ,
29
- PhoneAuthProvider : {
28
+ PhoneAuthProvider : Object . assign ( vi . fn ( ) , {
30
29
credential : vi . fn ( ) ,
31
- } ,
30
+ } ) ,
32
31
linkWithCredential : vi . fn ( ) ,
33
32
} ) ) ;
34
33
@@ -47,15 +46,12 @@ import {
47
46
EmailAuthProvider ,
48
47
PhoneAuthProvider ,
49
48
createUserWithEmailAndPassword as _createUserWithEmailAndPassword ,
50
- signInWithPhoneNumber as _signInWithPhoneNumber ,
51
49
sendPasswordResetEmail as _sendPasswordResetEmail ,
52
50
sendSignInLinkToEmail as _sendSignInLinkToEmail ,
53
51
signInAnonymously as _signInAnonymously ,
54
- signInWithRedirect ,
55
52
isSignInWithEmailLink as _isSignInWithEmailLink ,
56
53
UserCredential ,
57
54
Auth ,
58
- ConfirmationResult ,
59
55
AuthProvider ,
60
56
TotpSecret ,
61
57
} from "firebase/auth" ;
@@ -195,7 +191,7 @@ describe("createUserWithEmailAndPassword", () => {
195
191
const password = "password123" ;
196
192
197
193
const credential = EmailAuthProvider . credential ( email , password ) ;
198
- vi . mocked ( hasBehavior ) . mockImplementation ( ( ui , behavior ) => {
194
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( _ , behavior ) => {
199
195
if ( behavior === "autoUpgradeAnonymousCredential" ) return true ;
200
196
if ( behavior === "requireDisplayName" ) return false ;
201
197
return false ;
@@ -221,7 +217,7 @@ describe("createUserWithEmailAndPassword", () => {
221
217
const password = "password123" ;
222
218
223
219
const credential = EmailAuthProvider . credential ( email , password ) ;
224
- vi . mocked ( hasBehavior ) . mockImplementation ( ( ui , behavior ) => {
220
+ vi . mocked ( hasBehavior ) . mockImplementation ( ( _ , behavior ) => {
225
221
if ( behavior === "autoUpgradeAnonymousCredential" ) return true ;
226
222
if ( behavior === "requireDisplayName" ) return false ;
227
223
return false ;
@@ -380,44 +376,54 @@ describe("createUserWithEmailAndPassword", () => {
380
376
} ) ;
381
377
} ) ;
382
378
383
- describe ( "signInWithPhoneNumber " , ( ) => {
379
+ describe ( "verifyPhoneNumber " , ( ) => {
384
380
beforeEach ( ( ) => {
385
381
vi . clearAllMocks ( ) ;
386
382
} ) ;
387
383
388
- it ( "should update state and call signInWithPhoneNumber successfully" , async ( ) => {
384
+ it ( "should update state and call PhoneAuthProvider.verifyPhoneNumber successfully" , async ( ) => {
389
385
const mockUI = createMockUI ( ) ;
390
386
const phoneNumber = "+1234567890" ;
391
- const mockRecaptchaVerifier = { } as any ;
392
- const mockConfirmationResult = {
393
- verificationId : "test-verification-id" ,
394
- confirm : vi . fn ( ) ,
395
- } as any ;
396
-
397
- vi . mocked ( _signInWithPhoneNumber ) . mockResolvedValue ( mockConfirmationResult ) ;
387
+ const mockAppVerifier = { } as any ;
388
+ const mockVerificationId = "test-verification-id" ;
389
+
390
+ const mockVerifyPhoneNumber = vi . fn ( ) . mockResolvedValue ( mockVerificationId ) ;
391
+ vi . mocked ( PhoneAuthProvider ) . mockImplementation (
392
+ ( ) =>
393
+ ( {
394
+ verifyPhoneNumber : mockVerifyPhoneNumber ,
395
+ } ) as any
396
+ ) ;
398
397
399
- const result = await signInWithPhoneNumber ( mockUI , phoneNumber , mockRecaptchaVerifier ) ;
398
+ const result = await verifyPhoneNumber ( mockUI , phoneNumber , mockAppVerifier ) ;
400
399
401
400
// Verify state management
402
401
expect ( vi . mocked ( mockUI . setState ) . mock . calls ) . toEqual ( [ [ "pending" ] , [ "idle" ] ] ) ;
403
402
404
- // Verify the Firebase function was called with correct parameters
405
- expect ( _signInWithPhoneNumber ) . toHaveBeenCalledWith ( mockUI . auth , phoneNumber , mockRecaptchaVerifier ) ;
406
- expect ( _signInWithPhoneNumber ) . toHaveBeenCalledTimes ( 1 ) ;
403
+ // Verify the PhoneAuthProvider was created and verifyPhoneNumber was called
404
+ expect ( PhoneAuthProvider ) . toHaveBeenCalledWith ( mockUI . auth ) ;
405
+ expect ( mockVerifyPhoneNumber ) . toHaveBeenCalledWith ( phoneNumber , mockAppVerifier ) ;
406
+ expect ( mockVerifyPhoneNumber ) . toHaveBeenCalledTimes ( 1 ) ;
407
407
408
408
// Verify the result
409
- expect ( result ) . toEqual ( mockConfirmationResult ) ;
409
+ expect ( result ) . toEqual ( mockVerificationId ) ;
410
410
} ) ;
411
411
412
412
it ( "should call handleFirebaseError if an error is thrown" , async ( ) => {
413
413
const mockUI = createMockUI ( ) ;
414
414
const phoneNumber = "+1234567890" ;
415
- const mockRecaptchaVerifier = { } as any ;
415
+ const mockAppVerifier = { } as any ;
416
416
const error = new FirebaseError ( "auth/invalid-phone-number" , "Invalid phone number" ) ;
417
417
418
- vi . mocked ( _signInWithPhoneNumber ) . mockRejectedValue ( error ) ;
418
+ const mockVerifyPhoneNumber = vi . fn ( ) . mockRejectedValue ( error ) ;
419
+ vi . mocked ( PhoneAuthProvider ) . mockImplementation (
420
+ ( ) =>
421
+ ( {
422
+ verifyPhoneNumber : mockVerifyPhoneNumber ,
423
+ } ) as any
424
+ ) ;
419
425
420
- await signInWithPhoneNumber ( mockUI , phoneNumber , mockRecaptchaVerifier ) ;
426
+ await verifyPhoneNumber ( mockUI , phoneNumber , mockAppVerifier ) ;
421
427
422
428
// Verify error handling
423
429
expect ( handleFirebaseError ) . toHaveBeenCalledWith ( mockUI , error ) ;
@@ -429,12 +435,18 @@ describe("signInWithPhoneNumber", () => {
429
435
it ( "should handle recaptcha verification errors" , async ( ) => {
430
436
const mockUI = createMockUI ( ) ;
431
437
const phoneNumber = "+1234567890" ;
432
- const mockRecaptchaVerifier = { } as any ;
438
+ const mockAppVerifier = { } as any ;
433
439
const error = new Error ( "reCAPTCHA verification failed" ) ;
434
440
435
- vi . mocked ( _signInWithPhoneNumber ) . mockRejectedValue ( error ) ;
441
+ const mockVerifyPhoneNumber = vi . fn ( ) . mockRejectedValue ( error ) ;
442
+ vi . mocked ( PhoneAuthProvider ) . mockImplementation (
443
+ ( ) =>
444
+ ( {
445
+ verifyPhoneNumber : mockVerifyPhoneNumber ,
446
+ } ) as any
447
+ ) ;
436
448
437
- await signInWithPhoneNumber ( mockUI , phoneNumber , mockRecaptchaVerifier ) ;
449
+ await verifyPhoneNumber ( mockUI , phoneNumber , mockAppVerifier ) ;
438
450
439
451
// Verify error handling
440
452
expect ( handleFirebaseError ) . toHaveBeenCalledWith ( mockUI , error ) ;
@@ -453,15 +465,15 @@ describe("confirmPhoneNumber", () => {
453
465
const mockUI = createMockUI ( {
454
466
auth : { currentUser : null } as Auth ,
455
467
} ) ;
456
- const confirmationResult = { verificationId : "test-verification-id" } as ConfirmationResult ;
468
+ const verificationId = "test-verification-id" ;
457
469
const verificationCode = "123456" ;
458
470
459
- const credential = PhoneAuthProvider . credential ( confirmationResult . verificationId , verificationCode ) ;
471
+ const credential = PhoneAuthProvider . credential ( verificationId , verificationCode ) ;
460
472
vi . mocked ( hasBehavior ) . mockReturnValue ( false ) ;
461
473
vi . mocked ( PhoneAuthProvider . credential ) . mockReturnValue ( credential ) ;
462
474
vi . mocked ( _signInWithCredential ) . mockResolvedValue ( { providerId : "phone" } as UserCredential ) ;
463
475
464
- const result = await confirmPhoneNumber ( mockUI , confirmationResult , verificationCode ) ;
476
+ const result = await confirmPhoneNumber ( mockUI , verificationId , verificationCode ) ;
465
477
466
478
// Since currentUser is null, the behavior should not called.
467
479
expect ( hasBehavior ) . toHaveBeenCalledTimes ( 0 ) ;
@@ -480,16 +492,16 @@ describe("confirmPhoneNumber", () => {
480
492
const mockUI = createMockUI ( {
481
493
auth : { currentUser : { isAnonymous : true } } as Auth ,
482
494
} ) ;
483
- const confirmationResult = { verificationId : "test-verification-id" } as ConfirmationResult ;
495
+ const verificationId = "test-verification-id" ;
484
496
const verificationCode = "123456" ;
485
497
486
- const credential = PhoneAuthProvider . credential ( confirmationResult . verificationId , verificationCode ) ;
498
+ const credential = PhoneAuthProvider . credential ( verificationId , verificationCode ) ;
487
499
vi . mocked ( hasBehavior ) . mockReturnValue ( true ) ;
488
500
vi . mocked ( PhoneAuthProvider . credential ) . mockReturnValue ( credential ) ;
489
501
const mockBehavior = vi . fn ( ) . mockResolvedValue ( { providerId : "phone" } as UserCredential ) ;
490
502
vi . mocked ( getBehavior ) . mockReturnValue ( mockBehavior ) ;
491
503
492
- const result = await confirmPhoneNumber ( mockUI , confirmationResult , verificationCode ) ;
504
+ const result = await confirmPhoneNumber ( mockUI , verificationId , verificationCode ) ;
493
505
494
506
expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "autoUpgradeAnonymousCredential" ) ;
495
507
expect ( getBehavior ) . toHaveBeenCalledWith ( mockUI , "autoUpgradeAnonymousCredential" ) ;
@@ -505,14 +517,14 @@ describe("confirmPhoneNumber", () => {
505
517
const mockUI = createMockUI ( {
506
518
auth : { currentUser : { isAnonymous : false } } as Auth ,
507
519
} ) ;
508
- const confirmationResult = { verificationId : "test-verification-id" } as ConfirmationResult ;
520
+ const verificationId = "test-verification-id" ;
509
521
const verificationCode = "123456" ;
510
522
511
- const credential = PhoneAuthProvider . credential ( confirmationResult . verificationId , verificationCode ) ;
523
+ const credential = PhoneAuthProvider . credential ( verificationId , verificationCode ) ;
512
524
vi . mocked ( PhoneAuthProvider . credential ) . mockReturnValue ( credential ) ;
513
525
vi . mocked ( _signInWithCredential ) . mockResolvedValue ( { providerId : "phone" } as UserCredential ) ;
514
526
515
- const result = await confirmPhoneNumber ( mockUI , confirmationResult , verificationCode ) ;
527
+ const result = await confirmPhoneNumber ( mockUI , verificationId , verificationCode ) ;
516
528
517
529
// Behavior should not be called when user is not anonymous
518
530
expect ( hasBehavior ) . not . toHaveBeenCalled ( ) ;
@@ -527,14 +539,14 @@ describe("confirmPhoneNumber", () => {
527
539
const mockUI = createMockUI ( {
528
540
auth : { currentUser : null } as Auth ,
529
541
} ) ;
530
- const confirmationResult = { verificationId : "test-verification-id" } as ConfirmationResult ;
542
+ const verificationId = "test-verification-id" ;
531
543
const verificationCode = "123456" ;
532
544
533
- const credential = PhoneAuthProvider . credential ( confirmationResult . verificationId , verificationCode ) ;
545
+ const credential = PhoneAuthProvider . credential ( verificationId , verificationCode ) ;
534
546
vi . mocked ( PhoneAuthProvider . credential ) . mockReturnValue ( credential ) ;
535
547
vi . mocked ( _signInWithCredential ) . mockResolvedValue ( { providerId : "phone" } as UserCredential ) ;
536
548
537
- const result = await confirmPhoneNumber ( mockUI , confirmationResult , verificationCode ) ;
549
+ const result = await confirmPhoneNumber ( mockUI , verificationId , verificationCode ) ;
538
550
539
551
// Behavior should not be called when user is null
540
552
expect ( hasBehavior ) . not . toHaveBeenCalled ( ) ;
@@ -549,16 +561,16 @@ describe("confirmPhoneNumber", () => {
549
561
const mockUI = createMockUI ( {
550
562
auth : { currentUser : { isAnonymous : true } } as Auth ,
551
563
} ) ;
552
- const confirmationResult = { verificationId : "test-verification-id" } as ConfirmationResult ;
564
+ const verificationId = "test-verification-id" ;
553
565
const verificationCode = "123456" ;
554
566
555
- const credential = PhoneAuthProvider . credential ( confirmationResult . verificationId , verificationCode ) ;
567
+ const credential = PhoneAuthProvider . credential ( verificationId , verificationCode ) ;
556
568
vi . mocked ( hasBehavior ) . mockReturnValue ( true ) ;
557
569
vi . mocked ( PhoneAuthProvider . credential ) . mockReturnValue ( credential ) ;
558
570
const mockBehavior = vi . fn ( ) . mockResolvedValue ( undefined ) ;
559
571
vi . mocked ( getBehavior ) . mockReturnValue ( mockBehavior ) ;
560
572
561
- await confirmPhoneNumber ( mockUI , confirmationResult , verificationCode ) ;
573
+ await confirmPhoneNumber ( mockUI , verificationId , verificationCode ) ;
562
574
563
575
expect ( hasBehavior ) . toHaveBeenCalledWith ( mockUI , "autoUpgradeAnonymousCredential" ) ;
564
576
expect ( getBehavior ) . toHaveBeenCalledWith ( mockUI , "autoUpgradeAnonymousCredential" ) ;
@@ -576,14 +588,14 @@ describe("confirmPhoneNumber", () => {
576
588
const mockUI = createMockUI ( {
577
589
auth : { currentUser : null } as Auth ,
578
590
} ) ;
579
- const confirmationResult = { verificationId : "test-verification-id" } as ConfirmationResult ;
591
+ const verificationId = "test-verification-id" ;
580
592
const verificationCode = "123456" ;
581
593
582
594
const error = new FirebaseError ( "auth/invalid-verification-code" , "Invalid verification code" ) ;
583
595
584
596
vi . mocked ( _signInWithCredential ) . mockRejectedValue ( error ) ;
585
597
586
- await confirmPhoneNumber ( mockUI , confirmationResult , verificationCode ) ;
598
+ await confirmPhoneNumber ( mockUI , verificationId , verificationCode ) ;
587
599
588
600
expect ( handleFirebaseError ) . toHaveBeenCalledWith ( mockUI , error ) ;
589
601
expect ( vi . mocked ( mockUI . setState ) . mock . calls ) . toEqual ( [ [ "pending" ] , [ "idle" ] ] ) ;
0 commit comments