-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathact.test.js
58 lines (46 loc) · 1.44 KB
/
act.test.js
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
55
56
57
58
// @flow
import React from 'react';
import { Text } from 'react-native';
import ReactTestRenderer from 'react-test-renderer';
import act from '../act';
import render from '../render';
import fireEvent from '../fireEvent';
const UseEffect = ({ callback }: { callback: Function }) => {
React.useEffect(callback);
return null;
};
const Counter = () => {
const [count, setCount] = React.useState(0);
return (
<Text testID="counter" onPress={() => setCount(count + 1)}>
{count}
</Text>
);
};
test('render should trigger useEffect', () => {
const effectCallback = jest.fn();
render(<UseEffect callback={effectCallback} />);
expect(effectCallback).toHaveBeenCalledTimes(1);
});
test('update should trigger useEffect', () => {
const effectCallback = jest.fn();
const { update } = render(<UseEffect callback={effectCallback} />);
update(<UseEffect callback={effectCallback} />);
expect(effectCallback).toHaveBeenCalledTimes(2);
});
test('fireEvent should trigger useState', () => {
const { getByTestId } = render(<Counter />);
const counter = getByTestId('counter');
expect(counter.props.children).toEqual(0);
fireEvent.press(counter);
expect(counter.props.children).toEqual(1);
});
test('should act even if there is no act in react-test-renderer', () => {
// $FlowFixMe
ReactTestRenderer.act = undefined;
const callback = jest.fn();
act(() => {
callback();
});
expect(callback).toHaveBeenCalled();
});