Skip to content

Commit 2305a32

Browse files
authored
Merge pull request Expensify#75604 from ShridharGoel/isActiveTestsFix
[NoQA] Optimise isActiveRouteTests
2 parents 1f7efee + 40372d2 commit 2305a32

1 file changed

Lines changed: 36 additions & 63 deletions

File tree

tests/navigation/isActiveRouteTests.tsx

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,50 @@
1-
import {describe, expect} from '@jest/globals';
2-
import {cleanup, 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 navigationRef from '@src/libs/Navigation/navigationRef';
8-
import NAVIGATORS from '@src/NAVIGATORS';
1+
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
2+
import type {getPathFromState as GetPathFromState} from '@react-navigation/native';
3+
import Navigation from '@libs/Navigation/Navigation';
4+
import navigationRef from '@libs/Navigation/navigationRef';
95
import type {Route} from '@src/ROUTES';
10-
import SCREENS from '@src/SCREENS';
11-
import TestNavigationContainer from '../utils/TestNavigationContainer';
126

13-
jest.mock('@hooks/useResponsiveLayout', () => jest.fn());
14-
jest.mock('@libs/getIsNarrowLayout', () => jest.fn());
15-
16-
jest.mock('@pages/home/sidebar/NavigationTabBarAvatar');
17-
jest.mock('@src/components/Navigation/TopLevelNavigationTabBar');
7+
jest.mock('@libs/Navigation/navigationRef', () => {
8+
const navigationRefMock = {
9+
current: {getCurrentRoute: jest.fn()},
10+
getRootState: jest.fn(),
11+
isReady: jest.fn(),
12+
};
13+
14+
return {
15+
// eslint-disable-next-line @typescript-eslint/naming-convention
16+
__esModule: true,
17+
default: navigationRefMock,
18+
};
19+
});
1820

19-
const mockedGetIsNarrowLayout = getIsNarrowLayout as jest.MockedFunction<typeof getIsNarrowLayout>;
20-
const mockedUseResponsiveLayout = useResponsiveLayout as jest.MockedFunction<typeof useResponsiveLayout>;
21+
jest.mock('@react-navigation/native', () => {
22+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
23+
const actual = jest.requireActual('@react-navigation/native') as {getPathFromState: typeof GetPathFromState};
24+
return {
25+
...actual,
26+
getPathFromState: jest.fn<typeof GetPathFromState>(() => '/settings/profile?backTo=settings'),
27+
};
28+
});
2129

2230
describe('Navigation', () => {
2331
afterEach(() => {
24-
// Ensure mounted components are unmounted
25-
cleanup();
26-
27-
// Clear timers and restore real timers (in case fake timers are used anywhere)
28-
jest.clearAllTimers();
29-
jest.useRealTimers();
30-
31-
// Reset any mocks used by this file
32-
jest.restoreAllMocks();
33-
jest.resetModules();
34-
35-
// Clear the navigation ref so listeners/hooks attached to it don't keep the worker alive.
36-
// This is intentionally type-unsafe to forcibly drop the ref between tests.
37-
if (navigationRef.current) {
38-
navigationRef.current = null;
39-
}
32+
jest.clearAllMocks();
4033
});
34+
4135
beforeEach(() => {
42-
mockedGetIsNarrowLayout.mockReturnValue(true);
43-
mockedUseResponsiveLayout.mockReturnValue({...CONST.NAVIGATION_TESTS.DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE, shouldUseNarrowLayout: true});
36+
const navigationRefMock = navigationRef as typeof navigationRef & {
37+
current: {getCurrentRoute: jest.Mock};
38+
getRootState: jest.Mock;
39+
isReady: jest.Mock;
40+
};
41+
42+
navigationRefMock.current.getCurrentRoute.mockReturnValue({name: 'test'});
43+
navigationRefMock.getRootState.mockReturnValue({} as ReturnType<typeof navigationRef.getRootState>);
44+
navigationRefMock.isReady.mockReturnValue(true);
4445
});
4546

4647
it('Should correctly identify active routes', () => {
47-
// Given current active route is "/settings/profile?backTo=settings%2profile"
48-
render(
49-
<TestNavigationContainer
50-
initialState={{
51-
index: 0,
52-
routes: [
53-
{
54-
name: NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR,
55-
state: {
56-
index: 1,
57-
routes: [
58-
{
59-
name: SCREENS.SETTINGS.ROOT,
60-
},
61-
{
62-
name: SCREENS.SETTINGS.PROFILE.ROOT,
63-
params: {
64-
backTo: 'settings/profile',
65-
},
66-
},
67-
],
68-
},
69-
},
70-
],
71-
}}
72-
/>,
73-
);
74-
7548
expect(Navigation.isActiveRoute('settings/profile' as Route)).toBe(true);
7649
expect(Navigation.isActiveRoute('settings/profile/' as Route)).toBe(true);
7750
expect(Navigation.isActiveRoute('settings/profile?param=1' as Route)).toBe(true);

0 commit comments

Comments
 (0)