|
| 1 | +import React from 'react' |
| 2 | +import {renderHook} from '../pure' |
| 3 | + |
| 4 | +test('gives comitted result', () => { |
| 5 | + const {result} = renderHook(() => { |
| 6 | + const [state, setState] = React.useState(1) |
| 7 | + |
| 8 | + React.useEffect(() => { |
| 9 | + setState(2) |
| 10 | + }, []) |
| 11 | + |
| 12 | + return [state, setState] |
| 13 | + }) |
| 14 | + |
| 15 | + expect(result.current).toEqual([2, expect.any(Function)]) |
| 16 | +}) |
| 17 | + |
| 18 | +test('allows rerendering', () => { |
| 19 | + const {result, rerender} = renderHook( |
| 20 | + ({branch}) => { |
| 21 | + const [left, setLeft] = React.useState('left') |
| 22 | + const [right, setRight] = React.useState('right') |
| 23 | + |
| 24 | + // eslint-disable-next-line jest/no-if |
| 25 | + switch (branch) { |
| 26 | + case 'left': |
| 27 | + return [left, setLeft] |
| 28 | + case 'right': |
| 29 | + return [right, setRight] |
| 30 | + |
| 31 | + default: |
| 32 | + throw new Error( |
| 33 | + 'No Props passed. This is a bug in the implementation', |
| 34 | + ) |
| 35 | + } |
| 36 | + }, |
| 37 | + {initialProps: {branch: 'left'}}, |
| 38 | + ) |
| 39 | + |
| 40 | + expect(result.current).toEqual(['left', expect.any(Function)]) |
| 41 | + |
| 42 | + rerender({branch: 'right'}) |
| 43 | + |
| 44 | + expect(result.current).toEqual(['right', expect.any(Function)]) |
| 45 | +}) |
| 46 | + |
| 47 | +test('allows wrapper components', async () => { |
| 48 | + const Context = React.createContext('default') |
| 49 | + function Wrapper({children}) { |
| 50 | + return <Context.Provider value="provided">{children}</Context.Provider> |
| 51 | + } |
| 52 | + const {result} = renderHook( |
| 53 | + () => { |
| 54 | + return React.useContext(Context) |
| 55 | + }, |
| 56 | + { |
| 57 | + wrapper: Wrapper, |
| 58 | + }, |
| 59 | + ) |
| 60 | + |
| 61 | + expect(result.current).toEqual('provided') |
| 62 | +}) |
0 commit comments