Skip to content

Commit ce82069

Browse files
committed
fix(mobile): add NetInfo mock + fix ErrorBoundary test logic bugs
- Add __mocks__/@react-native-community/netinfo.js and moduleNameMapper to fix 3 test suites crashing on missing native module - ErrorBoundary: use getAllByText when error.stack duplicates message - ErrorBoundary: move stop-booming button outside boundary so it stays mounted when Bomb throws All 19 suites pass (176/176 tests) with 0 tsc errors.
1 parent b75b7e4 commit ce82069

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const NetInfo = {
2+
fetch: jest.fn(() =>
3+
Promise.resolve({
4+
isConnected: true,
5+
isInternetReachable: true,
6+
type: "wifi",
7+
details: { isConnectionExpensive: false },
8+
}),
9+
),
10+
addEventListener: jest.fn(() => ({
11+
remove: jest.fn(),
12+
})),
13+
useNetInfo: jest.fn(() => ({
14+
isConnected: true,
15+
isInternetReachable: true,
16+
type: "wifi",
17+
details: null,
18+
})),
19+
configure: jest.fn(),
20+
};
21+
22+
module.exports = NetInfo;

mobile/components/__tests__/ErrorBoundary.test.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ describe("ErrorBoundary", () => {
7878
const alert = screen.getByRole("alert");
7979
expect(alert).toBeTruthy();
8080
expect(alert.props.accessibilityLiveRegion).toBe("assertive");
81-
expect(screen.getByText(/intentional render error/i)).toBeTruthy();
81+
const matches = screen.getAllByText(/intentional render error/i);
82+
expect(matches.length).toBeGreaterThan(0);
8283
expect(screen.getByRole("button", { name: /retry/i })).toBeTruthy();
8384

8485
spy.mockRestore();
@@ -149,12 +150,14 @@ describe("ErrorBoundary", () => {
149150
function ToggleHarness() {
150151
const [boom, setBoom] = React.useState(true);
151152
return (
152-
<ErrorBoundary>
153+
<>
153154
<Pressable testID="stop-booming" onPress={() => setBoom(false)}>
154155
<Text>stop</Text>
155156
</Pressable>
156-
<Bomb shouldThrow={boom} />
157-
</ErrorBoundary>
157+
<ErrorBoundary>
158+
<Bomb shouldThrow={boom} />
159+
</ErrorBoundary>
160+
</>
158161
);
159162
}
160163
render(<ToggleHarness />);

mobile/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"^expo-local-authentication$": "<rootDir>/__mocks__/expo-local-authentication.js",
6464
"^expo-secure-store$": "<rootDir>/__mocks__/expo-secure-store.js",
6565
"^expo-notifications$": "<rootDir>/__mocks__/expo-notifications.js",
66+
"^@react-native-community/netinfo$": "<rootDir>/__mocks__/@react-native-community/netinfo.js",
6667
"^react-native/Libraries/BatchedBridge/NativeModules$": "<rootDir>/__mocks__/NativeModules.js"
6768
}
6869
}

0 commit comments

Comments
 (0)