Skip to content

Commit fa79dea

Browse files
committed
add unit test for isActiveRoute
1 parent 6db56ce commit fa79dea

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

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)