|
1 |
| -import { act } from 'react-test-renderer'; |
| 1 | +// This file and the act() implementation is sourced from react-testing-library |
| 2 | +// https://github.com/testing-library/react-testing-library/blob/c80809a956b0b9f3289c4a6fa8b5e8cc72d6ef6d/src/act-compat.js |
| 3 | +import { act as reactTestRendererAct } from 'react-test-renderer'; |
| 4 | +import { checkReactVersionAtLeast } from './react-versions'; |
2 | 5 |
|
3 | 6 | const actMock = (callback: () => void) => {
|
4 | 7 | callback();
|
5 | 8 | };
|
6 | 9 |
|
7 |
| -export default act || actMock; |
| 10 | +// See https://github.com/reactwg/react-18/discussions/102 for more context on global.IS_REACT_ACT_ENVIRONMENT |
| 11 | +declare global { |
| 12 | + var IS_REACT_ACT_ENVIRONMENT: boolean | undefined; |
| 13 | +} |
| 14 | + |
| 15 | +function setIsReactActEnvironment(isReactActEnvironment: boolean | undefined) { |
| 16 | + globalThis.IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment; |
| 17 | +} |
| 18 | + |
| 19 | +function getIsReactActEnvironment() { |
| 20 | + return globalThis.IS_REACT_ACT_ENVIRONMENT; |
| 21 | +} |
| 22 | + |
| 23 | +type Act = typeof reactTestRendererAct; |
| 24 | + |
| 25 | +function withGlobalActEnvironment(actImplementation: Act) { |
| 26 | + return (callback: Parameters<Act>[0]) => { |
| 27 | + const previousActEnvironment = getIsReactActEnvironment(); |
| 28 | + setIsReactActEnvironment(true); |
| 29 | + |
| 30 | + // this code is riddled with eslint disabling comments because this doesn't use real promises but eslint thinks we do |
| 31 | + try { |
| 32 | + // The return value of `act` is always a thenable. |
| 33 | + let callbackNeedsToBeAwaited = false; |
| 34 | + const actResult = actImplementation(() => { |
| 35 | + const result = callback(); |
| 36 | + if ( |
| 37 | + result !== null && |
| 38 | + typeof result === 'object' && |
| 39 | + // @ts-expect-error this should be a promise or thenable |
| 40 | + // eslint-disable-next-line promise/prefer-await-to-then |
| 41 | + typeof result.then === 'function' |
| 42 | + ) { |
| 43 | + callbackNeedsToBeAwaited = true; |
| 44 | + } |
| 45 | + return result; |
| 46 | + }); |
| 47 | + if (callbackNeedsToBeAwaited) { |
| 48 | + const thenable = actResult; |
| 49 | + return { |
| 50 | + then: ( |
| 51 | + resolve: (value: never) => never, |
| 52 | + reject: (value: never) => never |
| 53 | + ) => { |
| 54 | + // eslint-disable-next-line |
| 55 | + thenable.then( |
| 56 | + // eslint-disable-next-line promise/always-return |
| 57 | + (returnValue) => { |
| 58 | + setIsReactActEnvironment(previousActEnvironment); |
| 59 | + resolve(returnValue); |
| 60 | + }, |
| 61 | + (error) => { |
| 62 | + setIsReactActEnvironment(previousActEnvironment); |
| 63 | + reject(error); |
| 64 | + } |
| 65 | + ); |
| 66 | + }, |
| 67 | + }; |
| 68 | + } else { |
| 69 | + setIsReactActEnvironment(previousActEnvironment); |
| 70 | + return actResult; |
| 71 | + } |
| 72 | + } catch (error) { |
| 73 | + // Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT |
| 74 | + // or if we have to await the callback first. |
| 75 | + setIsReactActEnvironment(previousActEnvironment); |
| 76 | + throw error; |
| 77 | + } |
| 78 | + }; |
| 79 | +} |
| 80 | +const getAct = () => { |
| 81 | + if (!reactTestRendererAct) { |
| 82 | + return actMock; |
| 83 | + } |
| 84 | + |
| 85 | + return checkReactVersionAtLeast(18, 0) |
| 86 | + ? withGlobalActEnvironment(reactTestRendererAct) |
| 87 | + : reactTestRendererAct; |
| 88 | +}; |
| 89 | +const act = getAct(); |
| 90 | + |
| 91 | +export default act; |
| 92 | +export { |
| 93 | + setIsReactActEnvironment as setReactActEnvironment, |
| 94 | + getIsReactActEnvironment, |
| 95 | +}; |
0 commit comments