feat Add Typescript types (#18)

This commit is contained in:
Kacper Wiszczuk
2018-10-07 22:32:12 +02:00
committed by GitHub
parent 0ce1d7ec47
commit b6569a6e5d
5 changed files with 184 additions and 1 deletions
+79
View File
@@ -0,0 +1,79 @@
import * as React from 'react';
import { ReactTestInstance } from 'react-test-renderer';
import { render, fireEvent, shallow, flushMicrotasksQueue, debug } from '../..';
const View = props => props.children;
const Text = props => props.children;
const TestComponent = () => (
<View>
<Text>Test component</Text>
</View>
);
const tree = render(<TestComponent />);
// getByAPI tests
const getByNameString: ReactTestInstance = tree.getByName('View');
const getByNameContructor: ReactTestInstance = tree.getByName(View);
const getByTextString: ReactTestInstance = tree.getByText('<View />');
const getByTextRegExp: ReactTestInstance = tree.getByText(/View/g);
const getByProps: ReactTestInstance = tree.getByProps({ value: 2 });
const getByTestId: ReactTestInstance = tree.getByTestId('test-id');
const getAllByNameString: Array<ReactTestInstance> = tree.getAllByName('View');
const getAllByNameConstructor: Array<ReactTestInstance> = tree.getAllByName(
View
);
const getAllByTextString: Array<ReactTestInstance> = tree.getAllByText(
'<View />'
);
const getAllByTextRegExp: Array<ReactTestInstance> = tree.getAllByText(/Text/g);
const getAllByProps: Array<ReactTestInstance> = tree.getAllByProps({
value: 2,
});
// queuryByAPI tests
const queryByNameString: ReactTestInstance | null = tree.queryByName('View');
const queryByNameConstructor: ReactTestInstance | null = tree.queryByName(View);
const queryByTextString: ReactTestInstance | null = tree.queryByText(
'<View />'
);
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 queryAllByNameString: Array<ReactTestInstance> = tree.getAllByName(
'View'
);
const queryAllByNameConstructor: Array<ReactTestInstance> = tree.getAllByName(
View
);
const queryAllByTextString: Array<ReactTestInstance> = tree.queryAllByText(
'View'
);
const queryAllByTextRegExp: Array<ReactTestInstance> = tree.queryAllByText(
/View/g
);
const queryAllByProps: Array<ReactTestInstance> = tree.getAllByProps({
value: 2,
});
tree.update(<View />);
tree.unmount();
// fireEvent API tests
fireEvent(getByNameString, 'press');
fireEvent(getByNameString, 'press', 'data');
fireEvent.press(getByNameString);
fireEvent.doublePress(getByNameString);
fireEvent.changeText(getByNameString, 'string');
fireEvent.scroll(getByNameString, 'eventData');
// shallow API
const shallowTree: { output: React.ReactElement<any> } = shallow(
<TestComponent />
);
const waitForFlush: Promise<any> = flushMicrotasksQueue();
debug(<TestComponent />);
debug(getByNameString);
+59
View File
@@ -0,0 +1,59 @@
import * as React from 'react';
import { ReactTestInstance } from 'react-test-renderer';
export interface GetByAPI {
getByName: (name: React.ReactType) => ReactTestInstance;
getByText: (text: string | RegExp) => ReactTestInstance;
getByProps: (props: Record<string, any>) => ReactTestInstance;
getByTestId: (testID: string) => ReactTestInstance;
getAllByName: (name: React.ReactType) => Array<ReactTestInstance>;
getAllByText: (text: string | RegExp) => Array<ReactTestInstance>;
getAllByProps: (props: Record<string, any>) => Array<ReactTestInstance>;
}
export interface QueryByAPI {
queryByName: (name: React.ReactType) => 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> | [];
queryAllByText: (text: string | RegExp) => Array<ReactTestInstance> | [];
queryAllByProps: (
props: Record<string, any>
) => Array<ReactTestInstance> | [];
}
export interface RenderOptions {
createNodeMock: (element: React.ReactElement<any>) => any;
}
export interface RenderAPI extends GetByAPI, QueryByAPI {
update(nextElement: React.ReactElement<any>): void;
unmount(nextElement?: React.ReactElement<any>): void;
}
export type FireEventFunction = (
element: ReactTestInstance,
eventName: string,
data?: any
) => any;
export type FireEventAPI = FireEventFunction & {
press: (element: ReactTestInstance) => any;
doublePress: (element: ReactTestInstance) => any;
changeText: (element: ReactTestInstance, data?: any) => any;
scroll: (element: ReactTestInstance, data?: any) => any;
};
export declare const render: (
component: React.ReactElement<any>,
options?: RenderOptions
) => RenderAPI;
export declare const shallow: <P = {}>(
instance: ReactTestInstance | React.ReactElement<P>
) => { output: React.ReactElement<P> };
export declare const flushMicrotasksQueue: () => Promise<any>;
export declare const debug: (
instance: ReactTestInstance | React.ReactElement<any>
) => void;
export declare const fireEvent: FireEventAPI;