Skip to content

Commit 59cdaf0

Browse files
authored
Merge pull request Expensify#65707 from linhvovan29546/fix/65480-the-loading-indicator-displayed-when-offline
Fix/65480 the loading indicator displayed when offline
2 parents e1033bd + 8624ab8 commit 59cdaf0

2 files changed

Lines changed: 122 additions & 2 deletions

File tree

src/pages/workspace/WorkspacePageWithSections.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function WorkspacePageWithSections({
128128
}: WorkspacePageWithSectionsProps) {
129129
const styles = useThemeStyles();
130130
const policyID = route.params?.policyID;
131-
useNetwork({onReconnect: () => fetchData(policyID, shouldSkipVBBACall)});
131+
const {isOffline} = useNetwork({onReconnect: () => fetchData(policyID, shouldSkipVBBACall)});
132132

133133
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: false});
134134
const [reimbursementAccount = CONST.REIMBURSEMENT_ACCOUNT.DEFAULT_DATA] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {canBeMissing: true});
@@ -218,7 +218,7 @@ function WorkspacePageWithSections({
218218
>
219219
{headerContent}
220220
</HeaderWithBackButton>
221-
{(isLoading || firstRender.current) && shouldShowLoading && isFocused ? (
221+
{!isOffline && (isLoading || firstRender.current) && shouldShowLoading && isFocused ? (
222222
<FullScreenLoadingIndicator style={[styles.flex1, styles.pRelative]} />
223223
) : (
224224
<>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)