@@ -11,7 +11,16 @@ import {
1111} from '../nativeClientSyncCoordinator' ;
1212
1313const mocks = vi . hoisted ( ( ) => {
14+ const appStateListeners = new Set < ( state : string ) => void > ( ) ;
1415 return {
16+ appState : {
17+ currentState : 'active' as string ,
18+ addEventListener : ( _type : 'change' , listener : ( state : string ) => void ) => {
19+ appStateListeners . add ( listener ) ;
20+ return { remove : ( ) => appStateListeners . delete ( listener ) } ;
21+ } ,
22+ } ,
23+ appStateListeners,
1524 configure : vi . fn ( ) ,
1625 getClientToken : vi . fn ( ) ,
1726 nativeClientEvent : null as unknown ,
@@ -62,6 +71,7 @@ vi.mock('@clerk/react/internal', () => {
6271
6372vi . mock ( 'react-native' , ( ) => {
6473 return {
74+ AppState : mocks . appState ,
6575 NativeModules : {
6676 BlobModule : { } ,
6777 } ,
@@ -106,8 +116,10 @@ vi.mock('../../specs/NativeClerkModule', () => {
106116 } ;
107117} ) ;
108118
109- vi . mock ( '../../utils/runtime' , ( ) => {
119+ vi . mock ( '../../utils/runtime' , async ( ) => {
120+ const runtime = await vi . importActual < typeof import ( '../../utils/runtime' ) > ( '../../utils/runtime' ) ;
110121 return {
122+ ...runtime ,
111123 isNative : ( ) => true ,
112124 isWeb : ( ) => false ,
113125 } ;
@@ -122,6 +134,18 @@ vi.mock('../singleton', () => {
122134 } ;
123135} ) ;
124136
137+ const setAppState = ( state : string ) => {
138+ mocks . appState . currentState = state ;
139+ for ( const listener of [ ...mocks . appStateListeners ] ) {
140+ listener ( state ) ;
141+ }
142+ } ;
143+
144+ const rejectUnlessAppIsActive = ( token : string ) => ( ) =>
145+ mocks . appState . currentState === 'active'
146+ ? Promise . resolve ( token )
147+ : Promise . reject ( new Error ( 'User interaction is not allowed.' ) ) ;
148+
125149function deferred ( ) : { promise : Promise < void > ; resolve : ( ) => void } {
126150 let resolve ! : ( ) => void ;
127151 const promise = new Promise < void > ( innerResolve => {
@@ -142,6 +166,8 @@ describe('ClerkProvider native client sync', () => {
142166 beforeEach ( ( ) => {
143167 __internal_resetNativeClientSyncCoordinator ( ) ;
144168 vi . clearAllMocks ( ) ;
169+ mocks . appState . currentState = 'active' ;
170+ mocks . appStateListeners . clear ( ) ;
145171 mocks . nativeClientEvent = null ;
146172 mocks . configure . mockResolvedValue ( undefined ) ;
147173 mocks . getClientToken . mockResolvedValue ( null ) ;
@@ -531,6 +557,97 @@ describe('ClerkProvider native client sync', () => {
531557 consoleError . mockRestore ( ) ;
532558 } ) ;
533559
560+ test ( 'finishes bootstrap with the cached token once the app is active when the token cache cannot be read in the background' , async ( ) => {
561+ const consoleError = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => undefined ) ;
562+ setAppState ( 'background' ) ;
563+ mocks . tokenCache . getToken . mockImplementation ( rejectUnlessAppIsActive ( 'client-token' ) ) ;
564+ mocks . getClientToken . mockResolvedValue ( 'client-token' ) ;
565+
566+ const { rerender } = render (
567+ < ClerkProvider
568+ publishableKey = 'pk_test_123'
569+ tokenCache = { mocks . tokenCache }
570+ /> ,
571+ ) ;
572+
573+ await act ( async ( ) => {
574+ await new Promise ( resolve => setTimeout ( resolve , 300 ) ) ;
575+ } ) ;
576+ expect ( mocks . configure ) . not . toHaveBeenCalled ( ) ;
577+
578+ act ( ( ) => {
579+ setAppState ( 'active' ) ;
580+ } ) ;
581+
582+ await waitFor ( ( ) => expect ( mocks . configure ) . toHaveBeenCalledWith ( 'pk_test_123' , 'client-token' ) ) ;
583+ expect ( mocks . configure ) . toHaveBeenCalledTimes ( 1 ) ;
584+ expect ( mocks . syncClientStateFromJs ) . not . toHaveBeenCalled ( ) ;
585+ expect ( mocks . tokenCache . clearToken ) . not . toHaveBeenCalled ( ) ;
586+
587+ mocks . nativeClientEvent = {
588+ issuedAt : 1 ,
589+ changed : { client : true , deviceToken : true } ,
590+ deviceToken : 'native-client-token' ,
591+ } ;
592+ rerender (
593+ < ClerkProvider
594+ publishableKey = 'pk_test_123'
595+ tokenCache = { mocks . tokenCache }
596+ /> ,
597+ ) ;
598+
599+ await waitFor ( ( ) =>
600+ expect ( mocks . tokenCache . saveToken ) . toHaveBeenCalledWith ( CLERK_CLIENT_JWT_KEY , 'native-client-token' ) ,
601+ ) ;
602+ consoleError . mockRestore ( ) ;
603+ } ) ;
604+
605+ test ( 'applies a native client change once the app is active when the token cache cannot be read in the background' , async ( ) => {
606+ const consoleError = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => undefined ) ;
607+ mocks . tokenCache . getToken . mockImplementation ( rejectUnlessAppIsActive ( 'client-token' ) ) ;
608+ mocks . getClientToken . mockResolvedValue ( 'client-token' ) ;
609+
610+ const { rerender } = render (
611+ < ClerkProvider
612+ publishableKey = 'pk_test_123'
613+ tokenCache = { mocks . tokenCache }
614+ /> ,
615+ ) ;
616+
617+ await waitFor ( ( ) => expect ( mocks . configure ) . toHaveBeenCalledWith ( 'pk_test_123' , 'client-token' ) ) ;
618+ await act ( async ( ) => {
619+ await Promise . resolve ( ) ;
620+ } ) ;
621+
622+ setAppState ( 'background' ) ;
623+ mocks . nativeClientEvent = {
624+ issuedAt : 1 ,
625+ changed : { client : true , deviceToken : true } ,
626+ deviceToken : 'native-client-token' ,
627+ } ;
628+ rerender (
629+ < ClerkProvider
630+ publishableKey = 'pk_test_123'
631+ tokenCache = { mocks . tokenCache }
632+ /> ,
633+ ) ;
634+
635+ await act ( async ( ) => {
636+ await new Promise ( resolve => setTimeout ( resolve , 50 ) ) ;
637+ } ) ;
638+ expect ( mocks . tokenCache . saveToken ) . not . toHaveBeenCalled ( ) ;
639+
640+ act ( ( ) => {
641+ setAppState ( 'active' ) ;
642+ } ) ;
643+
644+ await waitFor ( ( ) =>
645+ expect ( mocks . tokenCache . saveToken ) . toHaveBeenCalledWith ( CLERK_CLIENT_JWT_KEY , 'native-client-token' ) ,
646+ ) ;
647+ expect ( mocks . clerkInstance . __internal_reloadInitialResources ) . toHaveBeenCalled ( ) ;
648+ consoleError . mockRestore ( ) ;
649+ } ) ;
650+
534651 test ( 'disables synchronization when switching publishable keys fails to configure native' , async ( ) => {
535652 const consoleError = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => undefined ) ;
536653 const configureError = new Error ( 'native key switch failed' ) ;
0 commit comments