forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathact-compat.js
More file actions
208 lines (193 loc) · 6.12 KB
/
act-compat.js
File metadata and controls
208 lines (193 loc) · 6.12 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import * as React from 'react'
import ReactDOM from 'react-dom'
import * as testUtils from 'react-dom/test-utils'
const isomorphicAct = React.unstable_act
const domAct = testUtils.act
const actSupported = domAct !== undefined
// act is supported react-dom@16.8.0
// so for versions that don't have act from test utils
// we do this little polyfill. No warnings, but it's
// better than nothing.
function actPolyfill(cb) {
ReactDOM.unstable_batchedUpdates(cb)
ReactDOM.render(<div />, document.createElement('div'))
}
function getGlobalThis() {
/* istanbul ignore else */
if (typeof self !== 'undefined') {
return self
}
/* istanbul ignore next */
if (typeof window !== 'undefined') {
return window
}
/* istanbul ignore next */
if (typeof global !== 'undefined') {
return global
}
/* istanbul ignore next */
throw new Error('unable to locate global object')
}
function setReactActEnvironment(isReactActEnvironment) {
getGlobalThis().IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment
}
function getIsReactActEnvironment() {
return getGlobalThis().IS_REACT_ACT_ENVIRONMENT
}
function withGlobalActEnvironment(actImplementation) {
return callback => {
const previousActEnvironment = getIsReactActEnvironment()
setReactActEnvironment(true)
try {
// The return value of `act` is always a thenable.
let callbackNeedsToBeAwaited = false
const actResult = actImplementation(() => {
const result = callback()
if (
result !== null &&
typeof result === 'object' &&
typeof result.then === 'function'
) {
callbackNeedsToBeAwaited = true
}
return result
})
if (callbackNeedsToBeAwaited) {
const thenable = actResult
return {
then: (resolve, reject) => {
thenable.then(
returnValue => {
setReactActEnvironment(previousActEnvironment)
resolve(returnValue)
},
error => {
setReactActEnvironment(previousActEnvironment)
reject(error)
},
)
},
}
} else {
setReactActEnvironment(previousActEnvironment)
return actResult
}
} catch (error) {
// Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT
// or if we have to await the callback first.
setReactActEnvironment(previousActEnvironment)
throw error
}
}
}
const act = withGlobalActEnvironment(isomorphicAct || domAct || actPolyfill)
let youHaveBeenWarned = false
let isAsyncActSupported = null
function asyncAct(cb) {
if (actSupported === true) {
if (isAsyncActSupported === null) {
return new Promise((resolve, reject) => {
// patch console.error here
const originalConsoleError = console.error
console.error = function error(...args) {
/* if console.error fired *with that specific message* */
/* istanbul ignore next */
const firstArgIsString = typeof args[0] === 'string'
if (
firstArgIsString &&
args[0].indexOf(
'Warning: Do not await the result of calling ReactTestUtils.act',
) === 0
) {
// v16.8.6
isAsyncActSupported = false
} else if (
firstArgIsString &&
args[0].indexOf(
'Warning: The callback passed to ReactTestUtils.act(...) function must not return anything',
) === 0
) {
// no-op
} else {
originalConsoleError.apply(console, args)
}
}
let cbReturn, result
try {
result = domAct(() => {
cbReturn = cb()
return cbReturn
})
} catch (err) {
console.error = originalConsoleError
reject(err)
return
}
result.then(
() => {
console.error = originalConsoleError
// if it got here, it means async act is supported
isAsyncActSupported = true
resolve()
},
err => {
console.error = originalConsoleError
isAsyncActSupported = true
reject(err)
},
)
// 16.8.6's act().then() doesn't call a resolve handler, so we need to manually flush here, sigh
if (isAsyncActSupported === false) {
console.error = originalConsoleError
/* istanbul ignore next */
if (!youHaveBeenWarned) {
// if act is supported and async act isn't and they're trying to use async
// act, then they need to upgrade from 16.8 to 16.9.
// This is a seamless upgrade, so we'll add a warning
console.error(
`It looks like you're using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least react-dom@16.9.0 to remove this warning.`,
)
youHaveBeenWarned = true
}
cbReturn.then(() => {
// a faux-version.
// todo - copy https://github.com/facebook/react/blob/master/packages/shared/enqueueTask.js
Promise.resolve().then(() => {
// use sync act to flush effects
act(() => {})
resolve()
})
}, reject)
}
})
} else if (isAsyncActSupported === false) {
// use the polyfill directly
let result
act(() => {
result = cb()
})
return result.then(() => {
return Promise.resolve().then(() => {
// use sync act to flush effects
act(() => {})
})
})
}
// all good! regular act
return act(cb)
}
// use the polyfill
let result
act(() => {
result = cb()
})
return result.then(() => {
return Promise.resolve().then(() => {
// use sync act to flush effects
act(() => {})
})
})
}
export default act
export {asyncAct, setReactActEnvironment, getIsReactActEnvironment}
/* eslint no-console:0 */