Added Flow types to profiling test
This commit is contained in:
parent
c29483122c
commit
705cd9b109
|
@ -0,0 +1,81 @@
|
||||||
|
// flow-typed signature: b6bb53397d83d2d821e258cc73818d1b
|
||||||
|
// flow-typed version: 9c71eca8ef/react-test-renderer_v16.x.x/flow_>=v0.47.x
|
||||||
|
|
||||||
|
// Type definitions for react-test-renderer 16.x.x
|
||||||
|
// Ported from: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-test-renderer
|
||||||
|
|
||||||
|
type ReactComponentInstance = React$Component<any>;
|
||||||
|
|
||||||
|
type ReactTestRendererJSON = {
|
||||||
|
type: string,
|
||||||
|
props: { [propName: string]: any },
|
||||||
|
children: null | ReactTestRendererJSON[],
|
||||||
|
};
|
||||||
|
|
||||||
|
type ReactTestRendererTree = ReactTestRendererJSON & {
|
||||||
|
nodeType: 'component' | 'host',
|
||||||
|
instance: ?ReactComponentInstance,
|
||||||
|
rendered: null | ReactTestRendererTree,
|
||||||
|
};
|
||||||
|
|
||||||
|
type ReactTestInstance = {
|
||||||
|
instance: ?ReactComponentInstance,
|
||||||
|
type: string,
|
||||||
|
props: { [propName: string]: any },
|
||||||
|
parent: null | ReactTestInstance,
|
||||||
|
children: Array<ReactTestInstance | string>,
|
||||||
|
|
||||||
|
find(predicate: (node: ReactTestInstance) => boolean): ReactTestInstance,
|
||||||
|
findByType(type: React$ElementType): ReactTestInstance,
|
||||||
|
findByProps(props: { [propName: string]: any }): ReactTestInstance,
|
||||||
|
|
||||||
|
findAll(
|
||||||
|
predicate: (node: ReactTestInstance) => boolean,
|
||||||
|
options?: { deep: boolean }
|
||||||
|
): ReactTestInstance[],
|
||||||
|
findAllByType(
|
||||||
|
type: React$ElementType,
|
||||||
|
options?: { deep: boolean }
|
||||||
|
): ReactTestInstance[],
|
||||||
|
findAllByProps(
|
||||||
|
props: { [propName: string]: any },
|
||||||
|
options?: { deep: boolean }
|
||||||
|
): ReactTestInstance[],
|
||||||
|
};
|
||||||
|
|
||||||
|
type TestRendererOptions = {
|
||||||
|
createNodeMock(element: React$Element<any>): any,
|
||||||
|
};
|
||||||
|
|
||||||
|
declare module 'react-test-renderer' {
|
||||||
|
declare export type ReactTestRenderer = {
|
||||||
|
toJSON(): null | ReactTestRendererJSON,
|
||||||
|
toTree(): null | ReactTestRendererTree,
|
||||||
|
unmount(nextElement?: React$Element<any>): void,
|
||||||
|
update(nextElement: React$Element<any>): void,
|
||||||
|
getInstance(): ?ReactComponentInstance,
|
||||||
|
root: ReactTestInstance,
|
||||||
|
};
|
||||||
|
|
||||||
|
declare type Thenable = {
|
||||||
|
then(resolve: () => mixed, reject?: () => mixed): mixed,
|
||||||
|
};
|
||||||
|
|
||||||
|
declare function create(
|
||||||
|
nextElement: React$Element<any>,
|
||||||
|
options?: TestRendererOptions
|
||||||
|
): ReactTestRenderer;
|
||||||
|
|
||||||
|
declare function act(callback: () => void): Thenable;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module 'react-test-renderer/shallow' {
|
||||||
|
declare export default class ShallowRenderer {
|
||||||
|
static createRenderer(): ShallowRenderer;
|
||||||
|
getMountedInstance(): ReactTestInstance;
|
||||||
|
getRenderOutput<E: React$Element<any>>(): E;
|
||||||
|
getRenderOutput(): React$Element<any>;
|
||||||
|
render(element: React$Element<any>, context?: any): void;
|
||||||
|
unmount(): void;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +1,17 @@
|
||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
|
import type React from 'react';
|
||||||
|
import type ReactDOM from 'react-dom';
|
||||||
|
import typeof ReactTestRenderer from 'react-test-renderer';
|
||||||
|
import type Store from 'src/devtools/store';
|
||||||
|
|
||||||
describe('profiling', () => {
|
describe('profiling', () => {
|
||||||
let React;
|
let React: React;
|
||||||
let ReactDOM;
|
let ReactDOM: ReactDOM;
|
||||||
let Scheduler;
|
let Scheduler;
|
||||||
let SchedulerTracing;
|
let SchedulerTracing;
|
||||||
let TestRenderer;
|
let TestRenderer: ReactTestRenderer;
|
||||||
let store;
|
let store: Store;
|
||||||
let utils;
|
let utils;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
@ -161,17 +166,21 @@ describe('profiling', () => {
|
||||||
const rootID = store.roots[0];
|
const rootID = store.roots[0];
|
||||||
|
|
||||||
for (let index = 0; index < store.numElements; index++) {
|
for (let index = 0; index < store.numElements; index++) {
|
||||||
await utils.actSuspense(() =>
|
await utils.actSuspense(() => {
|
||||||
|
const fiberID = store.getElementIDAtIndex(index);
|
||||||
|
if (fiberID == null) {
|
||||||
|
throw Error(`Unexpected null ID for element at index ${index}`);
|
||||||
|
}
|
||||||
TestRenderer.create(
|
TestRenderer.create(
|
||||||
<React.Suspense fallback={null}>
|
<React.Suspense fallback={null}>
|
||||||
<Suspender
|
<Suspender
|
||||||
fiberID={store.getElementIDAtIndex(index)}
|
fiberID={fiberID}
|
||||||
rendererID={rendererID}
|
rendererID={rendererID}
|
||||||
rootID={rootID}
|
rootID={rootID}
|
||||||
/>
|
/>
|
||||||
</React.Suspense>
|
</React.Suspense>
|
||||||
)
|
);
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
done();
|
done();
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
|
import typeof ReactTestRenderer from 'react-test-renderer';
|
||||||
|
|
||||||
export function act(callback: Function): void {
|
export function act(callback: Function): void {
|
||||||
const TestUtils = require('react-dom/test-utils');
|
const TestUtils = require('react-dom/test-utils');
|
||||||
TestUtils.act(() => {
|
TestUtils.act(() => {
|
||||||
|
@ -10,7 +12,7 @@ export function act(callback: Function): void {
|
||||||
jest.runAllTimers();
|
jest.runAllTimers();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function actSuspense(callback: Function) {
|
export async function actSuspense(callback: Function): Promise<void> {
|
||||||
const TestUtils = require('react-dom/test-utils');
|
const TestUtils = require('react-dom/test-utils');
|
||||||
const Scheduler = require('scheduler');
|
const Scheduler = require('scheduler');
|
||||||
|
|
||||||
|
@ -26,7 +28,7 @@ export async function actSuspense(callback: Function) {
|
||||||
Scheduler.flushAll();
|
Scheduler.flushAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function beforeEachProfiling() {
|
export function beforeEachProfiling(): void {
|
||||||
// Mock React's timing information so that test runs are predictable.
|
// Mock React's timing information so that test runs are predictable.
|
||||||
jest.mock('scheduler', () =>
|
jest.mock('scheduler', () =>
|
||||||
// $FlowFixMe Flow does not konw about requireActual
|
// $FlowFixMe Flow does not konw about requireActual
|
||||||
|
@ -41,7 +43,7 @@ export function beforeEachProfiling() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRendererID() {
|
export function getRendererID(): number {
|
||||||
if (global.agent == null) {
|
if (global.agent == null) {
|
||||||
throw Error('Agent unavailable.');
|
throw Error('Agent unavailable.');
|
||||||
}
|
}
|
||||||
|
@ -49,10 +51,10 @@ export function getRendererID() {
|
||||||
if (ids.length !== 1) {
|
if (ids.length !== 1) {
|
||||||
throw Error('Multiple renderers attached.');
|
throw Error('Multiple renderers attached.');
|
||||||
}
|
}
|
||||||
return ids[0];
|
return parseInt(ids[0], 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function requireTestRenderer() {
|
export function requireTestRenderer(): ReactTestRenderer {
|
||||||
let hook;
|
let hook;
|
||||||
try {
|
try {
|
||||||
// Hide the hook before requiring TestRenderer, so we don't end up with a loop.
|
// Hide the hook before requiring TestRenderer, so we don't end up with a loop.
|
||||||
|
|
Loading…
Reference in New Issue