|
| 1 | +import { mount } from 'enzyme' |
| 2 | +import React from 'react' |
| 3 | +import { act } from 'react-dom/test-utils' |
| 4 | +import useStateAsync, { AsyncSetState } from '../src/useStateAsync' |
| 5 | + |
| 6 | +describe('useStateAsync', () => { |
| 7 | + it('should increment counter', async () => { |
| 8 | + let asyncState: [number, AsyncSetState<number>] |
| 9 | + |
| 10 | + function Wrapper() { |
| 11 | + asyncState = useStateAsync<number>(0) |
| 12 | + return null |
| 13 | + } |
| 14 | + |
| 15 | + mount(<Wrapper />) |
| 16 | + |
| 17 | + expect.assertions(4) |
| 18 | + |
| 19 | + const incrementAsync = async () => { |
| 20 | + await act(() => asyncState[1](prev => prev + 1)) |
| 21 | + } |
| 22 | + |
| 23 | + expect(asyncState![0]).toEqual(0) |
| 24 | + |
| 25 | + await incrementAsync() |
| 26 | + expect(asyncState![0]).toEqual(1) |
| 27 | + |
| 28 | + await incrementAsync() |
| 29 | + expect(asyncState![0]).toEqual(2) |
| 30 | + |
| 31 | + await incrementAsync() |
| 32 | + expect(asyncState![0]).toEqual(3) |
| 33 | + }) |
| 34 | + |
| 35 | + it('should reject on error', async () => { |
| 36 | + let asyncState: [number, AsyncSetState<number>] |
| 37 | + |
| 38 | + function Wrapper() { |
| 39 | + asyncState = useStateAsync<number>(1) |
| 40 | + return null |
| 41 | + } |
| 42 | + class CatchError extends React.Component { |
| 43 | + static getDerivedStateFromError() {} |
| 44 | + componentDidCatch() {} |
| 45 | + render() { |
| 46 | + return this.props.children |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + mount( |
| 51 | + <CatchError> |
| 52 | + <Wrapper /> |
| 53 | + </CatchError>, |
| 54 | + ) |
| 55 | + |
| 56 | + // @ts-ignore |
| 57 | + expect.errors(1) |
| 58 | + |
| 59 | + await act(async () => { |
| 60 | + const p = asyncState[1](() => { |
| 61 | + throw new Error('yo') |
| 62 | + }) |
| 63 | + return expect(p).rejects.toThrow('yo') |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should resolve even if no update happens', async () => { |
| 68 | + let asyncState: [number, AsyncSetState<number>] |
| 69 | + |
| 70 | + function Wrapper() { |
| 71 | + asyncState = useStateAsync<number>(1) |
| 72 | + return null |
| 73 | + } |
| 74 | + |
| 75 | + mount(<Wrapper />) |
| 76 | + |
| 77 | + expect.assertions(3) |
| 78 | + |
| 79 | + expect(asyncState![0]).toEqual(1) |
| 80 | + |
| 81 | + await act(() => expect(asyncState[1](1)).resolves.toEqual(1)) |
| 82 | + |
| 83 | + expect(asyncState![0]).toEqual(1) |
| 84 | + }) |
| 85 | + |
| 86 | + it('should resolve after update if already pending', async () => { |
| 87 | + let asyncState: [number, AsyncSetState<number>] |
| 88 | + |
| 89 | + function Wrapper() { |
| 90 | + asyncState = useStateAsync<number>(0) |
| 91 | + return null |
| 92 | + } |
| 93 | + |
| 94 | + mount(<Wrapper />) |
| 95 | + |
| 96 | + expect.assertions(5) |
| 97 | + |
| 98 | + expect(asyncState![0]).toEqual(0) |
| 99 | + |
| 100 | + const setAndAssert = async (n: number) => |
| 101 | + expect(asyncState[1](n)).resolves.toEqual(2) |
| 102 | + |
| 103 | + await act(() => |
| 104 | + Promise.all([setAndAssert(1), setAndAssert(1), setAndAssert(2)]), |
| 105 | + ) |
| 106 | + |
| 107 | + expect(asyncState![0]).toEqual(2) |
| 108 | + }) |
| 109 | +}) |
0 commit comments