Project Structure
1.
2├── app.test.js
3├── tabs.test.js
4├── demos
5│ ├── demo-api.test.js
6│ ├── demo-navigation.js
7│ └── navigation.test.js
8├── mocks
9│ ├── handlers.js
10│ ├── responses.js
11│ └── server.js
12├── setup
13│ ├── api.setup.js
14│ ├── jest.setup.js
15│ ├── navigation.setup.js
16│ └── redux.setup.js
1import { describe } from '@jest/globals';
2import { View, Text } from 'react-native';
3import React, { useState, useEffect } from 'react';
4import '@testing-library/jest-native/extend-expect';
5import { act, render, screen } from '@testing-library/react-native';
6
7export function App() {
8 const [items, setItems] = useState([]);
9 useEffect(() => {
10 async function getStuff() {
11 const r = await fetch('http://www.google.com');
12 const j = await r.json();
13 setItems(JSON.parse(j));
14 }
15 getStuff();
16 }, []);
17
18 return (
19 <View>
20 {items.map((i) => (
21 <Text key={i}>{i}</Text>
22 ))}
23 </View>
24 );
25}
26
27describe('APIs', () => {
28 test('are mocked by testing framework', async () => {
29 render(<App />);
30 await act(async () => {
31 const val = await screen.findByText(/Spam/i);
32 expect(val).toBeOnTheScreen();
33 });
34 });
35});