From cb5ee9a06072df4d681dcb14a2e674e18f45319a Mon Sep 17 00:00:00 2001 From: AbigailMcP Date: Mon, 4 Nov 2019 15:55:58 +0000 Subject: [PATCH] [Cavy] Add `wrap` type (#39972) * Add wrap type and update findComponent type * Flesh out wrap tests * Fix findComponent type * Revert Typescript version change --- types/cavy/cavy-tests.tsx | 58 ++++++++++++++++++++++++++++++++------- types/cavy/index.d.ts | 8 ++++-- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/types/cavy/cavy-tests.tsx b/types/cavy/cavy-tests.tsx index 06826e5195..5b7c9af0ee 100644 --- a/types/cavy/cavy-tests.tsx +++ b/types/cavy/cavy-tests.tsx @@ -1,12 +1,32 @@ import * as React from 'react'; import { TextInput, View, Text } from 'react-native'; -import { hook, TestScope, WithTestHook, Tester, TestHookStore, useCavy, TestReport } from 'cavy'; +import { + hook, + wrap, + TestScope, + WithTestHook, + Tester, + TestHookStore, + useCavy, + TestReport +} from 'cavy'; type Props = WithTestHook<{ foo: string; }>; +interface FCProps { + text: string; +} + +const FunctionComponent: React.FunctionComponent = ({ text }) => { + return {text}; +}; + +const WrappedText = wrap(Text); +const WrappedFunctionComponent = wrap(FunctionComponent); + class SampleComponent extends React.Component { textInputRef: React.ReactNode | null; @@ -20,10 +40,21 @@ class SampleComponent extends React.Component { } render() { + const { generateTestHook, foo } = this.props; return ( - - {this.props.foo} - + + + + + Wrapped text + + + {foo} + + ); } @@ -34,7 +65,7 @@ const HookedSampleComponent = hook(SampleComponent); // $ExpectType ComponentCla const SampleFunctionComponent: React.FunctionComponent = () => { const generateTestHook = useCavy(); const ref = React.createRef(); - return ; + return ; }; function sampleSpec(spec: TestScope) { @@ -44,13 +75,20 @@ function sampleSpec(spec: TestScope) { spec.it('it has a name and callback', async () => { spec.component.reRender(); // $ExpectType void spec.component.clearAsync(); // $ExpectType Promise - spec.findComponent('View.Sample'); // $ExpectType Promise> - spec.press('View.Sample'); // $ExpectType Promise - spec.fillIn('Input.Sample', "hello world"); // $ExpectType Promise + spec.findComponent('View'); // $ExpectType Promise> + spec.press('View'); // $ExpectType Promise + spec.fillIn('Input', 'hello world'); // $ExpectType Promise spec.pause(1000); // $ExpectType Promise - spec.exists('View.Sample'); // $ExpectType Promise + spec.exists('View'); // $ExpectType Promise spec.notExists('View.MissingSample'); // $ExpectType Promise - spec.containsText('Input.Sample', "hello"); // $ExpectType Promise + spec.containsText('Input', 'hello'); // $ExpectType Promise + // Test wrapped object like Text + spec.containsText('WrappedText', 'Wrapped text'); + // Test wrapped function component, access to props + const functionComponent = await spec.findComponent('WrappedFunctionComponent') as React.Component; + if (functionComponent.props.text !== 'text') { + throw new Error(); + } }); }); } diff --git a/types/cavy/index.d.ts b/types/cavy/index.d.ts index df5480c9a0..cbe829f72f 100644 --- a/types/cavy/index.d.ts +++ b/types/cavy/index.d.ts @@ -7,21 +7,25 @@ import * as React from 'react'; +// Turn off automatic exporting by exporting {}. export {}; type RefCallback = (element: React.ReactNode | null) => void; type TestHookGeneratorWithRefCallback = (label: string, ref?: RefCallback) => RefCallback; + type TestHookGeneratorWithRefObject = (label: string, ref?: React.RefObject) => React.RefObject; export type TestHookGenerator = TestHookGeneratorWithRefCallback & TestHookGeneratorWithRefObject; -export type WithTestHook = T & { generateTestHook: TestHookGenerator }; +export type WithTestHook

= P & { generateTestHook: TestHookGenerator }; -export function hook(component: React.ComponentClass>): React.ComponentClass; +export function hook

(WrappedComponent: React.ComponentClass>): React.ComponentClass

; export function useCavy(): TestHookGenerator; +export function wrap

(WrappedComponent: {} | React.FunctionComponent

): React.ComponentClass

; + export interface TesterProps { store: TestHookStore; specs: Array<(spec: TestScope) => void>;