Added Flow types to profiling test
Brian Vaughn committed
May 6, 2019 at 09:44 UTC
705cd9b10993fab6d05844f2a7d527d555dbeea6
3 files changed
+105
-13
flow-typed/npm/react-test-renderer_v16.x.x.js
new
+81
@@ -0,0 +1,81 @@
1
+// flow-typed signature: b6bb53397d83d2d821e258cc73818d1b
2
+// flow-typed version: 9c71eca8ef/react-test-renderer_v16.x.x/flow_>=v0.47.x
3
+
4
+// Type definitions for react-test-renderer 16.x.x
5
+// Ported from: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-test-renderer
6
+
7
+type ReactComponentInstance = React$Component<any>;
8
+
9
+type ReactTestRendererJSON = {
10
+ type: string,
11
+ props: { [propName: string]: any },
12
+ children: null | ReactTestRendererJSON[],
13
+};
14
+
15
+type ReactTestRendererTree = ReactTestRendererJSON & {
16
+ nodeType: 'component' | 'host',
17
+ instance: ?ReactComponentInstance,
18
+ rendered: null | ReactTestRendererTree,
19
+};
20
+
21
+type ReactTestInstance = {
22
+ instance: ?ReactComponentInstance,
23
+ type: string,
24
+ props: { [propName: string]: any },
25
+ parent: null | ReactTestInstance,
26
+ children: Array<ReactTestInstance | string>,
27
+
28
+ find(predicate: (node: ReactTestInstance) => boolean): ReactTestInstance,
29
+ findByType(type: React$ElementType): ReactTestInstance,
30
+ findByProps(props: { [propName: string]: any }): ReactTestInstance,
31
+
32
+ findAll(
33
+ predicate: (node: ReactTestInstance) => boolean,
34
+ options?: { deep: boolean }
35
+ ): ReactTestInstance[],
36
+ findAllByType(
37
+ type: React$ElementType,
38
+ options?: { deep: boolean }
39
+ ): ReactTestInstance[],
40
+ findAllByProps(
41
+ props: { [propName: string]: any },
42
+ options?: { deep: boolean }
43
+ ): ReactTestInstance[],
44
+};
45
+
46
+type TestRendererOptions = {
47
+ createNodeMock(element: React$Element<any>): any,
48
+};
49
+
50
+declare module 'react-test-renderer' {
51
+ declare export type ReactTestRenderer = {
52
+ toJSON(): null | ReactTestRendererJSON,
53
+ toTree(): null | ReactTestRendererTree,
54
+ unmount(nextElement?: React$Element<any>): void,
55
+ update(nextElement: React$Element<any>): void,
56
+ getInstance(): ?ReactComponentInstance,
57
+ root: ReactTestInstance,
58
+ };
59
+
60
+ declare type Thenable = {
61
+ then(resolve: () => mixed, reject?: () => mixed): mixed,
62
+ };
63
+
64
+ declare function create(
65
+ nextElement: React$Element<any>,
66
+ options?: TestRendererOptions
67
+ ): ReactTestRenderer;
68
+
69
+ declare function act(callback: () => void): Thenable;
70
+}
71
+
72
+declare module 'react-test-renderer/shallow' {
73
+ declare export default class ShallowRenderer {
74
+ static createRenderer(): ShallowRenderer;
75
+ getMountedInstance(): ReactTestInstance;
76
+ getRenderOutput<E: React$Element<any>>(): E;
77
+ getRenderOutput(): React$Element<any>;
78
+ render(element: React$Element<any>, context?: any): void;
79
+ unmount(): void;
80
+ }
81
+}
src/__tests__/profiling-test.js
+17
-8
@@ -1,12 +1,17 @@
1
// @flow
2
3
+import type React from 'react';
4
+import type ReactDOM from 'react-dom';
5
+import typeof ReactTestRenderer from 'react-test-renderer';
6
+import type Store from 'src/devtools/store';
7
+
8
describe('profiling', () => {
4
- let React;
5
- let ReactDOM;
9
+ let React: React;
10
+ let ReactDOM: ReactDOM;
11
let Scheduler;
12
let SchedulerTracing;
8
- let TestRenderer;
9
- let store;
13
+ let TestRenderer: ReactTestRenderer;
14
+ let store: Store;
15
let utils;
16
17
beforeEach(() => {
@@ -161,17 +166,21 @@ describe('profiling', () => {
166
const rootID = store.roots[0];
167
168
for (let index = 0; index < store.numElements; index++) {
164
- await utils.actSuspense(() =>
169
+ await utils.actSuspense(() => {
170
+ const fiberID = store.getElementIDAtIndex(index);
171
+ if (fiberID == null) {
172
+ throw Error(`Unexpected null ID for element at index ${index}`);
173
+ }
174
TestRenderer.create(
175
<React.Suspense fallback={null}>
176
<Suspender
168
- fiberID={store.getElementIDAtIndex(index)}
177
+ fiberID={fiberID}
178
rendererID={rendererID}
179
rootID={rootID}
180
/>
181
</React.Suspense>
173
- )
174
- );
182
+ );
183
+ });
184
}
185
186
done();
src/__tests__/utils.js
+7
-5
@@ -1,5 +1,7 @@
1
// @flow
2
3
+import typeof ReactTestRenderer from 'react-test-renderer';
4
+
5
export function act(callback: Function): void {
6
const TestUtils = require('react-dom/test-utils');
7
TestUtils.act(() => {
@@ -10,7 +12,7 @@ export function act(callback: Function): void {
12
jest.runAllTimers();
13
}
14
13
-export async function actSuspense(callback: Function) {
15
+export async function actSuspense(callback: Function): Promise<void> {
16
const TestUtils = require('react-dom/test-utils');
17
const Scheduler = require('scheduler');
18
@@ -26,7 +28,7 @@ export async function actSuspense(callback: Function) {
28
Scheduler.flushAll();
29
}
30
29
-export function beforeEachProfiling() {
31
+export function beforeEachProfiling(): void {
32
// Mock React's timing information so that test runs are predictable.
33
jest.mock('scheduler', () =>
34
// $FlowFixMe Flow does not konw about requireActual
@@ -41,7 +43,7 @@ export function beforeEachProfiling() {
43
);
44
}
45
44
-export function getRendererID() {
46
+export function getRendererID(): number {
47
if (global.agent == null) {
48
throw Error('Agent unavailable.');
49
}
@@ -49,10 +51,10 @@ export function getRendererID() {
51
if (ids.length !== 1) {
52
throw Error('Multiple renderers attached.');
53
}
52
- return ids[0];
54
+ return parseInt(ids[0], 10);
55
}
56
55
-export function requireTestRenderer() {
57
+export function requireTestRenderer(): ReactTestRenderer {
58
let hook;
59
try {
60
// Hide the hook before requiring TestRenderer, so we don't end up with a loop.