forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderHook.js
141 lines (120 loc) · 3.83 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, {useEffect} from 'react'
import {configure, renderHook} from '../pure'
const isReact18 = React.version.startsWith('18.')
const isReact19 = React.version.startsWith('19.')
const testGateReact18 = isReact18 ? test : test.skip
const testGateReact19 = isReact19 ? test : test.skip
test('gives committed 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, jest/no-conditional-in-test -- false-positive
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')
})
testGateReact18('legacyRoot uses legacy ReactDOM.render', () => {
const Context = React.createContext('default')
function Wrapper({children}) {
return <Context.Provider value="provided">{children}</Context.Provider>
}
let result
expect(() => {
result = renderHook(
() => {
return React.useContext(Context)
},
{
wrapper: Wrapper,
legacyRoot: true,
},
).result
}).toErrorDev(
[
"Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot",
],
{withoutStack: true},
)
expect(result.current).toEqual('provided')
})
testGateReact19('legacyRoot throws', () => {
const Context = React.createContext('default')
function Wrapper({children}) {
return <Context.Provider value="provided">{children}</Context.Provider>
}
expect(() => {
renderHook(
() => {
return React.useContext(Context)
},
{
wrapper: Wrapper,
legacyRoot: true,
},
).result
}).toThrowErrorMatchingInlineSnapshot(
`\`legacyRoot: true\` is not supported in this version of React. If your app runs React 19 or later, you should remove this flag. If your app runs React 18 or earlier, visit https://react.dev/blog/2022/03/08/react-18-upgrade-guide for upgrade instructions.`,
)
})
describe('reactStrictMode', () => {
let originalConfig
beforeEach(() => {
// Grab the existing configuration so we can restore
// it at the end of the test
configure(existingConfig => {
originalConfig = existingConfig
// Don't change the existing config
return {}
})
})
afterEach(() => {
configure(originalConfig)
})
test('reactStrictMode in renderOptions has precedence over config when rendering', () => {
const hookMountEffect = jest.fn()
configure({reactStrictMode: false})
renderHook(() => useEffect(() => hookMountEffect()), {
reactStrictMode: true,
})
expect(hookMountEffect).toHaveBeenCalledTimes(2)
})
})