|
| 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