|
| 1 | +import type { ReactNode } from 'react'; |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +import { deprecated_renderHookSync } from '../pure'; |
| 5 | + |
| 6 | +test('gives committed result', () => { |
| 7 | + const { result } = deprecated_renderHookSync(() => { |
| 8 | + const [state, setState] = React.useState(1); |
| 9 | + |
| 10 | + React.useEffect(() => { |
| 11 | + setState(2); |
| 12 | + }, []); |
| 13 | + |
| 14 | + return [state, setState]; |
| 15 | + }); |
| 16 | + |
| 17 | + expect(result.current).toEqual([2, expect.any(Function)]); |
| 18 | +}); |
| 19 | + |
| 20 | +test('allows rerendering', () => { |
| 21 | + const { result, rerender } = deprecated_renderHookSync( |
| 22 | + (props: { branch: 'left' | 'right' }) => { |
| 23 | + const [left, setLeft] = React.useState('left'); |
| 24 | + const [right, setRight] = React.useState('right'); |
| 25 | + |
| 26 | + switch (props.branch) { |
| 27 | + case 'left': |
| 28 | + return [left, setLeft]; |
| 29 | + case 'right': |
| 30 | + return [right, setRight]; |
| 31 | + default: |
| 32 | + throw new Error('No Props passed. This is a bug in the implementation'); |
| 33 | + } |
| 34 | + }, |
| 35 | + { initialProps: { branch: 'left' } }, |
| 36 | + ); |
| 37 | + |
| 38 | + expect(result.current).toEqual(['left', expect.any(Function)]); |
| 39 | + |
| 40 | + rerender({ branch: 'right' }); |
| 41 | + expect(result.current).toEqual(['right', expect.any(Function)]); |
| 42 | +}); |
| 43 | + |
| 44 | +test('allows wrapper components', () => { |
| 45 | + const Context = React.createContext('default'); |
| 46 | + function Wrapper({ children }: { children: ReactNode }) { |
| 47 | + return <Context.Provider value="provided">{children}</Context.Provider>; |
| 48 | + } |
| 49 | + const { result } = deprecated_renderHookSync( |
| 50 | + () => { |
| 51 | + return React.useContext(Context); |
| 52 | + }, |
| 53 | + { |
| 54 | + wrapper: Wrapper, |
| 55 | + }, |
| 56 | + ); |
| 57 | + |
| 58 | + expect(result.current).toEqual('provided'); |
| 59 | +}); |
| 60 | + |
| 61 | +function useMyHook<T>(param: T) { |
| 62 | + return { param }; |
| 63 | +} |
| 64 | + |
| 65 | +test('props type is inferred correctly when initial props is defined', () => { |
| 66 | + const { result, rerender } = deprecated_renderHookSync((num: number) => useMyHook(num), { |
| 67 | + initialProps: 5, |
| 68 | + }); |
| 69 | + expect(result.current.param).toBe(5); |
| 70 | + |
| 71 | + rerender(6); |
| 72 | + expect(result.current.param).toBe(6); |
| 73 | +}); |
| 74 | + |
| 75 | +test('props type is inferred correctly when initial props is explicitly undefined', () => { |
| 76 | + const { result, rerender } = deprecated_renderHookSync( |
| 77 | + (num: number | undefined) => useMyHook(num), |
| 78 | + { |
| 79 | + initialProps: undefined, |
| 80 | + }, |
| 81 | + ); |
| 82 | + |
| 83 | + expect(result.current.param).toBeUndefined(); |
| 84 | + |
| 85 | + rerender(6); |
| 86 | + expect(result.current.param).toBe(6); |
| 87 | +}); |
0 commit comments