-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathact.ts
75 lines (66 loc) · 2.84 KB
/
act.ts
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
// This file and the act() implementation is sourced from react-testing-library
// https://github.com/testing-library/react-testing-library/blob/3dcd8a9649e25054c0e650d95fca2317b7008576/types/index.d.ts
import * as React from 'react';
import { act as reactTestRendererAct } from 'react-test-renderer';
const reactAct = typeof React.act === 'function' ? React.act : reactTestRendererAct;
type ReactAct = 0 extends 1 & typeof React.act ? typeof reactTestRendererAct : typeof React.act;
// See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT
declare global {
// eslint-disable-next-line no-var
var IS_REACT_ACT_ENVIRONMENT: boolean | undefined;
}
function setIsReactActEnvironment(isReactActEnvironment: boolean | undefined) {
globalThis.IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment;
}
function getIsReactActEnvironment() {
return globalThis.IS_REACT_ACT_ENVIRONMENT;
}
function withGlobalActEnvironment(actImplementation: ReactAct) {
return (callback: Parameters<ReactAct>[0]) => {
const previousActEnvironment = getIsReactActEnvironment();
setIsReactActEnvironment(true);
try {
// The return value of `act` is always a thenable.
let callbackNeedsToBeAwaited = false;
const actResult = actImplementation(() => {
const result = callback();
// @ts-expect-error TS is too strict here
if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
callbackNeedsToBeAwaited = true;
}
return result;
});
if (callbackNeedsToBeAwaited) {
const thenable = actResult;
return {
then: (resolve: (value: never) => never, reject: (value: never) => never) => {
// eslint-disable-next-line promise/catch-or-return, promise/prefer-await-to-then
thenable.then(
// eslint-disable-next-line promise/always-return
(returnValue) => {
setIsReactActEnvironment(previousActEnvironment);
resolve(returnValue as never);
},
(error) => {
setIsReactActEnvironment(previousActEnvironment);
reject(error as never);
},
);
},
};
} else {
setIsReactActEnvironment(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.
setIsReactActEnvironment(previousActEnvironment);
throw error;
}
};
}
// @ts-expect-error: typings get too complex
const act = withGlobalActEnvironment(reactAct) as ReactAct;
export default act;
export { getIsReactActEnvironment, setIsReactActEnvironment as setReactActEnvironment };