| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import type {Fiber} from './ReactFiber'; |
| 11 | |
| 12 | import ReactSharedInternals from 'shared/ReactSharedInternals'; |
| 13 | |
| 14 | import {warnsIfNotActing} from './ReactFiberConfig'; |
| 15 | |
| 16 | export function isLegacyActEnvironment(fiber: Fiber): boolean { |
| 17 | if (__DEV__) { |
| 18 | // Legacy mode. We preserve the behavior of React 17's act. It assumes an |
| 19 | // act environment whenever `jest` is defined, but you can still turn off |
| 20 | // spurious warnings by setting IS_REACT_ACT_ENVIRONMENT explicitly |
| 21 | // to false. |
| 22 | |
| 23 | const isReactActEnvironmentGlobal = |
| 24 | // $FlowFixMe[cannot-resolve-name] Flow doesn't know about IS_REACT_ACT_ENVIRONMENT global |
| 25 | typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined' |
| 26 | ? // $FlowFixMe[cannot-resolve-name] |
| 27 | IS_REACT_ACT_ENVIRONMENT |
| 28 | : undefined; |
| 29 | |
| 30 | // $FlowFixMe[cannot-resolve-name] - Flow doesn't know about jest |
| 31 | const jestIsDefined = typeof jest !== 'undefined'; |
| 32 | return ( |
| 33 | // $FlowFixMe[constant-condition] |
| 34 | warnsIfNotActing && jestIsDefined && isReactActEnvironmentGlobal !== false |
| 35 | ); |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | export function isConcurrentActEnvironment(): void | boolean { |
| 41 | if (__DEV__) { |
| 42 | const isReactActEnvironmentGlobal = |
| 43 | // $FlowFixMe[cannot-resolve-name] Flow doesn't know about IS_REACT_ACT_ENVIRONMENT global |
| 44 | typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined' |
| 45 | ? // $FlowFixMe[cannot-resolve-name] |
| 46 | IS_REACT_ACT_ENVIRONMENT |
| 47 | : undefined; |
| 48 | |
| 49 | if ( |
| 50 | !isReactActEnvironmentGlobal && |
| 51 | ReactSharedInternals.actQueue !== null |
| 52 | ) { |
| 53 | // TODO: Include link to relevant documentation page. |
| 54 | console.error( |
| 55 | 'The current testing environment is not configured to support ' + |
| 56 | 'act(...)', |
| 57 | ); |
| 58 | } |
| 59 | return isReactActEnvironmentGlobal; |
| 60 | } |
| 61 | return false; |
| 62 | } |