|
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'; |
9 | 5 | import type {Route} from '@src/ROUTES'; |
10 | | -import SCREENS from '@src/SCREENS'; |
11 | | -import TestNavigationContainer from '../utils/TestNavigationContainer'; |
12 | 6 |
|
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 | +}); |
18 | 20 |
|
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 | +}); |
21 | 29 |
|
22 | 30 | describe('Navigation', () => { |
23 | 31 | 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(); |
40 | 33 | }); |
| 34 | + |
41 | 35 | 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); |
44 | 45 | }); |
45 | 46 |
|
46 | 47 | 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 | | - |
75 | 48 | expect(Navigation.isActiveRoute('settings/profile' as Route)).toBe(true); |
76 | 49 | expect(Navigation.isActiveRoute('settings/profile/' as Route)).toBe(true); |
77 | 50 | expect(Navigation.isActiveRoute('settings/profile?param=1' as Route)).toBe(true); |
|
0 commit comments