|
| 1 | +import {render, screen} from '@testing-library/react-native'; |
| 2 | +import React from 'react'; |
| 3 | +import {View} from 'react-native'; |
| 4 | +import Onyx from 'react-native-onyx'; |
| 5 | +import ComposeProviders from '@components/ComposeProviders'; |
| 6 | +import {LocaleContextProvider} from '@components/LocaleContextProvider'; |
| 7 | +import OnyxProvider from '@components/OnyxProvider'; |
| 8 | +import type Navigation from '@libs/Navigation/Navigation'; |
| 9 | +import WorkspacePageWithSections from '@pages/workspace/WorkspacePageWithSections'; |
| 10 | +import CONST from '@src/CONST'; |
| 11 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 12 | +import SCREENS from '@src/SCREENS'; |
| 13 | +import type {Policy} from '@src/types/onyx'; |
| 14 | +import createRandomPolicy from '../utils/collections/policies'; |
| 15 | +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; |
| 16 | +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; |
| 17 | + |
| 18 | +const POLICY_ID = 1; |
| 19 | + |
| 20 | +// Mock navigation hooks |
| 21 | +jest.mock('@react-navigation/native', () => { |
| 22 | + const actualNav = jest.requireActual<typeof Navigation>('@react-navigation/native'); |
| 23 | + return { |
| 24 | + ...actualNav, |
| 25 | + useIsFocused: () => true, |
| 26 | + useRoute: () => ({ |
| 27 | + key: 'test-route', |
| 28 | + name: 'WORKSPACE_INITIAL', |
| 29 | + params: {policyID: POLICY_ID.toString()}, |
| 30 | + }), |
| 31 | + usePreventRemove: jest.fn(), |
| 32 | + }; |
| 33 | +}); |
| 34 | + |
| 35 | +// Mock useResponsiveLayout hook |
| 36 | +jest.mock('@src/hooks/useResponsiveLayout'); |
| 37 | + |
| 38 | +// Mock FullScreenLoadingIndicator |
| 39 | +jest.mock('@components/FullscreenLoadingIndicator', () => { |
| 40 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 41 | + const ReactNative = require('react-native'); |
| 42 | + return () => { |
| 43 | + return <ReactNative.View testID="FullScreenLoadingIndicator" />; |
| 44 | + }; |
| 45 | +}); |
| 46 | + |
| 47 | +const mockPolicy: Policy = {...createRandomPolicy(POLICY_ID), type: CONST.POLICY.TYPE.CORPORATE, pendingAction: null, role: CONST.POLICY.ROLE.ADMIN}; |
| 48 | + |
| 49 | +const renderWorkspacePageWithSections = (props = {}) => { |
| 50 | + const defaultProps = { |
| 51 | + headerText: 'Test Workspace', |
| 52 | + route: { |
| 53 | + key: 'test-route', |
| 54 | + name: SCREENS.WORKSPACE.INITIAL, |
| 55 | + params: {policyID: POLICY_ID.toString()}, |
| 56 | + }, |
| 57 | + policy: mockPolicy, |
| 58 | + ...props, |
| 59 | + }; |
| 60 | + |
| 61 | + return render( |
| 62 | + <ComposeProviders components={[OnyxProvider, LocaleContextProvider]}> |
| 63 | + <WorkspacePageWithSections |
| 64 | + // eslint-disable-next-line react/jsx-props-no-spreading |
| 65 | + {...defaultProps} |
| 66 | + > |
| 67 | + <View /> |
| 68 | + </WorkspacePageWithSections> |
| 69 | + </ComposeProviders>, |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +describe('WorkspacePageWithSections', () => { |
| 74 | + describe('FullScreenLoadingIndicator behavior', () => { |
| 75 | + beforeAll(() => { |
| 76 | + Onyx.init({ |
| 77 | + keys: ONYXKEYS, |
| 78 | + }); |
| 79 | + Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, mockPolicy); |
| 80 | + return waitForBatchedUpdates(); |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(() => { |
| 84 | + jest.clearAllMocks(); |
| 85 | + return Onyx.clear(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should not display FullScreenLoadingIndicator when user is offline', async () => { |
| 89 | + // Given the network state is offline |
| 90 | + await Onyx.merge(ONYXKEYS.NETWORK, {isOffline: true}); |
| 91 | + |
| 92 | + // When render the component with loading enabled |
| 93 | + renderWorkspacePageWithSections({ |
| 94 | + shouldShowLoading: true, |
| 95 | + isLoading: true, |
| 96 | + }); |
| 97 | + |
| 98 | + await waitForBatchedUpdatesWithAct(); |
| 99 | + |
| 100 | + // Then the FullScreenLoadingIndicator should not be displayed |
| 101 | + expect(screen.queryByTestId('FullScreenLoadingIndicator')).toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should display FullScreenLoadingIndicator when user is online and loading', async () => { |
| 105 | + // Given the network state is online |
| 106 | + await Onyx.merge(ONYXKEYS.NETWORK, {isOffline: false}); |
| 107 | + |
| 108 | + // When render the component with loading enabled |
| 109 | + renderWorkspacePageWithSections({ |
| 110 | + shouldShowLoading: true, |
| 111 | + isLoading: true, |
| 112 | + }); |
| 113 | + |
| 114 | + await waitForBatchedUpdatesWithAct(); |
| 115 | + |
| 116 | + // Then the FullScreenLoadingIndicator should be displayed |
| 117 | + expect(screen.getByTestId('FullScreenLoadingIndicator')).toBeTruthy(); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments