@@ -2510,6 +2510,11 @@ describe('Clerk singleton', () => {
25102510 } ) ;
25112511
25122512 describe ( '__internal_attemptToEnableEnvironmentSetting' , ( ) => {
2513+ afterEach ( ( ) => {
2514+ mockEnvironmentFetch . mockReset ( ) ;
2515+ mockClientFetch . mockReset ( ) ;
2516+ } ) ;
2517+
25132518 describe ( 'for organizations' , ( ) => {
25142519 it ( 'does not open prompt if organizations is enabled in development' , async ( ) => {
25152520 mockEnvironmentFetch . mockReturnValue (
@@ -2630,6 +2635,68 @@ describe('Clerk singleton', () => {
26302635 expect ( result ?. isEnabled ) . toBe ( false ) ;
26312636 expect ( __internal_openEnableOrganizationsPromptSpy ) . not . toHaveBeenCalled ( ) ;
26322637 } ) ;
2638+
2639+ // Handles case where environment gets enabled via BAPI, but it gets cached and the user is redirected to the choose-organization task
2640+ // The enable org prompt should not appear in the task screen since orgs have already been enabled
2641+ it ( 'does not open prompt if organizations is disabled in development and session has choose-organization task' , async ( ) => {
2642+ const mockSession = {
2643+ id : '1' ,
2644+ remove : vi . fn ( ) ,
2645+ status : 'pending' ,
2646+ user : { } ,
2647+ touch : vi . fn ( ( ) => Promise . resolve ( ) ) ,
2648+ getToken : vi . fn ( ) ,
2649+ lastActiveToken : { getRawString : ( ) => 'mocked-token' } ,
2650+ tasks : [ { key : 'choose-organization' } ] ,
2651+ currentTask : { key : 'choose-organization' } ,
2652+ reload : vi . fn ( ( ) =>
2653+ Promise . resolve ( {
2654+ id : '1' ,
2655+ status : 'pending' ,
2656+ user : { } ,
2657+ tasks : [ { key : 'choose-organization' } ] ,
2658+ currentTask : {
2659+ key : 'choose-organization' ,
2660+ } ,
2661+ } ) ,
2662+ ) ,
2663+ } ;
2664+
2665+ mockEnvironmentFetch . mockReturnValue (
2666+ Promise . resolve ( {
2667+ userSettings : mockUserSettings ,
2668+ displayConfig : mockDisplayConfig ,
2669+ isSingleSession : ( ) => false ,
2670+ isProduction : ( ) => false ,
2671+ isDevelopmentOrStaging : ( ) => true ,
2672+ organizationSettings : {
2673+ enabled : false ,
2674+ } ,
2675+ } ) ,
2676+ ) ;
2677+
2678+ mockClientFetch . mockReturnValue (
2679+ Promise . resolve ( {
2680+ signedInSessions : [ mockSession ] ,
2681+ } ) ,
2682+ ) ;
2683+
2684+ const sut = new Clerk ( developmentPublishableKey ) ;
2685+
2686+ const __internal_openEnableOrganizationsPromptSpy = vi . fn ( ) ;
2687+ sut . __internal_openEnableOrganizationsPrompt = __internal_openEnableOrganizationsPromptSpy ;
2688+
2689+ await sut . load ( ) ;
2690+
2691+ const result = await sut . __internal_attemptToEnableEnvironmentSetting ( {
2692+ for : 'organizations' ,
2693+ caller : 'OrganizationSwitcher' ,
2694+ } ) ;
2695+
2696+ // Contains the organization task, so the prompt should not be opened
2697+ expect ( result ?. isEnabled ) . toBe ( true ) ;
2698+ expect ( __internal_openEnableOrganizationsPromptSpy ) . not . toHaveBeenCalled ( ) ;
2699+ } ) ;
26332700 } ) ;
26342701 } ) ;
26352702} ) ;
0 commit comments