diff --git a/flow-typed/npm/react-test-renderer_v16.x.x.js b/flow-typed/npm/react-test-renderer_v16.x.x.js new file mode 100644 index 0000000000..87a149a1d3 --- /dev/null +++ b/flow-typed/npm/react-test-renderer_v16.x.x.js @@ -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; + +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, + + 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, +}; + +declare module 'react-test-renderer' { + declare export type ReactTestRenderer = { + toJSON(): null | ReactTestRendererJSON, + toTree(): null | ReactTestRendererTree, + unmount(nextElement?: React$Element): void, + update(nextElement: React$Element): void, + getInstance(): ?ReactComponentInstance, + root: ReactTestInstance, + }; + + declare type Thenable = { + then(resolve: () => mixed, reject?: () => mixed): mixed, + }; + + declare function create( + nextElement: React$Element, + 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; + getRenderOutput(): React$Element; + render(element: React$Element, context?: any): void; + unmount(): void; + } +} diff --git a/src/__tests__/profiling-test.js b/src/__tests__/profiling-test.js index de2d4cf04c..5bd2e037bb 100644 --- a/src/__tests__/profiling-test.js +++ b/src/__tests__/profiling-test.js @@ -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( - ) - ); + ); + }); } done(); diff --git a/src/__tests__/utils.js b/src/__tests__/utils.js index cb15590ee5..1e3acc55ac 100644 --- a/src/__tests__/utils.js +++ b/src/__tests__/utils.js @@ -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 { 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.