Skip to content

Commit

Permalink
Move TestNavigationContainer to test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekBoman committed Jan 10, 2025
1 parent c7cc789 commit e1316a7
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 211 deletions.
82 changes: 1 addition & 81 deletions tests/navigation/GoBackTests.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import type {InitialState} from '@react-navigation/native';
import {NavigationContainer} from '@react-navigation/native';
import {act, render} from '@testing-library/react-native';
import React from 'react';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import createRootStackNavigator from '@libs/Navigation/AppNavigator/createRootStackNavigator';
import createSplitNavigator from '@libs/Navigation/AppNavigator/createSplitNavigator';
import Navigation from '@libs/Navigation/Navigation';
import navigationRef from '@libs/Navigation/navigationRef';
import type {AuthScreensParamList, ReportsSplitNavigatorParamList, SettingsSplitNavigatorParamList} from '@libs/Navigation/types';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';

const RootStack = createRootStackNavigator<AuthScreensParamList>();
const ReportsSplit = createSplitNavigator<ReportsSplitNavigatorParamList>();
const SettingsSplit = createSplitNavigator<SettingsSplitNavigatorParamList>();
import TestNavigationContainer from '../utils/TestNavigationContainer';

jest.mock('@hooks/useResponsiveLayout', () => jest.fn());
jest.mock('@libs/getIsNarrowLayout', () => jest.fn());
Expand All @@ -26,78 +18,6 @@ jest.mock('@pages/home/sidebar/BottomTabAvatar');
const mockedGetIsNarrowLayout = getIsNarrowLayout as jest.MockedFunction<typeof getIsNarrowLayout>;
const mockedUseResponsiveLayout = useResponsiveLayout as jest.MockedFunction<typeof useResponsiveLayout>;

function SettingsSplitNavigator() {
return (
<SettingsSplit.Navigator
sidebarScreen={SCREENS.SETTINGS.ROOT}
defaultCentralScreen={SCREENS.SETTINGS.PROFILE.ROOT}
parentRoute={CONST.NAVIGATION_TESTS.DEFAULT_PARENT_ROUTE}
>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.ROOT}
getComponent={() => jest.fn()}
/>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.PROFILE.ROOT}
getComponent={() => jest.fn()}
/>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.PREFERENCES.ROOT}
getComponent={() => jest.fn()}
/>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.ABOUT}
getComponent={() => jest.fn()}
/>
</SettingsSplit.Navigator>
);
}

function ReportsSplitNavigator() {
return (
<ReportsSplit.Navigator
sidebarScreen={SCREENS.HOME}
defaultCentralScreen={SCREENS.REPORT}
parentRoute={CONST.NAVIGATION_TESTS.DEFAULT_PARENT_ROUTE}
>
<ReportsSplit.Screen
name={SCREENS.HOME}
getComponent={() => jest.fn()}
/>
<ReportsSplit.Screen
name={SCREENS.REPORT}
getComponent={() => jest.fn()}
/>
</ReportsSplit.Navigator>
);
}

type TestNavigationContainerProps = {initialState: InitialState};

function TestNavigationContainer({initialState}: TestNavigationContainerProps) {
return (
<NavigationContainer
ref={navigationRef}
initialState={initialState}
>
<RootStack.Navigator>
<RootStack.Screen
name={NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}
component={ReportsSplitNavigator}
/>
<RootStack.Screen
name={NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR}
component={SettingsSplitNavigator}
/>
<RootStack.Screen
name={SCREENS.SEARCH.ROOT}
getComponent={() => jest.fn()}
/>
</RootStack.Navigator>
</NavigationContainer>
);
}

describe('Go back on the narrow layout', () => {
beforeEach(() => {
mockedGetIsNarrowLayout.mockReturnValue(true);
Expand Down
82 changes: 1 addition & 81 deletions tests/navigation/NavigateTests.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import type {InitialState} from '@react-navigation/native';
import {NavigationContainer} from '@react-navigation/native';
import {act, render} from '@testing-library/react-native';
import React from 'react';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import createRootStackNavigator from '@libs/Navigation/AppNavigator/createRootStackNavigator';
import createSplitNavigator from '@libs/Navigation/AppNavigator/createSplitNavigator';
import Navigation from '@libs/Navigation/Navigation';
import navigationRef from '@libs/Navigation/navigationRef';
import type {AuthScreensParamList, ReportsSplitNavigatorParamList, SettingsSplitNavigatorParamList} from '@libs/Navigation/types';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';

const RootStack = createRootStackNavigator<AuthScreensParamList>();
const ReportsSplit = createSplitNavigator<ReportsSplitNavigatorParamList>();
const SettingsSplit = createSplitNavigator<SettingsSplitNavigatorParamList>();
import TestNavigationContainer from '../utils/TestNavigationContainer';

jest.mock('@hooks/useResponsiveLayout', () => jest.fn());
jest.mock('@libs/getIsNarrowLayout', () => jest.fn());
Expand All @@ -26,78 +18,6 @@ jest.mock('@pages/home/sidebar/BottomTabAvatar');
const mockedGetIsNarrowLayout = getIsNarrowLayout as jest.MockedFunction<typeof getIsNarrowLayout>;
const mockedUseResponsiveLayout = useResponsiveLayout as jest.MockedFunction<typeof useResponsiveLayout>;

function SettingsSplitNavigator() {
return (
<SettingsSplit.Navigator
sidebarScreen={SCREENS.SETTINGS.ROOT}
defaultCentralScreen={SCREENS.SETTINGS.PROFILE.ROOT}
parentRoute={CONST.NAVIGATION_TESTS.DEFAULT_PARENT_ROUTE}
>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.ROOT}
getComponent={() => jest.fn()}
/>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.PROFILE.ROOT}
getComponent={() => jest.fn()}
/>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.PREFERENCES.ROOT}
getComponent={() => jest.fn()}
/>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.ABOUT}
getComponent={() => jest.fn()}
/>
</SettingsSplit.Navigator>
);
}

function ReportsSplitNavigator() {
return (
<ReportsSplit.Navigator
sidebarScreen={SCREENS.HOME}
defaultCentralScreen={SCREENS.REPORT}
parentRoute={CONST.NAVIGATION_TESTS.DEFAULT_PARENT_ROUTE}
>
<ReportsSplit.Screen
name={SCREENS.HOME}
getComponent={() => jest.fn()}
/>
<ReportsSplit.Screen
name={SCREENS.REPORT}
getComponent={() => jest.fn()}
/>
</ReportsSplit.Navigator>
);
}

type TestNavigationContainerProps = {initialState: InitialState};

function TestNavigationContainer({initialState}: TestNavigationContainerProps) {
return (
<NavigationContainer
ref={navigationRef}
initialState={initialState}
>
<RootStack.Navigator>
<RootStack.Screen
name={NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}
component={ReportsSplitNavigator}
/>
<RootStack.Screen
name={NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR}
component={SettingsSplitNavigator}
/>
<RootStack.Screen
name={SCREENS.SEARCH.ROOT}
getComponent={() => jest.fn()}
/>
</RootStack.Navigator>
</NavigationContainer>
);
}

describe('Navigate', () => {
beforeEach(() => {
mockedGetIsNarrowLayout.mockReturnValue(true);
Expand Down
50 changes: 1 addition & 49 deletions tests/navigation/SwitchPolicyIDTests.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import type {InitialState} from '@react-navigation/native';
import {NavigationContainer} from '@react-navigation/native';
import {act, render} from '@testing-library/react-native';
import React from 'react';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
import createRootStackNavigator from '@libs/Navigation/AppNavigator/createRootStackNavigator';
import createSplitNavigator from '@libs/Navigation/AppNavigator/createSplitNavigator';
import navigationRef from '@libs/Navigation/navigationRef';
import type {AuthScreensParamList, ReportsSplitNavigatorParamList} from '@libs/Navigation/types';
import switchPolicyAfterInteractions from '@pages/WorkspaceSwitcherPage/switchPolicyAfterInteractions';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';

const RootStack = createRootStackNavigator<AuthScreensParamList>();
const ReportsSplit = createSplitNavigator<ReportsSplitNavigatorParamList>();
import TestNavigationContainer from '../utils/TestNavigationContainer';

jest.mock('@hooks/useResponsiveLayout', () => jest.fn());
jest.mock('@libs/getIsNarrowLayout', () => jest.fn());
Expand All @@ -24,47 +17,6 @@ jest.mock('@pages/home/sidebar/BottomTabAvatar');
const mockedGetIsNarrowLayout = getIsNarrowLayout as jest.MockedFunction<typeof getIsNarrowLayout>;
const mockedUseResponsiveLayout = useResponsiveLayout as jest.MockedFunction<typeof useResponsiveLayout>;

function ReportsSplitNavigator() {
return (
<ReportsSplit.Navigator
sidebarScreen={SCREENS.HOME}
defaultCentralScreen={SCREENS.REPORT}
parentRoute={CONST.NAVIGATION_TESTS.DEFAULT_PARENT_ROUTE}
>
<ReportsSplit.Screen
name={SCREENS.HOME}
getComponent={() => jest.fn()}
/>
<ReportsSplit.Screen
name={SCREENS.REPORT}
getComponent={() => jest.fn()}
/>
</ReportsSplit.Navigator>
);
}

type TestNavigationContainerProps = {initialState: InitialState};

function TestNavigationContainer({initialState}: TestNavigationContainerProps) {
return (
<NavigationContainer
ref={navigationRef}
initialState={initialState}
>
<RootStack.Navigator>
<RootStack.Screen
name={NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}
component={ReportsSplitNavigator}
/>
<RootStack.Screen
name={SCREENS.SEARCH.ROOT}
getComponent={() => jest.fn()}
/>
</RootStack.Navigator>
</NavigationContainer>
);
}

describe('Switch policy ID', () => {
beforeEach(() => {
mockedGetIsNarrowLayout.mockReturnValue(true);
Expand Down
87 changes: 87 additions & 0 deletions tests/utils/TestNavigationContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type {InitialState} from '@react-navigation/native';
import {NavigationContainer} from '@react-navigation/native';
import React from 'react';
import createRootStackNavigator from '@libs/Navigation/AppNavigator/createRootStackNavigator';
import createSplitNavigator from '@libs/Navigation/AppNavigator/createSplitNavigator';
import navigationRef from '@libs/Navigation/navigationRef';
import type {AuthScreensParamList, ReportsSplitNavigatorParamList, SettingsSplitNavigatorParamList} from '@libs/Navigation/types';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import SCREENS from '@src/SCREENS';

const RootStack = createRootStackNavigator<AuthScreensParamList>();
const ReportsSplit = createSplitNavigator<ReportsSplitNavigatorParamList>();
const SettingsSplit = createSplitNavigator<SettingsSplitNavigatorParamList>();

type TestNavigationContainerProps = {initialState: InitialState};

function TestReportsSplitNavigator() {
return (
<ReportsSplit.Navigator
sidebarScreen={SCREENS.HOME}
defaultCentralScreen={SCREENS.REPORT}
parentRoute={CONST.NAVIGATION_TESTS.DEFAULT_PARENT_ROUTE}
>
<ReportsSplit.Screen
name={SCREENS.HOME}
getComponent={() => jest.fn()}
/>
<ReportsSplit.Screen
name={SCREENS.REPORT}
getComponent={() => jest.fn()}
/>
</ReportsSplit.Navigator>
);
}

function TestSettingsSplitNavigator() {
return (
<SettingsSplit.Navigator
sidebarScreen={SCREENS.SETTINGS.ROOT}
defaultCentralScreen={SCREENS.SETTINGS.PROFILE.ROOT}
parentRoute={CONST.NAVIGATION_TESTS.DEFAULT_PARENT_ROUTE}
>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.ROOT}
getComponent={() => jest.fn()}
/>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.PROFILE.ROOT}
getComponent={() => jest.fn()}
/>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.PREFERENCES.ROOT}
getComponent={() => jest.fn()}
/>
<SettingsSplit.Screen
name={SCREENS.SETTINGS.ABOUT}
getComponent={() => jest.fn()}
/>
</SettingsSplit.Navigator>
);
}

function TestNavigationContainer({initialState}: TestNavigationContainerProps) {
return (
<NavigationContainer
ref={navigationRef}
initialState={initialState}
>
<RootStack.Navigator>
<RootStack.Screen
name={NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}
component={TestReportsSplitNavigator}
/>
<RootStack.Screen
name={NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR}
component={TestSettingsSplitNavigator}
/>
<RootStack.Screen
name={SCREENS.SEARCH.ROOT}
getComponent={() => jest.fn()}
/>
</RootStack.Navigator>
</NavigationContainer>
);
}
export default TestNavigationContainer;

0 comments on commit e1316a7

Please sign in to comment.