Skip to content

Commit f327969

Browse files
Merge pull request Expensify#65340 from daledah/fix/64320
fix: correct isActiveRoute
2 parents 58229fe + 0ca96c5 commit f327969

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

src/libs/Navigation/Navigation.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ function getReportRHPActiveRoute(): string {
160160
return '';
161161
}
162162

163+
/**
164+
* Cleans the route path by removing redundant slashes and query parameters.
165+
* @param routePath The route path to clean.
166+
* @returns The cleaned route path.
167+
*/
168+
function cleanRoutePath(routePath: string): string {
169+
return routePath.replace(CONST.REGEX.ROUTES.REDUNDANT_SLASHES, (match, p1) => (p1 ? '/' : '')).replace(/\?.*/, '');
170+
}
171+
163172
/**
164173
* Check whether the passed route is currently Active or not.
165174
*
@@ -170,11 +179,11 @@ function getReportRHPActiveRoute(): string {
170179
* @return is active
171180
*/
172181
function isActiveRoute(routePath: Route): boolean {
173-
let activeRoute = getActiveRoute();
182+
let activeRoute = getActiveRouteWithoutParams();
174183
activeRoute = activeRoute.startsWith('/') ? activeRoute.substring(1) : activeRoute;
175184

176185
// We remove redundant (consecutive and trailing) slashes from path before matching
177-
return activeRoute === routePath.replace(CONST.REGEX.ROUTES.REDUNDANT_SLASHES, (match, p1) => (p1 ? '/' : ''));
186+
return cleanRoutePath(activeRoute) === cleanRoutePath(routePath);
178187
}
179188

180189
/**
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {describe, expect, test} from '@jest/globals';
2+
import {render} from '@testing-library/react-native';
3+
import useResponsiveLayout from '@hooks/useResponsiveLayout';
4+
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
5+
import CONST from '@src/CONST';
6+
import Navigation from '@src/libs/Navigation/Navigation';
7+
import NAVIGATORS from '@src/NAVIGATORS';
8+
import type {Route} from '@src/ROUTES';
9+
import SCREENS from '@src/SCREENS';
10+
import TestNavigationContainer from '../utils/TestNavigationContainer';
11+
12+
jest.mock('@hooks/useResponsiveLayout', () => jest.fn());
13+
jest.mock('@libs/getIsNarrowLayout', () => jest.fn());
14+
15+
// Mock Fullstory library dependency
16+
jest.mock('@libs/Fullstory', () => ({
17+
default: {
18+
consentAndIdentify: jest.fn(),
19+
},
20+
parseFSAttributes: jest.fn(),
21+
}));
22+
23+
jest.mock('@pages/home/sidebar/NavigationTabBarAvatar');
24+
jest.mock('@src/components/Navigation/TopLevelNavigationTabBar');
25+
26+
const mockedGetIsNarrowLayout = getIsNarrowLayout as jest.MockedFunction<typeof getIsNarrowLayout>;
27+
const mockedUseResponsiveLayout = useResponsiveLayout as jest.MockedFunction<typeof useResponsiveLayout>;
28+
29+
describe('Navigation', () => {
30+
beforeEach(() => {
31+
mockedGetIsNarrowLayout.mockReturnValue(true);
32+
mockedUseResponsiveLayout.mockReturnValue({...CONST.NAVIGATION_TESTS.DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE, shouldUseNarrowLayout: true});
33+
});
34+
// given current active route is "/settings/profile?backTo=settings%2profile"
35+
test.each([
36+
['settings/profile' as Route, true],
37+
['settings/profile/' as Route, true],
38+
['settings/profile?param=1' as Route, true],
39+
['settings/profile/display-name' as Route, false],
40+
['settings/profile/display-name/' as Route, false],
41+
['settings/preferences' as Route, false],
42+
['report' as Route, false],
43+
['report/123/' as Route, false],
44+
['report/123' as Route, false],
45+
])('isActiveRoute("%s") should return %s', (routeToCheck, expectedResult) => {
46+
render(
47+
<TestNavigationContainer
48+
initialState={{
49+
index: 0,
50+
routes: [
51+
{
52+
name: NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR,
53+
state: {
54+
index: 1,
55+
routes: [
56+
{
57+
name: SCREENS.SETTINGS.ROOT,
58+
},
59+
{
60+
name: SCREENS.SETTINGS.PROFILE.ROOT,
61+
params: {
62+
backTo: 'settings/profile',
63+
},
64+
},
65+
],
66+
},
67+
},
68+
],
69+
}}
70+
/>,
71+
);
72+
const result = Navigation.isActiveRoute(routeToCheck);
73+
expect(result).toBe(expectedResult);
74+
});
75+
});

0 commit comments

Comments
 (0)