|
| 1 | +import { useState, useEffect } from 'react' |
| 2 | +import { renderHook } from 'src' |
| 3 | + |
| 4 | +describe('error hook tests', () => { |
| 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 | + setValue(throwError) |
| 19 | + }) |
| 20 | + }, [throwError]) |
| 21 | + return useError(value) |
| 22 | + } |
| 23 | + |
| 24 | + describe('synchronous', () => { |
| 25 | + test('should raise error', () => { |
| 26 | + const { result } = renderHook(() => useError(true)) |
| 27 | + |
| 28 | + expect(() => { |
| 29 | + expect(result.current).not.toBe(undefined) |
| 30 | + }).toThrow(Error('expected')) |
| 31 | + }) |
| 32 | + |
| 33 | + test('should capture error', () => { |
| 34 | + const { result } = renderHook(() => useError(true)) |
| 35 | + |
| 36 | + expect(result.error).toEqual(Error('expected')) |
| 37 | + }) |
| 38 | + |
| 39 | + test('should not capture error', () => { |
| 40 | + const { result } = renderHook(() => useError(false)) |
| 41 | + |
| 42 | + expect(result.current).not.toBe(undefined) |
| 43 | + expect(result.error).toBe(undefined) |
| 44 | + }) |
| 45 | + |
| 46 | + test('should reset error', () => { |
| 47 | + const { result, rerender } = renderHook((throwError) => useError(throwError), { |
| 48 | + initialProps: true |
| 49 | + }) |
| 50 | + |
| 51 | + expect(result.error).not.toBe(undefined) |
| 52 | + |
| 53 | + rerender(false) |
| 54 | + |
| 55 | + expect(result.current).not.toBe(undefined) |
| 56 | + expect(result.error).toBe(undefined) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe('asynchronous', () => { |
| 61 | + test('should raise async error', async () => { |
| 62 | + const { result, waitForNextUpdate } = renderHook(() => useAsyncError(true)) |
| 63 | + |
| 64 | + await waitForNextUpdate() |
| 65 | + |
| 66 | + expect(() => { |
| 67 | + expect(result.current).not.toBe(undefined) |
| 68 | + }).toThrow(Error('expected')) |
| 69 | + }) |
| 70 | + |
| 71 | + test('should capture async error', async () => { |
| 72 | + const { result, waitForNextUpdate } = renderHook(() => useAsyncError(true)) |
| 73 | + |
| 74 | + await waitForNextUpdate() |
| 75 | + |
| 76 | + expect(result.error).toEqual(Error('expected')) |
| 77 | + }) |
| 78 | + |
| 79 | + test('should not capture async error', async () => { |
| 80 | + const { result, waitForNextUpdate } = renderHook(() => useAsyncError(false)) |
| 81 | + |
| 82 | + await waitForNextUpdate() |
| 83 | + |
| 84 | + expect(result.current).not.toBe(undefined) |
| 85 | + expect(result.error).toBe(undefined) |
| 86 | + }) |
| 87 | + |
| 88 | + test('should reset async error', async () => { |
| 89 | + const { result, waitForNextUpdate, rerender } = renderHook( |
| 90 | + (throwError) => useAsyncError(throwError), |
| 91 | + { |
| 92 | + initialProps: true |
| 93 | + } |
| 94 | + ) |
| 95 | + |
| 96 | + await waitForNextUpdate() |
| 97 | + |
| 98 | + expect(result.error).not.toBe(undefined) |
| 99 | + |
| 100 | + rerender(false) |
| 101 | + |
| 102 | + await waitForNextUpdate() |
| 103 | + |
| 104 | + expect(result.current).not.toBe(undefined) |
| 105 | + expect(result.error).toBe(undefined) |
| 106 | + }) |
| 107 | + }) |
| 108 | +}) |
0 commit comments