From 10e696165101a1e58c1293dc4a69ed9c8491115b Mon Sep 17 00:00:00 2001 From: Karl Dominick Cinco Date: Mon, 26 Nov 2018 13:48:48 +0800 Subject: [PATCH 1/2] refactor: Modify ByType API typings to allow required props --- typings/__tests__/index.test.tsx | 75 ++++++++++++++++++++------------ typings/index.d.ts | 14 +++--- 2 files changed, 55 insertions(+), 34 deletions(-) diff --git a/typings/__tests__/index.test.tsx b/typings/__tests__/index.test.tsx index c85827090..8a32da5e5 100644 --- a/typings/__tests__/index.test.tsx +++ b/typings/__tests__/index.test.tsx @@ -1,16 +1,23 @@ -import * as React from 'react'; -import { ReactTestInstance } from 'react-test-renderer'; +import * as React from "react"; +import { ReactTestInstance } from "react-test-renderer"; import { render, fireEvent, shallow, flushMicrotasksQueue, debug, - waitForElement, -} from '../..'; + waitForElement +} from "../.."; + +interface HasRequiredProp { + requiredProp: string; +} const View = props => props.children; const Text = props => props.children; +const ElementWithRequiredProps = (props: HasRequiredProp) => ( + {props.requiredProp} +); const TestComponent = () => ( @@ -21,64 +28,76 @@ const TestComponent = () => ( const tree = render(); // getByAPI tests -const getByNameString: ReactTestInstance = tree.getByName('View'); +const getByNameString: ReactTestInstance = tree.getByName("View"); const getByNameContructor: ReactTestInstance = tree.getByName(View); const getByType: ReactTestInstance = tree.getByType(View); -const getByTextString: ReactTestInstance = tree.getByText(''); +const getByTypeWithRequiredProps: ReactTestInstance = tree.getByType( + ElementWithRequiredProps +); +const getByTextString: ReactTestInstance = tree.getByText(""); const getByTextRegExp: ReactTestInstance = tree.getByText(/View/g); const getByProps: ReactTestInstance = tree.getByProps({ value: 2 }); -const getByTestId: ReactTestInstance = tree.getByTestId('test-id'); -const getAllByNameString: Array = tree.getAllByName('View'); +const getByTestId: ReactTestInstance = tree.getByTestId("test-id"); +const getAllByNameString: Array = tree.getAllByName("View"); const getAllByNameConstructor: Array = tree.getAllByName( View ); const getAllByType: Array = tree.getAllByType(View); +const getAllByTypeWithRequiredProps: Array< + ReactTestInstance +> = tree.getAllByType(ElementWithRequiredProps); const getAllByTextString: Array = tree.getAllByText( - '' + "" ); const getAllByTextRegExp: Array = tree.getAllByText(/Text/g); const getAllByProps: Array = tree.getAllByProps({ - value: 2, + value: 2 }); // queuryByAPI tests -const queryByNameString: ReactTestInstance | null = tree.queryByName('View'); +const queryByNameString: ReactTestInstance | null = tree.queryByName("View"); const queryByNameConstructor: ReactTestInstance | null = tree.queryByName(View); const queryByType: ReactTestInstance | null = tree.queryByType(View); +const queryByTypeWithRequiredProps: ReactTestInstance | null = tree.queryByType( + ElementWithRequiredProps +); const queryByTextString: ReactTestInstance | null = tree.queryByText( - '' + "" ); const queryByTextRegExp: ReactTestInstance | null = tree.queryByText(/View/g); const queryByProps: ReactTestInstance | null = tree.queryByProps({ value: 2 }); -const queryByTestId: ReactTestInstance | null = tree.queryByTestId('test-id'); +const queryByTestId: ReactTestInstance | null = tree.queryByTestId("test-id"); const queryAllByNameString: Array = tree.getAllByName( - 'View' + "View" ); const queryAllByNameConstructor: Array = tree.getAllByName( View ); const queryAllByType: Array = tree.getAllByType(View); +const queryAllByTypeWithRequiredProps: Array< + ReactTestInstance +> = tree.getAllByType(ElementWithRequiredProps); const queryAllByTextString: Array = tree.queryAllByText( - 'View' + "View" ); const queryAllByTextRegExp: Array = tree.queryAllByText( /View/g ); const queryAllByProps: Array = tree.getAllByProps({ - value: 2, + value: 2 }); -const debugFn = tree.debug() -const debugFnWithMessage = tree.debug('my message') +const debugFn = tree.debug(); +const debugFnWithMessage = tree.debug("my message"); tree.update(); tree.unmount(); // fireEvent API tests -fireEvent(getByNameString, 'press'); -fireEvent(getByNameString, 'press', 'data'); +fireEvent(getByNameString, "press"); +fireEvent(getByNameString, "press", "data"); fireEvent.press(getByNameString); -fireEvent.changeText(getByNameString, 'string'); -fireEvent.scroll(getByNameString, 'eventData'); +fireEvent.changeText(getByNameString, "string"); +fireEvent.scroll(getByNameString, "eventData"); // shallow API const shallowTree: { output: React.ReactElement } = shallow( @@ -89,18 +108,18 @@ const waitForFlush: Promise = flushMicrotasksQueue(); // debug API debug(); -debug(, 'message'); +debug(, "message"); debug(getByNameString); -debug(getByNameString, 'message'); +debug(getByNameString, "message"); debug.shallow(); -debug.shallow(, 'message'); +debug.shallow(, "message"); debug.deep(); -debug.deep(, 'message'); +debug.deep(, "message"); debug.deep(tree.toJSON()); const waitBy: Promise = waitForElement( - () => tree.getByName('View') + () => tree.getByName("View") ); const waitByAll: Promise> = waitForElement< Array ->(() => tree.getAllByName('View'), 1000, 50); +>(() => tree.getAllByName("View"), 1000, 50); diff --git a/typings/index.d.ts b/typings/index.d.ts index bc3ab1f04..2ea1c5ec4 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,26 +1,28 @@ -import * as React from 'react'; -import { ReactTestInstance, ReactTestRendererJSON } from 'react-test-renderer'; +import * as React from "react"; +import { ReactTestInstance, ReactTestRendererJSON } from "react-test-renderer"; export interface GetByAPI { getByName: (name: React.ReactType) => ReactTestInstance; - getByType: (type: React.ComponentType) => ReactTestInstance; + getByType:

(type: React.ComponentType

) => ReactTestInstance; getByText: (text: string | RegExp) => ReactTestInstance; getByProps: (props: Record) => ReactTestInstance; getByTestId: (testID: string) => ReactTestInstance; getAllByName: (name: React.ReactType) => Array; - getAllByType: (type: React.ComponentType) => Array; + getAllByType:

(type: React.ComponentType

) => Array; getAllByText: (text: string | RegExp) => Array; getAllByProps: (props: Record) => Array; } export interface QueryByAPI { queryByName: (name: React.ReactType) => ReactTestInstance | null; - queryByType: (type: React.ComponentType) => ReactTestInstance | null; + queryByType:

(type: React.ComponentType

) => ReactTestInstance | null; queryByText: (name: string | RegExp) => ReactTestInstance | null; queryByProps: (props: Record) => ReactTestInstance | null; queryByTestId: (testID: string) => ReactTestInstance | null; queryAllByName: (name: React.ReactType) => Array | []; - queryAllByType: (type: React.ComponentType) => Array | []; + queryAllByType:

( + type: React.ComponentType

+ ) => Array | []; queryAllByText: (text: string | RegExp) => Array | []; queryAllByProps: ( props: Record From 0f448e059b7d009f70c5b91223d7636dd693e426 Mon Sep 17 00:00:00 2001 From: Karl Dominick Cinco Date: Mon, 26 Nov 2018 13:56:45 +0800 Subject: [PATCH 2/2] refactor: Modify ByType API typings to allow required props --- typings/__tests__/index.test.tsx | 56 ++++++++++++++++---------------- typings/index.d.ts | 4 +-- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/typings/__tests__/index.test.tsx b/typings/__tests__/index.test.tsx index 8a32da5e5..4b1d744d3 100644 --- a/typings/__tests__/index.test.tsx +++ b/typings/__tests__/index.test.tsx @@ -1,13 +1,13 @@ -import * as React from "react"; -import { ReactTestInstance } from "react-test-renderer"; +import * as React from 'react'; +import { ReactTestInstance } from 'react-test-renderer'; import { render, fireEvent, shallow, flushMicrotasksQueue, debug, - waitForElement -} from "../.."; + waitForElement, +} from '../..'; interface HasRequiredProp { requiredProp: string; @@ -28,17 +28,17 @@ const TestComponent = () => ( const tree = render(); // getByAPI tests -const getByNameString: ReactTestInstance = tree.getByName("View"); +const getByNameString: ReactTestInstance = tree.getByName('View'); const getByNameContructor: ReactTestInstance = tree.getByName(View); const getByType: ReactTestInstance = tree.getByType(View); const getByTypeWithRequiredProps: ReactTestInstance = tree.getByType( ElementWithRequiredProps ); -const getByTextString: ReactTestInstance = tree.getByText(""); +const getByTextString: ReactTestInstance = tree.getByText(''); const getByTextRegExp: ReactTestInstance = tree.getByText(/View/g); const getByProps: ReactTestInstance = tree.getByProps({ value: 2 }); -const getByTestId: ReactTestInstance = tree.getByTestId("test-id"); -const getAllByNameString: Array = tree.getAllByName("View"); +const getByTestId: ReactTestInstance = tree.getByTestId('test-id'); +const getAllByNameString: Array = tree.getAllByName('View'); const getAllByNameConstructor: Array = tree.getAllByName( View ); @@ -47,28 +47,28 @@ const getAllByTypeWithRequiredProps: Array< ReactTestInstance > = tree.getAllByType(ElementWithRequiredProps); const getAllByTextString: Array = tree.getAllByText( - "" + '' ); const getAllByTextRegExp: Array = tree.getAllByText(/Text/g); const getAllByProps: Array = tree.getAllByProps({ - value: 2 + value: 2, }); // queuryByAPI tests -const queryByNameString: ReactTestInstance | null = tree.queryByName("View"); +const queryByNameString: ReactTestInstance | null = tree.queryByName('View'); const queryByNameConstructor: ReactTestInstance | null = tree.queryByName(View); const queryByType: ReactTestInstance | null = tree.queryByType(View); const queryByTypeWithRequiredProps: ReactTestInstance | null = tree.queryByType( ElementWithRequiredProps ); const queryByTextString: ReactTestInstance | null = tree.queryByText( - "" + '' ); const queryByTextRegExp: ReactTestInstance | null = tree.queryByText(/View/g); const queryByProps: ReactTestInstance | null = tree.queryByProps({ value: 2 }); -const queryByTestId: ReactTestInstance | null = tree.queryByTestId("test-id"); +const queryByTestId: ReactTestInstance | null = tree.queryByTestId('test-id'); const queryAllByNameString: Array = tree.getAllByName( - "View" + 'View' ); const queryAllByNameConstructor: Array = tree.getAllByName( View @@ -78,26 +78,26 @@ const queryAllByTypeWithRequiredProps: Array< ReactTestInstance > = tree.getAllByType(ElementWithRequiredProps); const queryAllByTextString: Array = tree.queryAllByText( - "View" + 'View' ); const queryAllByTextRegExp: Array = tree.queryAllByText( /View/g ); const queryAllByProps: Array = tree.getAllByProps({ - value: 2 + value: 2, }); -const debugFn = tree.debug(); -const debugFnWithMessage = tree.debug("my message"); +const debugFn = tree.debug() +const debugFnWithMessage = tree.debug('my message') tree.update(); tree.unmount(); // fireEvent API tests -fireEvent(getByNameString, "press"); -fireEvent(getByNameString, "press", "data"); +fireEvent(getByNameString, 'press'); +fireEvent(getByNameString, 'press', 'data'); fireEvent.press(getByNameString); -fireEvent.changeText(getByNameString, "string"); -fireEvent.scroll(getByNameString, "eventData"); +fireEvent.changeText(getByNameString, 'string'); +fireEvent.scroll(getByNameString, 'eventData'); // shallow API const shallowTree: { output: React.ReactElement } = shallow( @@ -108,18 +108,18 @@ const waitForFlush: Promise = flushMicrotasksQueue(); // debug API debug(); -debug(, "message"); +debug(, 'message'); debug(getByNameString); -debug(getByNameString, "message"); +debug(getByNameString, 'message'); debug.shallow(); -debug.shallow(, "message"); +debug.shallow(, 'message'); debug.deep(); -debug.deep(, "message"); +debug.deep(, 'message'); debug.deep(tree.toJSON()); const waitBy: Promise = waitForElement( - () => tree.getByName("View") + () => tree.getByName('View') ); const waitByAll: Promise> = waitForElement< Array ->(() => tree.getAllByName("View"), 1000, 50); +>(() => tree.getAllByName('View'), 1000, 50); diff --git a/typings/index.d.ts b/typings/index.d.ts index 2ea1c5ec4..d1ce52fda 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1,5 +1,5 @@ -import * as React from "react"; -import { ReactTestInstance, ReactTestRendererJSON } from "react-test-renderer"; +import * as React from 'react'; +import { ReactTestInstance, ReactTestRendererJSON } from 'react-test-renderer'; export interface GetByAPI { getByName: (name: React.ReactType) => ReactTestInstance;