-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathauto-cleanup.test.tsx
51 lines (42 loc) · 1.19 KB
/
auto-cleanup.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
import * as React from 'react';
import { View } from 'react-native';
import { render } from '..';
let isMounted = false;
class Test extends React.Component<{ onUnmount?(): void }> {
componentDidMount() {
isMounted = true;
}
componentWillUnmount() {
isMounted = false;
if (this.props.onUnmount) {
this.props.onUnmount();
}
}
render() {
return <View />;
}
}
afterEach(() => {
jest.useRealTimers();
});
// This just verifies that by importing RNTL in an environment which supports afterEach (like jest)
// we'll get automatic cleanup between tests.
test('component is mounted, but not unmounted before test ends', () => {
const fn = jest.fn();
render(<Test onUnmount={fn} />);
expect(isMounted).toEqual(true);
expect(fn).not.toHaveBeenCalled();
});
test('component is automatically unmounted after first test ends', () => {
expect(isMounted).toEqual(false);
});
test('does not time out with legacy fake timers', () => {
jest.useFakeTimers({ legacyFakeTimers: true });
render(<Test />);
expect(isMounted).toEqual(true);
});
test('does not time out with fake timers', () => {
jest.useFakeTimers();
render(<Test />);
expect(isMounted).toEqual(true);
});