Added Flow types to profiling test

This commit is contained in:
Brian Vaughn 2019-05-06 09:44:01 -07:00
parent c29483122c
commit 705cd9b109
3 changed files with 105 additions and 13 deletions

View File

@ -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;
}
}

View File

@ -1,12 +1,17 @@
// @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', () => {
let React;
let ReactDOM;
let React: React;
let ReactDOM: ReactDOM;
let Scheduler;
let SchedulerTracing;
let TestRenderer;
let store;
let TestRenderer: ReactTestRenderer;
let store: Store;
let utils;
beforeEach(() => {
@ -161,17 +166,21 @@ describe('profiling', () => {
const rootID = store.roots[0];
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(
<React.Suspense fallback={null}>
<Suspender
fiberID={store.getElementIDAtIndex(index)}
fiberID={fiberID}
rendererID={rendererID}
rootID={rootID}
/>
</React.Suspense>
)
);
);
});
}
done();

View File

@ -1,5 +1,7 @@
// @flow
import typeof ReactTestRenderer from 'react-test-renderer';
export function act(callback: Function): void {
const TestUtils = require('react-dom/test-utils');
TestUtils.act(() => {
@ -10,7 +12,7 @@ export function act(callback: Function): void {
jest.runAllTimers();
}
export async function actSuspense(callback: Function) {
export async function actSuspense(callback: Function): Promise<void> {
const TestUtils = require('react-dom/test-utils');
const Scheduler = require('scheduler');
@ -26,7 +28,7 @@ export async function actSuspense(callback: Function) {
Scheduler.flushAll();
}
export function beforeEachProfiling() {
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
@ -41,7 +43,7 @@ export function beforeEachProfiling() {
);
}
export function getRendererID() {
export function getRendererID(): number {
if (global.agent == null) {
throw Error('Agent unavailable.');
}
@ -49,10 +51,10 @@ export function getRendererID() {
if (ids.length !== 1) {
throw Error('Multiple renderers attached.');
}
return ids[0];
return parseInt(ids[0], 10);
}
export function requireTestRenderer() {
export function requireTestRenderer(): ReactTestRenderer {
let hook;
try {
// Hide the hook before requiring TestRenderer, so we don't end up with a loop.