@@ -537,6 +537,87 @@ describe('Clerk singleton', () => {
537
537
} ) ;
538
538
} ) ;
539
539
} ) ;
540
+
541
+ describe ( 'with force organization selection enabled' , ( ) => {
542
+ const mockSession = {
543
+ id : '1' ,
544
+ remove : jest . fn ( ) ,
545
+ status : 'active' ,
546
+ user : { } ,
547
+ touch : jest . fn ( ( ) => Promise . resolve ( ) ) ,
548
+ getToken : jest . fn ( ) ,
549
+ lastActiveToken : { getRawString : ( ) => 'mocked-token' } ,
550
+ } ;
551
+
552
+ beforeEach ( ( ) => {
553
+ mockEnvironmentFetch . mockReturnValue (
554
+ Promise . resolve ( {
555
+ userSettings : mockUserSettings ,
556
+ displayConfig : mockDisplayConfig ,
557
+ isSingleSession : ( ) => false ,
558
+ isProduction : ( ) => false ,
559
+ isDevelopmentOrStaging : ( ) => true ,
560
+ organizationSettings : {
561
+ forceOrganizationSelection : true ,
562
+ } ,
563
+ } ) ,
564
+ ) ;
565
+ } ) ;
566
+
567
+ afterEach ( ( ) => {
568
+ mockSession . remove . mockReset ( ) ;
569
+ mockSession . touch . mockReset ( ) ;
570
+ mockEnvironmentFetch . mockReset ( ) ;
571
+
572
+ // cleanup global window pollution
573
+ ( window as any ) . __unstable__onBeforeSetActive = null ;
574
+ ( window as any ) . __unstable__onAfterSetActive = null ;
575
+ } ) ;
576
+
577
+ it ( 'does not update session to personal workspace' , async ( ) => {
578
+ const mockSessionWithOrganization = {
579
+ id : '1' ,
580
+ status : 'active' ,
581
+ user : {
582
+ organizationMemberships : [
583
+ {
584
+ id : 'orgmem_id' ,
585
+ organization : {
586
+ id : 'org_id' ,
587
+ slug : 'some-org-slug' ,
588
+ } ,
589
+ } ,
590
+ ] ,
591
+ } ,
592
+ touch : jest . fn ( ) ,
593
+ getToken : jest . fn ( ) ,
594
+ } ;
595
+
596
+ mockClientFetch . mockReturnValue ( Promise . resolve ( { signedInSessions : [ mockSessionWithOrganization ] } ) ) ;
597
+ const sut = new Clerk ( productionPublishableKey ) ;
598
+ await sut . load ( ) ;
599
+
600
+ mockSessionWithOrganization . touch . mockImplementationOnce ( ( ) => {
601
+ sut . session = mockSessionWithOrganization as any ;
602
+ return Promise . resolve ( ) ;
603
+ } ) ;
604
+ mockSessionWithOrganization . getToken . mockImplementation ( ( ) => 'mocked-token' ) ;
605
+
606
+ await sut . setActive ( { organization : 'some-org-slug' } ) ;
607
+
608
+ await waitFor ( ( ) => {
609
+ expect ( mockSessionWithOrganization . touch ) . toHaveBeenCalled ( ) ;
610
+ expect ( mockSessionWithOrganization . getToken ) . toHaveBeenCalled ( ) ;
611
+ expect ( ( mockSessionWithOrganization as any as ActiveSessionResource ) ?. lastActiveOrganizationId ) . toEqual (
612
+ 'org_id' ,
613
+ ) ;
614
+ expect ( sut . session ) . toMatchObject ( mockSessionWithOrganization ) ;
615
+ } ) ;
616
+
617
+ await sut . setActive ( { organization : null } ) ;
618
+ expect ( sut . session ) . toMatchObject ( mockSessionWithOrganization ) ;
619
+ } ) ;
620
+ } ) ;
540
621
} ) ;
541
622
542
623
describe ( '.load()' , ( ) => {
0 commit comments