|
| 1 | +import {NavigationContainer} from '@react-navigation/native'; |
| 2 | +import {render, renderHook} from '@testing-library/react-native'; |
| 3 | +import React from 'react'; |
| 4 | +import useResponsiveLayout from '@hooks/useResponsiveLayout'; |
| 5 | +import type ResponsiveLayoutResult from '@hooks/useResponsiveLayout/types'; |
| 6 | +import getIsNarrowLayout from '@libs/getIsNarrowLayout'; |
| 7 | +import createResponsiveStackNavigator from '@libs/Navigation/AppNavigator/createResponsiveStackNavigator'; |
| 8 | +import BottomTabNavigator from '@libs/Navigation/AppNavigator/Navigators/BottomTabNavigator'; |
| 9 | +import useNavigationResetRootOnLayoutChange from '@libs/Navigation/AppNavigator/useNavigationResetRootOnLayoutChange'; |
| 10 | +import navigationRef from '@libs/Navigation/navigationRef'; |
| 11 | +import type {AuthScreensParamList} from '@libs/Navigation/types'; |
| 12 | +import ProfilePage from '@pages/settings/Profile/ProfilePage'; |
| 13 | +import NAVIGATORS from '@src/NAVIGATORS'; |
| 14 | +import SCREENS from '@src/SCREENS'; |
| 15 | + |
| 16 | +const RootStack = createResponsiveStackNavigator<AuthScreensParamList>(); |
| 17 | + |
| 18 | +jest.mock('@hooks/useResponsiveLayout', () => jest.fn()); |
| 19 | +jest.mock('@libs/getIsNarrowLayout', () => jest.fn()); |
| 20 | + |
| 21 | +jest.mock('@pages/settings/InitialSettingsPage'); |
| 22 | +jest.mock('@pages/settings/Profile/ProfilePage'); |
| 23 | +jest.mock('@libs/Navigation/AppNavigator/createCustomBottomTabNavigator/BottomTabBar'); |
| 24 | + |
| 25 | +const DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE: ResponsiveLayoutResult = { |
| 26 | + shouldUseNarrowLayout: true, |
| 27 | + isSmallScreenWidth: true, |
| 28 | + isInNarrowPaneModal: false, |
| 29 | + isExtraSmallScreenHeight: false, |
| 30 | + isMediumScreenWidth: false, |
| 31 | + isLargeScreenWidth: false, |
| 32 | + isExtraSmallScreenWidth: false, |
| 33 | + isSmallScreen: false, |
| 34 | + onboardingIsMediumOrLargerScreenWidth: false, |
| 35 | +}; |
| 36 | + |
| 37 | +const INITIAL_STATE = { |
| 38 | + routes: [ |
| 39 | + { |
| 40 | + name: NAVIGATORS.BOTTOM_TAB_NAVIGATOR, |
| 41 | + state: { |
| 42 | + index: 1, |
| 43 | + routes: [{name: SCREENS.HOME}, {name: SCREENS.SETTINGS.ROOT}], |
| 44 | + }, |
| 45 | + }, |
| 46 | + ], |
| 47 | +}; |
| 48 | + |
| 49 | +const mockedGetIsNarrowLayout = getIsNarrowLayout as jest.MockedFunction<typeof getIsNarrowLayout>; |
| 50 | +const mockedUseResponsiveLayout = useResponsiveLayout as jest.MockedFunction<typeof useResponsiveLayout>; |
| 51 | + |
| 52 | +describe('Resize screen', () => { |
| 53 | + it('Should display the settings profile after resizing the screen with the settings page opened to the wide layout', () => { |
| 54 | + // Given the initialized navigation on the narrow layout with the settings screen |
| 55 | + mockedGetIsNarrowLayout.mockReturnValue(true); |
| 56 | + mockedUseResponsiveLayout.mockReturnValue({...DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE, shouldUseNarrowLayout: true}); |
| 57 | + |
| 58 | + const {rerender} = renderHook(() => useNavigationResetRootOnLayoutChange()); |
| 59 | + |
| 60 | + render( |
| 61 | + <NavigationContainer |
| 62 | + ref={navigationRef} |
| 63 | + initialState={INITIAL_STATE} |
| 64 | + > |
| 65 | + <RootStack.Navigator> |
| 66 | + <RootStack.Screen |
| 67 | + name={NAVIGATORS.BOTTOM_TAB_NAVIGATOR} |
| 68 | + component={BottomTabNavigator} |
| 69 | + /> |
| 70 | + |
| 71 | + <RootStack.Screen |
| 72 | + name={SCREENS.SETTINGS.PROFILE.ROOT} |
| 73 | + component={ProfilePage} |
| 74 | + /> |
| 75 | + </RootStack.Navigator> |
| 76 | + </NavigationContainer>, |
| 77 | + ); |
| 78 | + |
| 79 | + const rootStateBeforeResize = navigationRef.current?.getRootState(); |
| 80 | + |
| 81 | + expect(rootStateBeforeResize?.routes.at(0)?.name).toBe(NAVIGATORS.BOTTOM_TAB_NAVIGATOR); |
| 82 | + expect(rootStateBeforeResize?.routes.at(1)).toBeUndefined(); |
| 83 | + |
| 84 | + // When resizing the screen to the wide layout |
| 85 | + mockedGetIsNarrowLayout.mockReturnValue(false); |
| 86 | + mockedUseResponsiveLayout.mockReturnValue({...DEFAULT_USE_RESPONSIVE_LAYOUT_VALUE, shouldUseNarrowLayout: false}); |
| 87 | + rerender({}); |
| 88 | + |
| 89 | + const rootStateAfterResize = navigationRef.current?.getRootState(); |
| 90 | + |
| 91 | + // Then the settings profile page should be displayed on the screen |
| 92 | + expect(rootStateAfterResize?.routes.at(0)?.name).toBe(NAVIGATORS.BOTTOM_TAB_NAVIGATOR); |
| 93 | + expect(rootStateAfterResize?.routes.at(1)?.name).toBe(SCREENS.SETTINGS.PROFILE.ROOT); |
| 94 | + }); |
| 95 | +}); |
0 commit comments