@@ -56,6 +56,8 @@ jest.mock('@libs/PolicyUtils', () => ({
5656
5757const fakePolicyID = 'ABCD1234' ;
5858const mockPolicy = { ...createRandomPolicy ( 0 ) , id : fakePolicyID } ;
59+ const fakeDomainAccountID = 4242 ;
60+ const mockDomain = { accountID : fakeDomainAccountID , validated : true , email : 'admin@example.com' } ;
5961const mockedGetPathFromState = getPathFromState as jest . MockedFunction < typeof getPathFromState > ;
6062/* eslint-disable @typescript-eslint/unbound-method -- jest.fn() mocks don't rely on `this` binding */
6163const mockedGetRootState = navigationRef . getRootState as unknown as jest . Mock < { routes : unknown [ ] } | undefined > ;
@@ -77,6 +79,15 @@ function setupOnyxForPolicy() {
7779 } ) ;
7880}
7981
82+ function setupOnyxForDomain ( ) {
83+ mockUseOnyx . mockImplementation ( ( key : unknown ) => {
84+ if ( key === ONYXKEYS . COLLECTION . DOMAIN ) {
85+ return [ { [ `${ ONYXKEYS . COLLECTION . DOMAIN } ${ fakeDomainAccountID } ` ] : mockDomain } ] ;
86+ }
87+ return [ undefined ] ;
88+ } ) ;
89+ }
90+
8091function buildStateWithUserOnDifferentTab ( workspaceRoutes : unknown [ ] ) {
8192 return {
8293 routes : [
@@ -312,4 +323,48 @@ describe('useRestoreWorkspacesTabOnNavigate', () => {
312323
313324 expect ( Navigation . navigate ) . toHaveBeenCalledWith ( restoredPath ) ;
314325 } ) ;
326+
327+ // Domain restore: when the last workspace-tab route is a DOMAIN_SPLIT_NAVIGATOR with a domainAccountID,
328+ // resolve the matching Domain via useOnyx(ONYXKEYS.COLLECTION.DOMAIN) and navigate to the domain page.
329+ it ( 'restores to the last visited domain when re-entering the Workspaces tab' , ( ) => {
330+ setupOnyxForDomain ( ) ;
331+ mockedGetRootState . mockReturnValue (
332+ buildStateWithUserOnDifferentTab ( [
333+ {
334+ name : NAVIGATORS . DOMAIN_SPLIT_NAVIGATOR ,
335+ state : { routes : [ { name : SCREENS . DOMAIN . INITIAL , params : { domainAccountID : fakeDomainAccountID } } ] } ,
336+ } ,
337+ ] ) ,
338+ ) ;
339+
340+ const { result} = renderHook ( ( ) => useRestoreWorkspacesTabOnNavigate ( ) ) ;
341+ result . current ( ) ;
342+
343+ expect ( Navigation . navigate ) . toHaveBeenCalledWith ( ROUTES . DOMAIN_INITIAL . getRoute ( fakeDomainAccountID ) ) ;
344+ } ) ;
345+
346+ // If the last route was a domain but it's no longer present in the Onyx domain collection
347+ // (e.g. user lost access), the lookup yields undefined and we fall back to the workspaces list.
348+ it ( 'falls back to the workspaces list when the last visited domain is missing from Onyx' , ( ) => {
349+ // Onyx returns no matching domain for the params.domainAccountID
350+ mockUseOnyx . mockImplementation ( ( key : unknown ) => {
351+ if ( key === ONYXKEYS . COLLECTION . DOMAIN ) {
352+ return [ { } ] ;
353+ }
354+ return [ undefined ] ;
355+ } ) ;
356+ mockedGetRootState . mockReturnValue (
357+ buildStateWithUserOnDifferentTab ( [
358+ {
359+ name : NAVIGATORS . DOMAIN_SPLIT_NAVIGATOR ,
360+ state : { routes : [ { name : SCREENS . DOMAIN . INITIAL , params : { domainAccountID : fakeDomainAccountID } } ] } ,
361+ } ,
362+ ] ) ,
363+ ) ;
364+
365+ const { result} = renderHook ( ( ) => useRestoreWorkspacesTabOnNavigate ( ) ) ;
366+ result . current ( ) ;
367+
368+ expect ( Navigation . navigate ) . toHaveBeenCalledWith ( ROUTES . WORKSPACES_LIST . route ) ;
369+ } ) ;
315370} ) ;
0 commit comments