-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.unit.tsx
54 lines (45 loc) · 1.69 KB
/
App.unit.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @format
*/
import 'react-native';
import React from 'react';
import App from '../App';
// Note: test renderer must be required after react-native.
// import renderer from 'react-test-renderer';
import {fireEvent, render, waitFor} from '@testing-library/react-native';
it('has a button for each valid tag returned', async () => {
global.fetch = jest.fn().mockImplementationOnce(() => {
return new Promise((resolve, _) => {
resolve({
ok: true,
status: 200,
json: () => Promise.resolve(['one', 'two', 'three', 'box of frogs']),
});
});
});
const component = render(<App />);
await waitFor(() => expect(global.fetch).toHaveBeenCalledTimes(1));
expect(component.queryAllByText('one >')).toHaveLength(1);
expect(component.queryAllByText('two >')).toHaveLength(1);
expect(component.queryAllByText('three >')).toHaveLength(1);
expect(component.queryAllByText('four >')).toHaveLength(0);
expect(component.queryAllByText('box of frogs >')).toHaveLength(0);
});
it('displays a random image when I select a tag', async () => {
global.fetch = jest.fn().mockImplementationOnce(() => {
return new Promise((resolve, _) => {
resolve({
ok: true,
status: 200,
json: () => Promise.resolve(['one', 'two', 'three']),
});
});
});
const component = render(<App />);
await waitFor(() => expect(global.fetch).toHaveBeenCalledTimes(1));
const button = component.getByTestId('test:id/one');
fireEvent.press(button);
expect(component.queryAllByTestId('test:id/tagged-image')).toHaveLength(1);
const image = component.getByTestId('test:id/tagged-image');
expect(image.props.source.uri).toBe('https://cataas.com/c/one');
});