Skip to content

Commit

Permalink
Add flow types for ReactTestRenderer
Browse files Browse the repository at this point in the history
Summary: Changelog: [Internal]

Reviewed By: cpojer

Differential Revision: D20260771

fbshipit-source-id: 3935e546ebbc65a1baa9d0a25cff47753f6bc590
  • Loading branch information
logandaniels authored and facebook-github-bot committed Mar 5, 2020
1 parent d1018e0 commit c203ec0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
25 changes: 16 additions & 9 deletions Libraries/Utilities/ReactNativeTestTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ const React = require('react');

const ReactTestRenderer = require('react-test-renderer');
const ShallowRenderer = require('react-test-renderer/shallow');
// $FlowFixMe - error revealed when flow-typing ReactTestRenderer
const shallowRenderer = new ShallowRenderer();

import type {ReactTestRenderer as ReactTestRendererType} from 'react-test-renderer';

export type ReactTestInstance = $PropertyType<ReactTestRendererType, 'root'>;

// $FlowFixMe - error revealed when flow-typing ReactTestRenderer
export type Predicate = (node: ReactTestInstance) => boolean;

type $ReturnType<Fn> = $Call<<Ret, A>((...A) => Ret) => Ret, Fn>;
// $FlowFixMe - error revealed when flow-typing ReactTestRenderer
export type ReactTestRendererJSON = $ReturnType<ReactTestRenderer.create.toJSON>;

const {
Switch,
Text,
Expand All @@ -26,12 +38,6 @@ const {
VirtualizedList,
} = require('react-native');

import type {
ReactTestInstance,
ReactTestRendererNode,
Predicate,
} from 'react-test-renderer';

function byClickable(): Predicate {
return withMessage(
node =>
Expand All @@ -48,6 +54,7 @@ function byClickable(): Predicate {
node.instance?.state?.pressability != null ||
// TODO: Remove this after deleting `Touchable`.
(node.instance &&
// $FlowFixMe - error revealed when flow-typing ReactTestRenderer
typeof node.instance.touchableHandlePress === 'function'),
'is clickable',
);
Expand Down Expand Up @@ -75,7 +82,7 @@ function enter(instance: ReactTestInstance, text: string) {

// Returns null if there is no error, otherwise returns an error message string.
function maximumDepthError(
tree: {toJSON: () => ReactTestRendererNode, ...},
tree: ReactTestRendererType,
maxDepthLimit: number,
): ?string {
const maxDepth = maximumDepthOfJSON(tree.toJSON());
Expand Down Expand Up @@ -147,7 +154,7 @@ function expectRendersMatchingSnapshot(
}

// Takes a node from toJSON()
function maximumDepthOfJSON(node: ReactTestRendererNode): number {
function maximumDepthOfJSON(node: ?ReactTestRendererJSON): number {
if (node == null) {
return 0;
} else if (typeof node === 'string' || node.children == null) {
Expand All @@ -166,7 +173,7 @@ function renderAndEnforceStrictMode(element: React.Node): any {
return renderWithStrictMode(element);
}

function renderWithStrictMode(element: React.Node): any {
function renderWithStrictMode(element: React.Node): ReactTestRendererType {
const WorkAroundBugWithStrictModeInTestRenderer = prps => prps.children;
const StrictMode = (React: $FlowFixMe).StrictMode;
return ReactTestRenderer.create(
Expand Down
1 change: 1 addition & 0 deletions jest/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const React = require('react');
const ShallowRenderer = require('react-test-renderer/shallow');
const TestRenderer = require('react-test-renderer');

// $FlowFixMe - error revealed when flow-typing ReactTestRenderer
const renderer = new ShallowRenderer();

export const shallow = (Component: React.Element<any>): any => {
Expand Down

0 comments on commit c203ec0

Please sign in to comment.