Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useSearchTypeMenuSections from '@hooks/useSearchTypeMenuSections';
import useThemeStyles from '@hooks/useThemeStyles';

import navigateToDomainRouteWithSidebarSync from '@libs/Navigation/helpers/navigateToDomainRouteWithSidebarSync';
import navigateToWorkspaceSettingsRoute from '@libs/Navigation/helpers/navigateToWorkspaceSettingsRoute';
import Navigation from '@libs/Navigation/Navigation';
import {shouldShowPolicy} from '@libs/PolicyUtils';
Expand Down Expand Up @@ -182,7 +183,7 @@ type BuildDomainNavigationItemsParams = {
getItemText: (translationKey: TranslationPaths) => string;
getDestinationText: (destination: string) => string;
getDomainContext: (domainName: string) => ReactNode;
onSelect: (route: Route) => void;
onSelect: (route: Route, domainAccountID: number) => void;
};

type BuildAccountNavigationItemsParams = {
Expand Down Expand Up @@ -332,7 +333,7 @@ function buildDomainNavigationItems({
return {
text: getDestinationText(itemText),
singleIcon: item.icon,
action: () => onSelect(item.route),
action: () => onSelect(item.route, domain.accountID),
keyForList: `domain_${domain.accountID}_${item.screenName}`,
rightElement: domainContext,
matchTerms: [itemText, domainName],
Expand Down Expand Up @@ -441,7 +442,7 @@ function useNavigationSuggestions(query: string, shouldWatchForApprovals = true)
textStyle={[styles.textLabelSupporting, styles.label]}
/>
),
onSelect: (route) => Navigation.navigate(route),
onSelect: (route, domainAccountID) => navigateToDomainRouteWithSidebarSync(route, domainAccountID, shouldUseNarrowLayout),
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
});

const accountItems = buildAccountNavigationItems({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* React Navigation reuses the existing Domain split navigator when Search Router navigation crosses Domains, so its persistent sidebar keeps the previous Domain ID.
* Synchronize that ID before navigating to keep the sidebar and destination page consistent.
*/
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
import {SPLIT_TO_SIDEBAR} from '@libs/Navigation/linkingConfig/RELATIONS';
import Navigation from '@libs/Navigation/Navigation';
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
import navigationRef from '@libs/Navigation/navigationRef';

import NAVIGATORS from '@src/NAVIGATORS';
import type {Route} from '@src/ROUTES';

import {getTabState} from './tabNavigatorUtils';

type ActiveDomainSidebarRoute = {
sidebarRouteKey: string;
splitStateKey?: string;
domainAccountID?: number;
};

function getDomainAccountIDParam(params: unknown): number | undefined {
if (params && typeof params === 'object' && 'domainAccountID' in params && typeof params.domainAccountID === 'number') {
return params.domainAccountID;
}

return undefined;
}

function getActiveDomainSidebarRoute(): ActiveDomainSidebarRoute | undefined {
if (!navigationRef.isReady()) {
return undefined;
}
Comment on lines +29 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!navigationRef.isReady() early return

Every test stubs isReady.mockReturnValue(true), so the not-ready guard is never taken.

Add a test "navigates directly when navigationRef is not ready": isReady.mockReturnValue(false), call with shouldUseNarrowLayout = false. Assert Navigation.setParams is not called, Navigation.navigate is called with the target route, and getRootState is never read.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.


const routes = navigationRef.getRootState().routes;
const tabNavigatorRoute = routes.findLast((route) => route.name === NAVIGATORS.TAB_NAVIGATOR);
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
const workspaceNavigatorRoute = getTabState(tabNavigatorRoute)?.routes.find((route) => route.name === NAVIGATORS.WORKSPACE_NAVIGATOR);
// Wide layouts nest the Domain split under the workspace navigator; fall back to the root for the standalone case.
const domainSplitRoute =
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
workspaceNavigatorRoute?.state?.routes.findLast((route) => route.name === NAVIGATORS.DOMAIN_SPLIT_NAVIGATOR) ??
routes.findLast((route) => route.name === NAVIGATORS.DOMAIN_SPLIT_NAVIGATOR);
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
if (!domainSplitRoute) {
return undefined;
}

const sidebarRoute = domainSplitRoute.state?.routes.find((route) => route.name === SPLIT_TO_SIDEBAR[NAVIGATORS.DOMAIN_SPLIT_NAVIGATOR]);
if (!sidebarRoute?.key) {
return undefined;
}

const domainAccountID = getDomainAccountIDParam(sidebarRoute.params);
return {
sidebarRouteKey: sidebarRoute.key,
splitStateKey: domainSplitRoute.state?.key,
domainAccountID,
};
}

function navigateToDomainRouteWithSidebarSync(targetRoute: Route, domainAccountID: number, shouldUseNarrowLayout: boolean) {
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
if (shouldUseNarrowLayout) {
Navigation.navigate(targetRoute);
return;
}

const domainSidebarRoute = getActiveDomainSidebarRoute();
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
if (domainSidebarRoute && domainSidebarRoute.domainAccountID !== domainAccountID) {
Navigation.setParams({domainAccountID}, domainSidebarRoute.sidebarRouteKey, domainSidebarRoute.splitStateKey);
}
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved

Navigation.navigate(targetRoute);
}

export default navigateToDomainRouteWithSidebarSync;
20 changes: 15 additions & 5 deletions tests/unit/SearchRouterNavigationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import useNavigationSuggestions, {
} from '@components/Search/SearchRouter/useNavigationSuggestions';

import {setSearchContext} from '@libs/actions/Search';
import navigateToDomainRouteWithSidebarSync from '@libs/Navigation/helpers/navigateToDomainRouteWithSidebarSync';
import navigateToWorkspaceSettingsRoute from '@libs/Navigation/helpers/navigateToWorkspaceSettingsRoute';
import Navigation from '@libs/Navigation/Navigation';
import navigateToCannedSpendSearch from '@libs/SearchNavigationUtils';
Expand Down Expand Up @@ -55,6 +56,7 @@ const mockUseCreateNavigationSuggestions = jest.fn<NavigationSuggestionSourceIte
const mockUseSettingsNavigationMenuData = jest.fn<{accountMenuItemsData: MenuSection; generalMenuItemsData: MenuSection}, []>();
const mockClearSelectedTransactions = jest.fn();
const mockUseOnyx = jest.fn<[unknown], [key: string]>(() => [undefined]);
const mockShouldUseNarrowLayout = jest.fn(() => false);
const mockUseNetwork = jest.fn<{isOffline: boolean}, []>(() => ({isOffline: false}));
const mockIsBetaEnabled = jest.fn<boolean, [beta: string]>(() => false);
const currentUserAccountID = 1;
Expand Down Expand Up @@ -131,7 +133,7 @@ jest.mock('@hooks/usePermissions', () => ({

jest.mock('@hooks/useResponsiveLayout', () => ({
__esModule: true,
default: () => ({shouldUseNarrowLayout: false}),
default: () => ({shouldUseNarrowLayout: mockShouldUseNarrowLayout()}),
}));

jest.mock('@hooks/useSearchTypeMenuSections', () => ({
Expand All @@ -153,6 +155,11 @@ jest.mock('@libs/actions/Search', () => ({
setSearchContext: jest.fn(),
}));

jest.mock('@libs/Navigation/helpers/navigateToDomainRouteWithSidebarSync', () => ({
__esModule: true,
default: jest.fn(),
}));

jest.mock('@libs/Navigation/Navigation', () => ({
__esModule: true,
default: {
Expand Down Expand Up @@ -498,10 +505,10 @@ describe('Domain Search Router navigation source', () => {
for (const item of items) {
item.action?.();
}
expect(onSelect).toHaveBeenNthCalledWith(1, ROUTES.DOMAIN_MEMBERS.getRoute(123));
expect(onSelect).toHaveBeenNthCalledWith(2, ROUTES.DOMAIN_ADMINS.getRoute(123));
expect(onSelect).toHaveBeenNthCalledWith(3, ROUTES.DOMAIN_GROUPS.getRoute(123));
expect(onSelect).toHaveBeenNthCalledWith(4, ROUTES.DOMAIN_SAML.getRoute(123));
expect(onSelect).toHaveBeenNthCalledWith(1, ROUTES.DOMAIN_MEMBERS.getRoute(123), 123);
expect(onSelect).toHaveBeenNthCalledWith(2, ROUTES.DOMAIN_ADMINS.getRoute(123), 123);
expect(onSelect).toHaveBeenNthCalledWith(3, ROUTES.DOMAIN_GROUPS.getRoute(123), 123);
expect(onSelect).toHaveBeenNthCalledWith(4, ROUTES.DOMAIN_SAML.getRoute(123), 123);
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
});

it('matches Domain rows by subpage label or domain name', () => {
Expand Down Expand Up @@ -561,6 +568,9 @@ describe('Domain Search Router navigation source', () => {
throw new Error('Expected Domain navigation context to be a React element');
}
expect(rightElement.props).toMatchObject({text: 'example.com', icon: domainIcons.Globe});

result.current.at(0)?.action?.();
expect(navigateToDomainRouteWithSidebarSync).toHaveBeenCalledWith(ROUTES.DOMAIN_MEMBERS.getRoute(123), 123, false);
});
});

Expand Down
180 changes: 180 additions & 0 deletions tests/unit/navigateToDomainRouteWithSidebarSyncTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import navigateToDomainRouteWithSidebarSync from '@libs/Navigation/helpers/navigateToDomainRouteWithSidebarSync';
import Navigation from '@libs/Navigation/Navigation';
import navigationRef from '@libs/Navigation/navigationRef';

import NAVIGATORS from '@src/NAVIGATORS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';

import createMock from '../utils/createMock';

// eslint-disable-next-line @typescript-eslint/unbound-method -- jest.fn() mock doesn't rely on `this` binding
const mockGetRootState = jest.mocked(navigationRef.getRootState);

jest.mock('@libs/Navigation/Navigation', () => ({
__esModule: true,
default: {
navigate: jest.fn(),
setParams: jest.fn(),
},
}));

jest.mock('@libs/Navigation/navigationRef', () => ({
__esModule: true,
default: {
isReady: jest.fn(),
getRootState: jest.fn(),
},
}));

function mockDomainNavigationState(domainAccountID?: number) {
jest.mocked(navigationRef).isReady.mockReturnValue(true);
jest.mocked(navigationRef).getRootState.mockReturnValue(
createMock<ReturnType<typeof navigationRef.getRootState>>({
routes: [
{
key: 'tab-navigator',
name: NAVIGATORS.TAB_NAVIGATOR,
state: {
key: 'tab-state',
routes: [
{
key: 'workspace-navigator',
name: NAVIGATORS.WORKSPACE_NAVIGATOR,
state: {
key: 'workspace-state',
routes: [
{
key: 'domain-split',
name: NAVIGATORS.DOMAIN_SPLIT_NAVIGATOR,
state: {
key: 'domain-split-state',
routes: [
{
key: 'domain-sidebar',
name: SCREENS.DOMAIN.INITIAL,
params: {domainAccountID},
},
{
key: 'domain-members',
name: SCREENS.DOMAIN.MEMBERS,
params: {domainAccountID},
},
],
},
},
],
},
},
],
},
},
],
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
}),
);
}

describe('navigateToDomainRouteWithSidebarSync', () => {
Comment thread
nabi-ebrahimi marked this conversation as resolved.
Resolved
beforeEach(() => {
jest.clearAllMocks();
});

it('updates the persistent sidebar before navigating to another Domain', () => {
mockDomainNavigationState(100);
const targetRoute = ROUTES.DOMAIN_SAML.getRoute(200);

navigateToDomainRouteWithSidebarSync(targetRoute, 200, false);

expect(Navigation.setParams).toHaveBeenCalledWith({domainAccountID: 200}, 'domain-sidebar', 'domain-split-state');
expect(Navigation.navigate).toHaveBeenCalledWith(targetRoute);
const setParamsOrder = jest.mocked(Navigation.setParams).mock.invocationCallOrder.at(0) ?? 0;
const navigateOrder = jest.mocked(Navigation.navigate).mock.invocationCallOrder.at(0) ?? 0;
expect(setParamsOrder).toBeLessThan(navigateOrder);
});

it('does not update the sidebar when navigating within the same Domain', () => {
mockDomainNavigationState(100);
const targetRoute = ROUTES.DOMAIN_SAML.getRoute(100);

navigateToDomainRouteWithSidebarSync(targetRoute, 100, false);

expect(Navigation.setParams).not.toHaveBeenCalled();
expect(Navigation.navigate).toHaveBeenCalledWith(targetRoute);
});

it('navigates directly on narrow layouts', () => {
mockDomainNavigationState(100);
const targetRoute = ROUTES.DOMAIN_SAML.getRoute(200);

navigateToDomainRouteWithSidebarSync(targetRoute, 200, true);

expect(Navigation.setParams).not.toHaveBeenCalled();
expect(Navigation.navigate).toHaveBeenCalledWith(targetRoute);
});

it('navigates directly when there is no active Domain sidebar', () => {
jest.mocked(navigationRef).isReady.mockReturnValue(true);
jest.mocked(navigationRef).getRootState.mockReturnValue(
createMock<ReturnType<typeof navigationRef.getRootState>>({
routes: [],
}),
);
const targetRoute = ROUTES.DOMAIN_SAML.getRoute(200);

navigateToDomainRouteWithSidebarSync(targetRoute, 200, false);

expect(Navigation.setParams).not.toHaveBeenCalled();
expect(Navigation.navigate).toHaveBeenCalledWith(targetRoute);
});

it('navigates directly when navigation is not ready', () => {
jest.mocked(navigationRef).isReady.mockReturnValue(false);
const targetRoute = ROUTES.DOMAIN_SAML.getRoute(200);

navigateToDomainRouteWithSidebarSync(targetRoute, 200, false);

expect(Navigation.setParams).not.toHaveBeenCalled();
expect(Navigation.navigate).toHaveBeenCalledWith(targetRoute);
expect(mockGetRootState).not.toHaveBeenCalled();
});

it('synchronizes a sidebar with a missing Domain ID', () => {
mockDomainNavigationState();
const targetRoute = ROUTES.DOMAIN_SAML.getRoute(200);

navigateToDomainRouteWithSidebarSync(targetRoute, 200, false);

expect(Navigation.setParams).toHaveBeenCalledWith({domainAccountID: 200}, 'domain-sidebar', 'domain-split-state');
expect(Navigation.navigate).toHaveBeenCalledWith(targetRoute);
});

it('finds a root-level Domain split navigator', () => {
jest.mocked(navigationRef).isReady.mockReturnValue(true);
jest.mocked(navigationRef).getRootState.mockReturnValue(
createMock<ReturnType<typeof navigationRef.getRootState>>({
routes: [
{
key: 'domain-split',
name: NAVIGATORS.DOMAIN_SPLIT_NAVIGATOR,
state: {
key: 'domain-split-state',
routes: [
{
key: 'domain-sidebar',
name: SCREENS.DOMAIN.INITIAL,
params: {domainAccountID: 100},
},
],
},
},
],
}),
);
const targetRoute = ROUTES.DOMAIN_SAML.getRoute(200);

navigateToDomainRouteWithSidebarSync(targetRoute, 200, false);

expect(Navigation.setParams).toHaveBeenCalledWith({domainAccountID: 200}, 'domain-sidebar', 'domain-split-state');
expect(Navigation.navigate).toHaveBeenCalledWith(targetRoute);
});
});
Loading