Skip to content

Commit fd2cba8

Browse files
committed
Test harness and proper mocking
1 parent 49e755d commit fd2cba8

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

examples/cookbook/__mocks__/axios.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const chuckNorrisError = () => {
2+
throw Error(
3+
"Please ensure you mock 'Axios' - Only Chuck Norris is allowed to make API requests when testing ;)",
4+
);
5+
};
6+
7+
export default {
8+
get: jest.fn(chuckNorrisError),
9+
post: jest.fn(chuckNorrisError),
10+
put: jest.fn(chuckNorrisError),
11+
delete: jest.fn(chuckNorrisError),
12+
request: jest.fn(chuckNorrisError),
13+
};

examples/cookbook/app/network-requests/__tests__/PhoneBook.test.tsx

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,17 @@ import { render, screen, waitForElementToBeRemoved } from '@testing-library/reac
22
import React from 'react';
33
import PhoneBook from '../PhoneBook';
44
import { User } from '../types';
5+
import axios from 'axios';
56

6-
// Ensure that the global fetch function is mocked in your setup file
7-
// before running the tests. This will ensure that the tests don't
8-
// make any actual network requests that you haven't mocked.
9-
beforeAll(() => {
10-
jest.spyOn(global, 'fetch').mockImplementation(
11-
jest.fn(() => {
12-
throw Error('Only Chuck Norris is allowed to make API requests when testing ;)');
13-
}),
14-
);
15-
});
16-
17-
afterAll(() => {
18-
(global.fetch as jest.Mock).mockRestore();
19-
});
7+
jest.mock('axios');
8+
const mockedAxios = axios as jest.Mocked<typeof axios>;
209

2110
describe('PhoneBook', () => {
22-
let originalFetch: typeof global.fetch;
23-
beforeAll(() => {
24-
originalFetch = global.fetch;
25-
global.fetch = jest.fn().mockResolvedValueOnce({
11+
it('fetches contacts successfully and renders in list', async () => {
12+
(global.fetch as jest.SpyInstance).mockResolvedValueOnce({
2613
json: jest.fn().mockResolvedValueOnce(DATA),
2714
});
28-
//TODO: mock axios
29-
});
30-
31-
afterAll(() => {
32-
global.fetch = originalFetch;
33-
});
34-
35-
it('fetches contacts successfully and renders in list', async () => {
15+
(mockedAxios.get as jest.Mock).mockResolvedValue({ data: DATA });
3616
render(<PhoneBook />);
3717

3818
await waitForElementToBeRemoved(() => screen.getByText(/users data not quite there yet/i));
@@ -42,11 +22,15 @@ describe('PhoneBook', () => {
4222
});
4323

4424
it('fetches favorites successfully and renders in list', async () => {
25+
(global.fetch as jest.SpyInstance).mockResolvedValueOnce({
26+
json: jest.fn().mockResolvedValueOnce(DATA),
27+
});
28+
(mockedAxios.get as jest.Mock).mockResolvedValue({ data: DATA });
4529
render(<PhoneBook />);
4630

4731
await waitForElementToBeRemoved(() => screen.getByText(/figuring out your favorites/i));
4832
expect(await screen.findByText(/my favorites/i)).toBeOnTheScreen();
49-
expect(await screen.findAllByText(/name/i)).toHaveLength(3);
33+
expect(await screen.findAllByLabelText('favorite-contact-avatar')).toHaveLength(3);
5034
});
5135
});
5236

examples/cookbook/app/network-requests/components/FavoritesList.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export default ({ users }: { users: User[] }) => {
77
const renderItem: ListRenderItem<User> = useCallback(({ item: { picture } }) => {
88
return (
99
<View style={styles.userContainer}>
10-
<Image source={{ uri: picture.thumbnail }} style={styles.userImage} />
10+
<Image
11+
source={{ uri: picture.thumbnail }}
12+
style={styles.userImage}
13+
accessibilityLabel={'favorite-contact-avatar'}
14+
/>
1115
</View>
1216
);
1317
}, []);

examples/cookbook/jest-setup.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ import '@testing-library/react-native/extend-expect';
55

66
// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
77
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
8+
9+
// Guard against API requests made during testing
10+
beforeAll(() => {
11+
// the global fetch function:
12+
jest.spyOn(global, 'fetch').mockImplementation(()=> {
13+
throw Error("Please ensure you mock 'fetch' Only Chuck Norris is allowed to make API requests when testing ;)");
14+
});
15+
// with Axios:
16+
// see examples/cookbook/__mocks__/axios.ts
17+
});
18+
afterAll(() => {
19+
(global.fetch as jest.Mock).mockRestore();
20+
});

0 commit comments

Comments
 (0)