Skip to content

Commit 797e54c

Browse files
committed
test: πŸ’ add useUnmountPromise() tests
1 parent 01421bc commit 797e54c

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

β€Žsrc/useUnmountPromise.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { useMemo, useRef } from 'react';
1+
import { useMemo, useRef, useEffect } from 'react';
22

33
export type Race = <P extends Promise<any>, E = any>(promise: P, onError?: (error: E) => void) => P;
44

55
const useUnmountPromise = (): Race => {
66
const refUnmounted = useRef(false);
7-
useRef(() => () => {
7+
useEffect(() => () => {
88
refUnmounted.current = true;
99
});
1010

β€Žtests/useUnmountPromise.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import useUnmountPromise from '../src/useUnmountPromise';
3+
4+
describe('useUnmountPromise', () => {
5+
it('should be defined', () => {
6+
expect(useUnmountPromise).toBeDefined();
7+
});
8+
9+
it('should return a function', () => {
10+
const hook = renderHook(() => useUnmountPromise());
11+
12+
expect(typeof hook.result.current).toBe('function');
13+
});
14+
15+
it('when component is mounted function should resolve with wrapped promises', async () => {
16+
const hook = renderHook(() => useUnmountPromise());
17+
18+
const mounted = hook.result.current;
19+
const res = await mounted(new Promise(r => setTimeout(() => r(25), 10)));
20+
21+
expect(res).toBe(25);
22+
});
23+
24+
it('when component is unmounted promise never resolves', async () => {
25+
const hook = renderHook(() => useUnmountPromise());
26+
27+
const mounted = hook.result.current;
28+
const promise = mounted(new Promise(r => setTimeout(() => r(25), 10)));
29+
30+
hook.unmount();
31+
32+
const res = await Promise.race([promise, new Promise(r => setTimeout(() => r('UNMOUNTED'), 20))]);
33+
expect(res).toBe('UNMOUNTED');
34+
});
35+
36+
it('when component is mounted function should resolve with wrapped promises - 2', async () => {
37+
const hook = renderHook(() => useUnmountPromise());
38+
39+
const mounted = hook.result.current;
40+
const promise = mounted(new Promise(r => setTimeout(() => r(25), 10)));
41+
42+
// hook.unmount();
43+
44+
const res = await Promise.race([promise, new Promise(r => setTimeout(() => r('UNMOUNTED'), 20))]);
45+
expect(res).toBe(25);
46+
});
47+
48+
describe('when promise throws', () => {
49+
describe('when component is mounted', () => {
50+
it('onError callback is not called', async () => {
51+
const hook = renderHook(() => useUnmountPromise());
52+
53+
const mounted = hook.result.current;
54+
const onError = jest.fn();
55+
try {
56+
await mounted(new Promise((r, reject) => setTimeout(() => reject(r), 10)), onError);
57+
} catch {}
58+
59+
expect(onError).toHaveBeenCalledTimes(0);
60+
});
61+
});
62+
63+
describe('when component is un-mounted', () => {
64+
it('onError callback is called', async () => {
65+
const hook = renderHook(() => useUnmountPromise());
66+
67+
const mounted = hook.result.current;
68+
const onError = jest.fn();
69+
const promise = mounted(new Promise((r, reject) => setTimeout(() => reject(r), 10)), onError);
70+
71+
hook.unmount();
72+
await Promise.race([promise, new Promise(r => setTimeout(r, 20))]);
73+
74+
expect(onError).toHaveBeenCalledTimes(1);
75+
expect(typeof onError.mock.calls[0][0]).toBe('function');
76+
});
77+
});
78+
});
79+
});

0 commit comments

Comments
Β (0)