From c203ec00e539015894649d6a0f494b5de99ab71d Mon Sep 17 00:00:00 2001 From: Logan Daniels Date: Thu, 5 Mar 2020 11:57:43 -0800 Subject: [PATCH] Add flow types for ReactTestRenderer Summary: Changelog: [Internal] Reviewed By: cpojer Differential Revision: D20260771 fbshipit-source-id: 3935e546ebbc65a1baa9d0a25cff47753f6bc590 --- Libraries/Utilities/ReactNativeTestTools.js | 25 +++++++++++++-------- jest/renderer.js | 1 + 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Libraries/Utilities/ReactNativeTestTools.js b/Libraries/Utilities/ReactNativeTestTools.js index 124437ae8ef844..a354f0b6334dd1 100644 --- a/Libraries/Utilities/ReactNativeTestTools.js +++ b/Libraries/Utilities/ReactNativeTestTools.js @@ -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; + +// $FlowFixMe - error revealed when flow-typing ReactTestRenderer +export type Predicate = (node: ReactTestInstance) => boolean; + +type $ReturnType = $Call<((...A) => Ret) => Ret, Fn>; +// $FlowFixMe - error revealed when flow-typing ReactTestRenderer +export type ReactTestRendererJSON = $ReturnType; + const { Switch, Text, @@ -26,12 +38,6 @@ const { VirtualizedList, } = require('react-native'); -import type { - ReactTestInstance, - ReactTestRendererNode, - Predicate, -} from 'react-test-renderer'; - function byClickable(): Predicate { return withMessage( node => @@ -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', ); @@ -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()); @@ -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) { @@ -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( diff --git a/jest/renderer.js b/jest/renderer.js index af6f59a36cb897..71fa17f29431ed 100644 --- a/jest/renderer.js +++ b/jest/renderer.js @@ -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 => {