mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
80 lines
2.3 KiB
JavaScript
80 lines
2.3 KiB
JavaScript
// @flow
|
|
|
|
import typeof ReactTestRenderer from 'react-test-renderer';
|
|
|
|
export function act(callback: Function): void {
|
|
const TestUtils = require('react-dom/test-utils');
|
|
TestUtils.act(() => {
|
|
callback();
|
|
});
|
|
|
|
// Flush Bridge operations
|
|
jest.runAllTimers();
|
|
}
|
|
|
|
export async function actSuspense(
|
|
callback: Function,
|
|
numTimesToFlush: number = 1
|
|
): Promise<void> {
|
|
const TestUtils = require('react-dom/test-utils');
|
|
const Scheduler = require('scheduler');
|
|
|
|
// $FlowFixMe Flow doens't know about "await act()" yet
|
|
await TestUtils.act(async () => {
|
|
callback();
|
|
|
|
// Resolve pending suspense promises
|
|
jest.runAllTimers();
|
|
});
|
|
|
|
// Run cascading microtasks and flush scheduled React work.
|
|
// Components that suspend multiple times will need to do this once per suspend operation.
|
|
// HACK Ideally the mock scheduler would provide an API to ask if there was outstanding work.
|
|
while (--numTimesToFlush >= 0) {
|
|
// $FlowFixMe Flow doens't know about "await act()" yet
|
|
await TestUtils.act(async () => {
|
|
jest.runAllTimers();
|
|
Scheduler.flushAll();
|
|
});
|
|
}
|
|
}
|
|
|
|
export function beforeEachProfiling(): void {
|
|
// Mock React's timing information so that test runs are predictable.
|
|
jest.mock('scheduler', () =>
|
|
// $FlowFixMe Flow does not konw about requireActual
|
|
require.requireActual('scheduler/unstable_mock')
|
|
);
|
|
|
|
// DevTools itself uses performance.now() to offset commit times
|
|
// so they appear relative to when profiling was started in the UI.
|
|
jest.spyOn(performance, 'now').mockImplementation(
|
|
// $FlowFixMe Flow does not konw about requireActual
|
|
require.requireActual('scheduler/unstable_mock').unstable_now
|
|
);
|
|
}
|
|
|
|
export function getRendererID(): number {
|
|
if (global.agent == null) {
|
|
throw Error('Agent unavailable.');
|
|
}
|
|
const ids = Object.keys(global.agent._rendererInterfaces);
|
|
if (ids.length !== 1) {
|
|
throw Error('Multiple renderers attached.');
|
|
}
|
|
return parseInt(ids[0], 10);
|
|
}
|
|
|
|
export function requireTestRenderer(): ReactTestRenderer {
|
|
let hook;
|
|
try {
|
|
// Hide the hook before requiring TestRenderer, so we don't end up with a loop.
|
|
hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
|
delete global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
|
|
|
return require('react-test-renderer');
|
|
} finally {
|
|
global.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook;
|
|
}
|
|
}
|