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