Repository navigation
Fix cross-Domain Search Router navigation #100150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
71bf540
0773ded
d853d83
c41042e
648eade
9ee3789
0b5dee7
826f8f1
1b68aef
a637a6a
c7b1c7a
7404893
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| */ | ||
|
nabi-ebrahimi marked this conversation as resolved.
Resolved
|
||
| import {SPLIT_TO_SIDEBAR} from '@libs/Navigation/linkingConfig/RELATIONS'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Every test stubs Add a test "navigates directly when navigationRef is not ready":
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
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 = | ||
|
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); | ||
|
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) { | ||
|
nabi-ebrahimi marked this conversation as resolved.
Resolved
|
||
| if (shouldUseNarrowLayout) { | ||
| Navigation.navigate(targetRoute); | ||
| return; | ||
| } | ||
|
|
||
| const domainSidebarRoute = getActiveDomainSidebarRoute(); | ||
|
nabi-ebrahimi marked this conversation as resolved.
Resolved
|
||
| if (domainSidebarRoute && domainSidebarRoute.domainAccountID !== domainAccountID) { | ||
| Navigation.setParams({domainAccountID}, domainSidebarRoute.sidebarRouteKey, domainSidebarRoute.splitStateKey); | ||
| } | ||
|
nabi-ebrahimi marked this conversation as resolved.
Resolved
|
||
|
|
||
| Navigation.navigate(targetRoute); | ||
| } | ||
|
|
||
| export default navigateToDomainRouteWithSidebarSync; | ||
| 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}, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
|
nabi-ebrahimi marked this conversation as resolved.
Resolved
|
||
| }), | ||
| ); | ||
| } | ||
|
|
||
| describe('navigateToDomainRouteWithSidebarSync', () => { | ||
|
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); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.