-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseUnmountPromise.test.ts
101 lines (75 loc) Β· 2.98 KB
/
useUnmountPromise.test.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { renderHook } from '@testing-library/react-hooks';
import useUnmountPromise from '../src/useUnmountPromise';
describe('useUnmountPromise', () => {
it('should be defined', () => {
expect(useUnmountPromise).toBeDefined();
});
it('should return a function', () => {
const hook = renderHook(() => useUnmountPromise());
expect(typeof hook.result.current).toBe('function');
});
it('when component is mounted function should resolve with wrapped promises', async () => {
const hook = renderHook(() => useUnmountPromise());
const mounted = hook.result.current;
const res = await mounted(new Promise((r) => setTimeout(() => r(25), 10)));
expect(res).toBe(25);
});
it('when component is unmounted promise never resolves', async () => {
const hook = renderHook(() => useUnmountPromise());
const mounted = hook.result.current;
const promise = mounted(new Promise((r) => setTimeout(() => r(25), 10)));
hook.unmount();
const res = await Promise.race([
promise,
new Promise((r) => setTimeout(() => r('UNMOUNTED'), 20)),
]);
expect(res).toBe('UNMOUNTED');
});
it('should resolve promise when component is updated', async () => {
const hook = renderHook(() => useUnmountPromise());
const mounted = hook.result.current;
const pRes = mounted(new Promise((r) => setTimeout(() => r(25), 10)));
hook.rerender();
const res = await pRes;
expect(res).toBe(25);
});
it('when component is mounted function should resolve with wrapped promises - 2', async () => {
const hook = renderHook(() => useUnmountPromise());
const mounted = hook.result.current;
const promise = mounted(new Promise((r) => setTimeout(() => r(25), 10)));
// hook.unmount();
const res = await Promise.race([
promise,
new Promise((r) => setTimeout(() => r('UNMOUNTED'), 20)),
]);
expect(res).toBe(25);
});
describe('when promise throws', () => {
describe('when component is mounted', () => {
it('onError callback is not called', async () => {
const hook = renderHook(() => useUnmountPromise());
const mounted = hook.result.current;
const onError = jest.fn();
try {
await mounted(new Promise((r, reject) => setTimeout(() => reject(r), 10)), onError);
} catch {}
expect(onError).toHaveBeenCalledTimes(0);
});
});
describe('when component is un-mounted', () => {
it('onError callback is called', async () => {
const hook = renderHook(() => useUnmountPromise());
const mounted = hook.result.current;
const onError = jest.fn();
const promise = mounted(
new Promise((r, reject) => setTimeout(() => reject(r), 10)),
onError
);
hook.unmount();
await Promise.race([promise, new Promise((r) => setTimeout(r, 20))]);
expect(onError).toHaveBeenCalledTimes(1);
expect(typeof onError.mock.calls[0][0]).toBe('function');
});
});
});
});