|
| 1 | +import { useState, useCallback, useEffect } from 'react' |
| 2 | +import { renderHook } from 'react-hooks-testing-library' |
| 3 | + |
| 4 | +const useCounter = (initialCount: number = 0) => { |
| 5 | + const [count, setCount] = useState(initialCount) |
| 6 | + const incrementBy = useCallback( |
| 7 | + (n: number) => { |
| 8 | + setCount(count + n) |
| 9 | + }, |
| 10 | + [count] |
| 11 | + ) |
| 12 | + const decrementBy = useCallback( |
| 13 | + (n: number) => { |
| 14 | + setCount(count - n) |
| 15 | + }, |
| 16 | + [count] |
| 17 | + ) |
| 18 | + return { |
| 19 | + count, |
| 20 | + incrementBy, |
| 21 | + decrementBy |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function checkTypesWithNoInitialProps() { |
| 26 | + const { result, unmount, rerender } = renderHook(() => useCounter()) |
| 27 | + |
| 28 | + // check types |
| 29 | + const _result: { |
| 30 | + current: { |
| 31 | + count: number |
| 32 | + incrementBy: (_: number) => void |
| 33 | + decrementBy: (_: number) => void |
| 34 | + } |
| 35 | + } = result |
| 36 | + const _unmount: () => boolean = unmount |
| 37 | + const _rerender: () => void = rerender |
| 38 | +} |
| 39 | + |
| 40 | +function checkTypesWithInitialProps() { |
| 41 | + const { result, unmount, rerender } = renderHook(({ count }) => useCounter(count), { |
| 42 | + initialProps: { count: 10 } |
| 43 | + }) |
| 44 | + |
| 45 | + // check types |
| 46 | + const _result: { |
| 47 | + current: { |
| 48 | + count: number |
| 49 | + incrementBy: (_: number) => void |
| 50 | + decrementBy: (_: number) => void |
| 51 | + } |
| 52 | + } = result |
| 53 | + const _unmount: () => boolean = unmount |
| 54 | + const _rerender: (_?: { count: number }) => void = rerender |
| 55 | +} |
| 56 | + |
| 57 | +function checkTypesWhenHookReturnsVoid() { |
| 58 | + const { result, unmount, rerender } = renderHook(() => useEffect(() => {})) |
| 59 | + |
| 60 | + // check types |
| 61 | + const _result: { |
| 62 | + current: void |
| 63 | + } = result |
| 64 | + const _unmount: () => boolean = unmount |
| 65 | + const _rerender: () => void = rerender |
| 66 | +} |
0 commit comments