Skip to content
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

feat Add Typescript types to waitForElement #21

Merged
merged 3 commits into from
Oct 10, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ fireEvent.scroll(getByTestId('scroll-view'), eventData);
Defined as:

```jsx
function waitForExpect<T: ReactTestInstance>(
function waitForExpect<T: *>(
expectation: () => T,
timeout: number = 4500,
interval: number = 50
Expand Down
4 changes: 2 additions & 2 deletions src/waitForElement.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @flow
export default function waitForExpect<T: ReactTestInstance>(
export default function waitForExpect<T: *>(
expectation: () => T,
timeout: number = 4500,
interval: number = 50
) {
): Promise<T> {
const startTime = Date.now();
return new Promise<T>((resolve, reject) => {
const rejectOrRerun = error => {
Expand Down
16 changes: 15 additions & 1 deletion typings/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as React from 'react';
import { ReactTestInstance } from 'react-test-renderer';
import { render, fireEvent, shallow, flushMicrotasksQueue, debug } from '../..';
import {
render,
fireEvent,
shallow,
flushMicrotasksQueue,
debug,
waitForElement,
} from '../..';

const View = props => props.children;
const Text = props => props.children;
Expand Down Expand Up @@ -77,3 +84,10 @@ const waitForFlush: Promise<any> = flushMicrotasksQueue();

debug(<TestComponent />);
debug(getByNameString);

const waitBy: Promise<ReactTestInstance> = waitForElement<ReactTestInstance>(
() => tree.getByName('View')
);
const waitByAll: Promise<Array<ReactTestInstance>> = waitForElement<
Array<ReactTestInstance>
>(() => tree.getAllByName('View'), 1000, 50);
7 changes: 7 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export type FireEventAPI = FireEventFunction & {
scroll: (element: ReactTestInstance, data?: any) => any;
};

export type WaitForElementFunction = <T = any>(
expectation: () => T,
timeout?: number,
interval?: number
) => Promise<T>;

export declare const render: (
component: React.ReactElement<any>,
options?: RenderOptions
Expand All @@ -57,3 +63,4 @@ export declare const debug: (
instance: ReactTestInstance | React.ReactElement<any>
) => void;
export declare const fireEvent: FireEventAPI;
export declare const waitForElement: WaitForElementFunction;