@@ -474,7 +474,6 @@ describe('Clerk singleton', () => {
474
474
} ) ;
475
475
} ) ;
476
476
477
- // TODO -> Refactor tests
478
477
describe ( 'with `pending` session status' , ( ) => {
479
478
const mockSession = {
480
479
id : '1' ,
@@ -521,12 +520,53 @@ describe('Clerk singleton', () => {
521
520
522
521
const sut = new Clerk ( productionPublishableKey ) ;
523
522
await sut . load ( ) ;
524
- await sut . setActive ( { session : mockSession as any as ActiveSessionResource } ) ;
523
+ await sut . setActive ( { session : mockSession as any as PendingSessionResource } ) ;
524
+ expect ( mockSession . touch ) . toHaveBeenCalled ( ) ;
525
+ } ) ;
526
+
527
+ it ( 'calls `onPendingSession` from clerk options' , async ( ) => {
528
+ mockSession . touch . mockReturnValue ( Promise . resolve ( ) ) ;
529
+ mockClientFetch . mockReturnValue ( Promise . resolve ( { signedInSessions : [ mockSession ] } ) ) ;
530
+ const onPendingSession = jest . fn ( ) ;
531
+
532
+ const sut = new Clerk ( productionPublishableKey ) ;
533
+ await sut . load ( {
534
+ onPendingSession,
535
+ } ) ;
536
+ await sut . setActive ( { session : mockSession as any as PendingSessionResource } ) ;
537
+ expect ( mockSession . touch ) . toHaveBeenCalled ( ) ;
538
+ expect ( onPendingSession ) . toHaveBeenCalled ( ) ;
539
+ } ) ;
540
+
541
+ it ( 'calls `onPendingSession` as argument' , async ( ) => {
542
+ mockSession . touch . mockReturnValue ( Promise . resolve ( ) ) ;
543
+ mockClientFetch . mockReturnValue ( Promise . resolve ( { signedInSessions : [ mockSession ] } ) ) ;
544
+ const onPendingSession = jest . fn ( ) ;
545
+
546
+ const sut = new Clerk ( productionPublishableKey ) ;
547
+ await sut . load ( ) ;
548
+ await sut . setActive ( { session : mockSession as any as PendingSessionResource , onPendingSession } ) ;
549
+ expect ( mockSession . touch ) . toHaveBeenCalled ( ) ;
550
+ expect ( onPendingSession ) . toHaveBeenCalled ( ) ;
551
+ } ) ;
552
+
553
+ it ( 'if `onPendingSession` is not defined, navigate to `taskUrl` option' , async ( ) => {
554
+ mockSession . touch . mockReturnValue ( Promise . resolve ( ) ) ;
555
+ mockClientFetch . mockReturnValue ( Promise . resolve ( { signedInSessions : [ mockSession ] } ) ) ;
556
+
557
+ const sut = new Clerk ( productionPublishableKey ) ;
558
+ sut . navigate = jest . fn ( ) ;
559
+ await sut . load ( {
560
+ taskUrls : {
561
+ 'choose-organization' : '/choose-organization' ,
562
+ } ,
563
+ } ) ;
564
+ await sut . setActive ( { session : mockSession as any as PendingSessionResource } ) ;
525
565
expect ( mockSession . touch ) . toHaveBeenCalled ( ) ;
566
+ expect ( sut . navigate ) . toHaveBeenCalledWith ( '/choose-organization' ) ;
526
567
} ) ;
527
568
} ) ;
528
569
529
- // TODO -> Refactor tests
530
570
describe ( 'with force organization selection enabled' , ( ) => {
531
571
const mockSession = {
532
572
id : '1' ,
@@ -909,7 +949,7 @@ describe('Clerk singleton', () => {
909
949
mockEnvironmentFetch . mockReset ( ) ;
910
950
} ) ;
911
951
912
- describe ( 'with after-auth flows ' , ( ) => {
952
+ describe ( 'with pending session ' , ( ) => {
913
953
beforeEach ( ( ) => {
914
954
mockClientFetch . mockReset ( ) ;
915
955
mockEnvironmentFetch . mockReturnValue (
@@ -926,7 +966,7 @@ describe('Clerk singleton', () => {
926
966
) ;
927
967
} ) ;
928
968
929
- it ( 'redirects to pending task' , async ( ) => {
969
+ it ( 'navigates to task' , async ( ) => {
930
970
const mockSession = {
931
971
id : '1' ,
932
972
status : 'pending' ,
@@ -956,7 +996,6 @@ describe('Clerk singleton', () => {
956
996
} ) ,
957
997
) ;
958
998
959
- const mockSetActive = jest . fn ( ) ;
960
999
const mockSignUpCreate = jest
961
1000
. fn ( )
962
1001
. mockReturnValue ( Promise . resolve ( { status : 'complete' , createdSessionId : '123' } ) ) ;
@@ -967,60 +1006,13 @@ describe('Clerk singleton', () => {
967
1006
fail ( 'we should always have a client' ) ;
968
1007
}
969
1008
sut . client . signUp . create = mockSignUpCreate ;
970
- sut . setActive = mockSetActive ;
971
1009
972
1010
await sut . handleRedirectCallback ( ) ;
973
1011
974
1012
await waitFor ( ( ) => {
975
1013
expect ( mockNavigate . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/sign-in#/tasks/choose-organization' ) ;
976
1014
} ) ;
977
1015
} ) ;
978
-
979
- it ( 'redirects to after sign-in URL when task has been resolved' , async ( ) => {
980
- const mockSession = {
981
- id : '1' ,
982
- status : 'active' ,
983
- user : { } ,
984
- lastActiveToken : { getRawString : ( ) => 'mocked-token' } ,
985
- } ;
986
-
987
- const mockResource = {
988
- ...mockSession ,
989
- remove : jest . fn ( ) ,
990
- touch : jest . fn ( ( ) => Promise . resolve ( ) ) ,
991
- getToken : jest . fn ( ) ,
992
- reload : jest . fn ( ( ) => Promise . resolve ( mockSession ) ) ,
993
- } ;
994
-
995
- mockResource . touch . mockReturnValueOnce ( Promise . resolve ( ) ) ;
996
- mockClientFetch . mockReturnValue (
997
- Promise . resolve ( {
998
- signedInSessions : [ mockResource ] ,
999
- signIn : new SignIn ( null ) ,
1000
- signUp : new SignUp ( null ) ,
1001
- isEligibleForTouch : ( ) => false ,
1002
- } ) ,
1003
- ) ;
1004
-
1005
- const mockSetActive = jest . fn ( ) ;
1006
- const mockSignUpCreate = jest
1007
- . fn ( )
1008
- . mockReturnValue ( Promise . resolve ( { status : 'complete' , createdSessionId : '123' } ) ) ;
1009
-
1010
- const sut = new Clerk ( productionPublishableKey ) ;
1011
- await sut . load ( mockedLoadOptions ) ;
1012
- if ( ! sut . client ) {
1013
- fail ( 'we should always have a client' ) ;
1014
- }
1015
- sut . client . signUp . create = mockSignUpCreate ;
1016
- sut . setActive = mockSetActive ;
1017
-
1018
- await sut . handleRedirectCallback ( ) ;
1019
-
1020
- await waitFor ( ( ) => {
1021
- expect ( mockNavigate . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/' ) ;
1022
- } ) ;
1023
- } ) ;
1024
1016
} ) ;
1025
1017
1026
1018
it ( 'creates a new user and calls setActive if the user was not found during sso signup' , async ( ) => {
@@ -2434,115 +2426,6 @@ describe('Clerk singleton', () => {
2434
2426
} ) ;
2435
2427
} ) ;
2436
2428
2437
- describe ( 'navigateToTask' , ( ) => {
2438
- describe ( 'with `pending` session status' , ( ) => {
2439
- const mockSession = {
2440
- id : '1' ,
2441
- status : 'pending' ,
2442
- user : { } ,
2443
- tasks : [ { key : 'choose-organization' } ] ,
2444
- currentTask : { key : 'choose-organization' , __internal_getUrl : ( ) => 'https://sut/tasks/choose-organization' } ,
2445
- lastActiveToken : { getRawString : ( ) => 'mocked-token' } ,
2446
- } ;
2447
-
2448
- const mockResource = {
2449
- ...mockSession ,
2450
- remove : jest . fn ( ) ,
2451
- touch : jest . fn ( ( ) => Promise . resolve ( ) ) ,
2452
- getToken : jest . fn ( ) ,
2453
- reload : jest . fn ( ( ) => Promise . resolve ( mockSession ) ) ,
2454
- } ;
2455
-
2456
- beforeEach ( ( ) => {
2457
- mockResource . touch . mockReturnValueOnce ( Promise . resolve ( ) ) ;
2458
- mockClientFetch . mockReturnValue (
2459
- Promise . resolve ( {
2460
- signedInSessions : [ mockResource ] ,
2461
- isEligibleForTouch : ( ) => false ,
2462
- } ) ,
2463
- ) ;
2464
- } ) ;
2465
-
2466
- afterEach ( ( ) => {
2467
- mockResource . remove . mockReset ( ) ;
2468
- mockResource . touch . mockReset ( ) ;
2469
- } ) ;
2470
-
2471
- it ( 'navigates to next task with default internal routing for AIOs' , async ( ) => {
2472
- const sut = new Clerk ( productionPublishableKey ) ;
2473
- await sut . load ( mockedLoadOptions ) ;
2474
-
2475
- await sut . setActive ( { session : mockResource as any as PendingSessionResource } ) ;
2476
-
2477
- expect ( mockNavigate . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/sign-in#/tasks/choose-organization' ) ;
2478
- } ) ;
2479
-
2480
- it ( 'navigates to next task with custom routing from clerk options' , async ( ) => {
2481
- const sut = new Clerk ( productionPublishableKey ) ;
2482
- await sut . load ( {
2483
- ...mockedLoadOptions ,
2484
- taskUrls : {
2485
- 'choose-organization' : '/onboarding/choose-organization' ,
2486
- } ,
2487
- } ) ;
2488
-
2489
- await sut . setActive ( { session : mockResource as any as PendingSessionResource } ) ;
2490
-
2491
- expect ( mockNavigate . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/onboarding/choose-organization' ) ;
2492
- } ) ;
2493
- } ) ;
2494
-
2495
- describe ( 'with `active` session status' , ( ) => {
2496
- const mockSession = {
2497
- id : '1' ,
2498
- remove : jest . fn ( ) ,
2499
- status : 'active' ,
2500
- user : { } ,
2501
- touch : jest . fn ( ( ) => Promise . resolve ( ) ) ,
2502
- getToken : jest . fn ( ) ,
2503
- lastActiveToken : { getRawString : ( ) => 'mocked-token' } ,
2504
- reload : jest . fn ( ( ) =>
2505
- Promise . resolve ( {
2506
- id : '1' ,
2507
- remove : jest . fn ( ) ,
2508
- status : 'active' ,
2509
- user : { } ,
2510
- touch : jest . fn ( ( ) => Promise . resolve ( ) ) ,
2511
- getToken : jest . fn ( ) ,
2512
- lastActiveToken : { getRawString : ( ) => 'mocked-token' } ,
2513
- } ) ,
2514
- ) ,
2515
- } ;
2516
-
2517
- afterEach ( ( ) => {
2518
- mockSession . remove . mockReset ( ) ;
2519
- mockSession . touch . mockReset ( ) ;
2520
- ( window as any ) . __unstable__onBeforeSetActive = null ;
2521
- ( window as any ) . __unstable__onAfterSetActive = null ;
2522
- } ) ;
2523
-
2524
- it ( 'navigates to redirect url on completion' , async ( ) => {
2525
- mockSession . touch . mockReturnValue ( Promise . resolve ( ) ) ;
2526
- mockClientFetch . mockReturnValue (
2527
- Promise . resolve ( {
2528
- signedInSessions : [ mockSession ] ,
2529
- isEligibleForTouch : ( ) => false ,
2530
- } ) ,
2531
- ) ;
2532
-
2533
- const sut = new Clerk ( productionPublishableKey ) ;
2534
- await sut . load ( mockedLoadOptions ) ;
2535
- await sut . setActive ( { session : mockSession as any as ActiveSessionResource } ) ;
2536
-
2537
- const redirectUrlComplete = '/welcome-to-app' ;
2538
-
2539
- console . log ( mockNavigate . mock . calls ) ;
2540
-
2541
- expect ( mockNavigate . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/welcome-to-app' ) ;
2542
- } ) ;
2543
- } ) ;
2544
- } ) ;
2545
-
2546
2429
describe ( 'updateClient' , ( ) => {
2547
2430
afterEach ( ( ) => {
2548
2431
// cleanup global window pollution
0 commit comments