forked from testing-library/react-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathact-compat.js
53 lines (46 loc) · 1.46 KB
/
act-compat.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
import * as React from 'react'
import * as DeprecatedReactTestUtils from 'react-dom/test-utils'
const reactAct =
typeof React.act === 'function' ? React.act : DeprecatedReactTestUtils.act
function getGlobalThis() {
/* istanbul ignore else */
if (typeof globalThis !== 'undefined') {
return globalThis
}
/* istanbul ignore next */
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 setIsReactActEnvironment(isReactActEnvironment) {
getGlobalThis().IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment
}
function getIsReactActEnvironment() {
return getGlobalThis().IS_REACT_ACT_ENVIRONMENT
}
async function actIfEnabled(scope) {
if (getIsReactActEnvironment()) {
// scope passed to domAct needs to be `async` until React.act treats every scope as async.
// We already enforce `await act()` (regardless of scope) to flush microtasks
// inside the act scope.
return reactAct(async () => {
return scope()
})
} else {
// We wrap everything in act internally.
// But a userspace call might not want that so we respect global config here.
return scope()
}
}
export {actIfEnabled, setIsReactActEnvironment, getIsReactActEnvironment}
/* eslint no-console:0 */