Skip to content

fix: modify *ByType API TS typings to allow required props #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions typings/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import {
waitForElement,
} from '../..';

interface HasRequiredProp {
requiredProp: string;
}

const View = props => props.children;
const Text = props => props.children;
const ElementWithRequiredProps = (props: HasRequiredProp) => (
<Text>{props.requiredProp}</Text>
);

const TestComponent = () => (
<View>
Expand All @@ -24,6 +31,9 @@ const tree = render(<TestComponent />);
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('<View />');
const getByTextRegExp: ReactTestInstance = tree.getByText(/View/g);
const getByProps: ReactTestInstance = tree.getByProps({ value: 2 });
Expand All @@ -33,6 +43,9 @@ const getAllByNameConstructor: Array<ReactTestInstance> = tree.getAllByName(
View
);
const getAllByType: Array<ReactTestInstance> = tree.getAllByType(View);
const getAllByTypeWithRequiredProps: Array<
ReactTestInstance
> = tree.getAllByType(ElementWithRequiredProps);
const getAllByTextString: Array<ReactTestInstance> = tree.getAllByText(
'<View />'
);
Expand All @@ -45,6 +58,9 @@ const getAllByProps: Array<ReactTestInstance> = tree.getAllByProps({
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(
'<View />'
);
Expand All @@ -58,6 +74,9 @@ const queryAllByNameConstructor: Array<ReactTestInstance> = tree.getAllByName(
View
);
const queryAllByType: Array<ReactTestInstance> = tree.getAllByType(View);
const queryAllByTypeWithRequiredProps: Array<
ReactTestInstance
> = tree.getAllByType(ElementWithRequiredProps);
const queryAllByTextString: Array<ReactTestInstance> = tree.queryAllByText(
'View'
);
Expand Down
10 changes: 6 additions & 4 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ import { ReactTestInstance, ReactTestRendererJSON } from 'react-test-renderer';

export interface GetByAPI {
getByName: (name: React.ReactType) => ReactTestInstance;
getByType: (type: React.ComponentType) => ReactTestInstance;
getByType: <P>(type: React.ComponentType<P>) => ReactTestInstance;
getByText: (text: string | RegExp) => ReactTestInstance;
getByProps: (props: Record<string, any>) => ReactTestInstance;
getByTestId: (testID: string) => ReactTestInstance;
getAllByName: (name: React.ReactType) => Array<ReactTestInstance>;
getAllByType: (type: React.ComponentType) => Array<ReactTestInstance>;
getAllByType: <P>(type: React.ComponentType<P>) => Array<ReactTestInstance>;
getAllByText: (text: string | RegExp) => Array<ReactTestInstance>;
getAllByProps: (props: Record<string, any>) => Array<ReactTestInstance>;
}

export interface QueryByAPI {
queryByName: (name: React.ReactType) => ReactTestInstance | null;
queryByType: (type: React.ComponentType) => ReactTestInstance | null;
queryByType: <P>(type: React.ComponentType<P>) => ReactTestInstance | null;
queryByText: (name: string | RegExp) => ReactTestInstance | null;
queryByProps: (props: Record<string, any>) => ReactTestInstance | null;
queryByTestId: (testID: string) => ReactTestInstance | null;
queryAllByName: (name: React.ReactType) => Array<ReactTestInstance> | [];
queryAllByType: (type: React.ComponentType) => Array<ReactTestInstance> | [];
queryAllByType: <P>(
type: React.ComponentType<P>
) => Array<ReactTestInstance> | [];
queryAllByText: (text: string | RegExp) => Array<ReactTestInstance> | [];
queryAllByProps: (
props: Record<string, any>
Expand Down