MCPcopy
hub / github.com/testing-library/react-testing-library / withGlobalActEnvironment

Function withGlobalActEnvironment

src/act-compat.js:36–81  ·  view source on GitHub ↗
(actImplementation)

Source from the content-addressed store, hash-verified

34}
35
36function withGlobalActEnvironment(actImplementation) {
37 return callback => {
38 const previousActEnvironment = getIsReactActEnvironment()
39 setIsReactActEnvironment(true)
40 try {
41 // The return value of `act` is always a thenable.
42 let callbackNeedsToBeAwaited = false
43 const actResult = actImplementation(() => {
44 const result = callback()
45 if (
46 result !== null &&
47 typeof result === 'object' &&
48 typeof result.then === 'function'
49 ) {
50 callbackNeedsToBeAwaited = true
51 }
52 return result
53 })
54 if (callbackNeedsToBeAwaited) {
55 const thenable = actResult
56 return {
57 then: (resolve, reject) => {
58 thenable.then(
59 returnValue => {
60 setIsReactActEnvironment(previousActEnvironment)
61 resolve(returnValue)
62 },
63 error => {
64 setIsReactActEnvironment(previousActEnvironment)
65 reject(error)
66 },
67 )
68 },
69 }
70 } else {
71 setIsReactActEnvironment(previousActEnvironment)
72 return actResult
73 }
74 } catch (error) {
75 // Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT
76 // or if we have to await the callback first.
77 setIsReactActEnvironment(previousActEnvironment)
78 throw error
79 }
80 }
81}
82
83const act = withGlobalActEnvironment(reactAct)
84

Callers 1

act-compat.jsFile · 0.85

Calls 2

getIsReactActEnvironmentFunction · 0.85
setIsReactActEnvironmentFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…