-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathact.test.tsx
55 lines (42 loc) · 1.53 KB
/
act.test.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
55
import * as React from 'react';
import { Text } from 'react-native';
import { act, fireEvent, render, screen } from '..';
type UseEffectProps = { callback(): void };
const UseEffect = ({ callback }: UseEffectProps) => {
React.useEffect(callback);
return null;
};
const Counter = () => {
const [count, setCount] = React.useState(0);
const text = `Total count: ${count}`;
return <Text onPress={() => setCount(count + 1)}>{text}</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();
render(<UseEffect callback={effectCallback} />);
screen.update(<UseEffect callback={effectCallback} />);
expect(effectCallback).toHaveBeenCalledTimes(2);
});
test('fireEvent should trigger useState', () => {
render(<Counter />);
const counter = screen.getByText(/Total count/i);
expect(counter).toHaveTextContent('Total count: 0');
fireEvent.press(counter);
expect(counter).toHaveTextContent('Total count: 1');
});
test('should be able to not await act', () => {
const result = act(() => {});
expect(result).toHaveProperty('then');
});
test('should be able to await act', async () => {
const result = await act(async () => {});
expect(result).toBe(undefined);
});
test('should be able to await act when promise rejects', async () => {
await expect(act(() => Promise.reject('error'))).rejects.toBe('error');
});