From 4fb371b53a614604a78f29be48e79604cdfbc7a0 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Sun, 6 Jun 2021 17:07:43 -0700 Subject: [PATCH] RN: Add `react-test-renderer` Flow Definition Summary: Adds the `react-test-renderer` Flow definition (via `flow-typed`) so it can be used from tests with Flow enabled. Changelog: [Internal] Reviewed By: TheSavior Differential Revision: D28922375 fbshipit-source-id: 9b1be69f1b281bb3c248ca272cf2527baf154e2a --- .eslintrc | 8 ++ flow-typed/npm/react-test-renderer_v16.x.x.js | 79 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 flow-typed/npm/react-test-renderer_v16.x.x.js diff --git a/.eslintrc b/.eslintrc index 2c5a51053d8..f990717831d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -21,6 +21,14 @@ "@react-native/codegen/react-native-modules": 2 } }, + { + "files": [ + "flow-typed/**/*.js", + ], + "rules": { + quotes: 0 + } + }, { "files": [ "**/__fixtures__/**/*.js", 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 00000000000..44dc3e2c71f --- /dev/null +++ b/flow-typed/npm/react-test-renderer_v16.x.x.js @@ -0,0 +1,79 @@ +// flow-typed signature: 7bac6c05f7415881918d3d510109e739 +// flow-typed version: fce74493f0/react-test-renderer_v16.x.x/flow_>=v0.104.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 | Promise): 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; + } +}