react/packages/react-devtools-shared/src/__tests__/utils.js

204 lines
5.5 KiB
JavaScript
Raw Normal View History

// @flow
2019-05-07 00:44:01 +08:00
import typeof ReactTestRenderer from 'react-test-renderer';
2019-08-14 08:58:03 +08:00
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
2019-08-14 06:59:43 +08:00
import type Store from 'react-devtools-shared/src/devtools/store';
2019-08-14 08:58:03 +08:00
import type {ProfilingDataFrontend} from 'react-devtools-shared/src/devtools/views/Profiler/types';
import type {ElementType} from 'react-devtools-shared/src/types';
export function act(callback: Function): void {
2019-08-14 08:58:03 +08:00
const {act: actTestRenderer} = require('react-test-renderer');
const {act: actDOM} = require('react-dom/test-utils');
actDOM(() => {
actTestRenderer(() => {
callback();
});
});
// Flush Bridge operations
actDOM(() => {
actTestRenderer(() => {
jest.runAllTimers();
});
2019-05-29 03:24:35 +08:00
});
}
2019-05-10 23:27:14 +08:00
export async function actAsync(
2019-05-29 04:57:05 +08:00
cb: () => *,
2019-08-14 08:58:03 +08:00
recursivelyFlush: boolean = true,
2019-05-07 04:37:06 +08:00
): Promise<void> {
2019-08-14 08:58:03 +08:00
const {act: actTestRenderer} = require('react-test-renderer');
const {act: actDOM} = require('react-dom/test-utils');
// $FlowFixMe Flow doens't know about "await act()" yet
await actDOM(async () => {
await actTestRenderer(async () => {
await cb();
});
});
2019-05-29 04:57:05 +08:00
if (recursivelyFlush) {
// TODO (Jest v24) Replace with jest.getTimerCount() after we upgrade Jest
while (global.mockGetTimersCount() > 0) {
2019-05-29 04:57:05 +08:00
// $FlowFixMe Flow doens't know about "await act()" yet
await actDOM(async () => {
await actTestRenderer(async () => {
jest.runAllTimers();
});
2019-05-29 04:57:05 +08:00
});
}
} else {
// $FlowFixMe Flow doesn't know about "await act()" yet
await actDOM(async () => {
await actTestRenderer(async () => {
jest.runOnlyPendingTimers();
});
2019-05-07 04:37:06 +08:00
});
}
}
2019-05-07 00:44:01 +08:00
export function beforeEachProfiling(): void {
// Mock React's timing information so that test runs are predictable.
2019-05-26 00:10:43 +08:00
jest.mock('scheduler', () => jest.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.
2019-05-26 00:10:43 +08:00
jest
.spyOn(performance, 'now')
.mockImplementation(
2019-08-14 08:58:03 +08:00
jest.requireActual('scheduler/unstable_mock').unstable_now,
2019-05-26 00:10:43 +08:00
);
}
2019-05-10 05:21:22 +08:00
export function createDisplayNameFilter(
source: string,
2019-08-14 08:58:03 +08:00
isEnabled: boolean = true,
2019-05-10 05:21:22 +08:00
) {
2019-08-14 06:59:43 +08:00
const Types = require('react-devtools-shared/src/types');
2019-05-10 05:21:22 +08:00
let isValid = true;
try {
2019-08-14 12:59:07 +08:00
new RegExp(source); // eslint-disable-line no-new
2019-05-10 05:21:22 +08:00
} catch (error) {
isValid = false;
}
return {
type: Types.ComponentFilterDisplayName,
isEnabled,
isValid,
value: source,
};
}
2019-06-08 05:06:08 +08:00
export function createHOCFilter(isEnabled: boolean = true) {
2019-08-14 06:59:43 +08:00
const Types = require('react-devtools-shared/src/types');
2019-06-08 05:06:08 +08:00
return {
type: Types.ComponentFilterHOC,
isEnabled,
isValid: true,
};
}
export function createElementTypeFilter(
elementType: ElementType,
2019-08-14 08:58:03 +08:00
isEnabled: boolean = true,
2019-06-08 05:06:08 +08:00
) {
2019-08-14 06:59:43 +08:00
const Types = require('react-devtools-shared/src/types');
2019-06-08 05:06:08 +08:00
return {
type: Types.ComponentFilterElementType,
isEnabled,
value: elementType,
};
}
2019-05-10 05:21:22 +08:00
export function createLocationFilter(
source: string,
2019-08-14 08:58:03 +08:00
isEnabled: boolean = true,
2019-05-10 05:21:22 +08:00
) {
2019-08-14 06:59:43 +08:00
const Types = require('react-devtools-shared/src/types');
2019-05-10 05:21:22 +08:00
let isValid = true;
try {
2019-08-14 12:59:07 +08:00
new RegExp(source); // eslint-disable-line no-new
2019-05-10 05:21:22 +08:00
} catch (error) {
isValid = false;
}
return {
type: Types.ComponentFilterLocation,
isEnabled,
isValid,
value: source,
};
}
2019-05-07 00:44:01 +08:00
export function getRendererID(): number {
if (global.agent == null) {
throw Error('Agent unavailable.');
}
const ids = Object.keys(global.agent._rendererInterfaces);
2019-08-14 12:59:07 +08:00
const id = ids.find(innerID => {
const rendererInterface = global.agent._rendererInterfaces[innerID];
return rendererInterface.renderer.rendererPackageName === 'react-dom';
});
if (ids == null) {
throw Error('Could not find renderer.');
}
return parseInt(id, 10);
}
2019-05-07 00:44:01 +08:00
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;
}
}
export function exportImportHelper(bridge: FrontendBridge, store: Store): void {
const {
prepareProfilingDataExport,
prepareProfilingDataFrontendFromExport,
2019-08-14 06:59:43 +08:00
} = require('react-devtools-shared/src/devtools/views/Profiler/utils');
2019-08-14 08:58:03 +08:00
const {profilerStore} = store;
expect(profilerStore.profilingData).not.toBeNull();
const profilingDataFrontendInitial = ((profilerStore.profilingData: any): ProfilingDataFrontend);
const profilingDataExport = prepareProfilingDataExport(
2019-08-14 08:58:03 +08:00
profilingDataFrontendInitial,
);
// Simulate writing/reading to disk.
const serializedProfilingDataExport = JSON.stringify(
profilingDataExport,
null,
2019-08-14 08:58:03 +08:00
2,
);
const parsedProfilingDataExport = JSON.parse(serializedProfilingDataExport);
const profilingDataFrontend = prepareProfilingDataFrontendFromExport(
2019-08-14 08:58:03 +08:00
(parsedProfilingDataExport: any),
);
// Sanity check that profiling snapshots are serialized correctly.
expect(profilingDataFrontendInitial).toEqual(profilingDataFrontend);
// Snapshot the JSON-parsed object, rather than the raw string, because Jest formats the diff nicer.
2019-05-22 23:05:34 +08:00
expect(parsedProfilingDataExport).toMatchSnapshot('imported data');
2019-05-15 02:01:41 +08:00
act(() => {
// Apply the new exported-then-reimported data so tests can re-run assertions.
profilerStore.profilingData = profilingDataFrontend;
});
}