|
| 1 | +import { useState, useEffect } from 'react'; |
| 2 | + |
| 3 | +import { act, renderHook } from '../../'; |
| 4 | + |
| 5 | +function useError(throwError) { |
| 6 | + if (throwError) { |
| 7 | + throw new Error('expected'); |
| 8 | + } |
| 9 | + return true; |
| 10 | +} |
| 11 | + |
| 12 | +const somePromise = () => Promise.resolve(); |
| 13 | + |
| 14 | +function useAsyncError(throwError) { |
| 15 | + const [value, setValue] = useState(); |
| 16 | + useEffect(() => { |
| 17 | + somePromise().then(() => { |
| 18 | + act(() => { |
| 19 | + setValue(throwError); |
| 20 | + }); |
| 21 | + }); |
| 22 | + }, [throwError]); |
| 23 | + return useError(value); |
| 24 | +} |
| 25 | + |
| 26 | +test('should raise error', () => { |
| 27 | + const { result } = renderHook(() => useError(true)); |
| 28 | + |
| 29 | + expect(() => { |
| 30 | + expect(result.current).not.toBe(undefined); |
| 31 | + }).toThrow(Error('expected')); |
| 32 | +}); |
| 33 | + |
| 34 | +test('should capture error', () => { |
| 35 | + const { result } = renderHook(() => useError(true)); |
| 36 | + |
| 37 | + expect(result.error).toEqual(Error('expected')); |
| 38 | +}); |
| 39 | + |
| 40 | +test('should not capture error', () => { |
| 41 | + const { result } = renderHook(() => useError(false)); |
| 42 | + |
| 43 | + expect(result.current).not.toBe(undefined); |
| 44 | + expect(result.error).toBe(undefined); |
| 45 | +}); |
| 46 | + |
| 47 | +test('should reset error', () => { |
| 48 | + const { result, rerender } = renderHook(throwError => useError(throwError), { |
| 49 | + initialProps: true, |
| 50 | + }); |
| 51 | + |
| 52 | + expect(result.error).not.toBe(undefined); |
| 53 | + |
| 54 | + rerender(false); |
| 55 | + |
| 56 | + expect(result.current).not.toBe(undefined); |
| 57 | + expect(result.error).toBe(undefined); |
| 58 | +}); |
| 59 | + |
| 60 | +test('should raise async error', async () => { |
| 61 | + const { result, waitForNextUpdate } = renderHook(() => useAsyncError(true)); |
| 62 | + |
| 63 | + await waitForNextUpdate(); |
| 64 | + |
| 65 | + expect(() => { |
| 66 | + expect(result.current).not.toBe(undefined); |
| 67 | + }).toThrow(Error('expected')); |
| 68 | +}); |
| 69 | + |
| 70 | +test('should capture async error', async () => { |
| 71 | + const { result, waitForNextUpdate } = renderHook(() => useAsyncError(true)); |
| 72 | + |
| 73 | + await waitForNextUpdate(); |
| 74 | + |
| 75 | + expect(result.error).toEqual(Error('expected')); |
| 76 | +}); |
| 77 | + |
| 78 | +test('should not capture async error', async () => { |
| 79 | + const { result, waitForNextUpdate } = renderHook(() => useAsyncError(false)); |
| 80 | + |
| 81 | + await waitForNextUpdate(); |
| 82 | + |
| 83 | + expect(result.current).not.toBe(undefined); |
| 84 | + expect(result.error).toBe(undefined); |
| 85 | +}); |
| 86 | + |
| 87 | +test('should reset async error', async () => { |
| 88 | + const { result, waitForNextUpdate, rerender } = renderHook( |
| 89 | + throwError => useAsyncError(throwError), |
| 90 | + { |
| 91 | + initialProps: true, |
| 92 | + }, |
| 93 | + ); |
| 94 | + |
| 95 | + await waitForNextUpdate(); |
| 96 | + |
| 97 | + expect(result.error).not.toBe(undefined); |
| 98 | + |
| 99 | + rerender(false); |
| 100 | + |
| 101 | + await waitForNextUpdate(); |
| 102 | + |
| 103 | + expect(result.current).not.toBe(undefined); |
| 104 | + expect(result.error).toBe(undefined); |
| 105 | +}); |
0 commit comments