Skip to content

Commit 480dd28

Browse files
Esemesekthymikee
authored andcommitted
feat: Add Typescript types to waitForElement (#21)
Fix types for Flow too
1 parent d5219d2 commit 480dd28

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ fireEvent.scroll(getByTestId('scroll-view'), eventData);
259259
Defined as:
260260

261261
```jsx
262-
function waitForExpect<T: ReactTestInstance>(
262+
function waitForExpect<T: *>(
263263
expectation: () => T,
264264
timeout: number = 4500,
265265
interval: number = 50

src/waitForElement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @flow
2-
export default function waitForExpect<T: ReactTestInstance>(
2+
export default function waitForExpect<T: *>(
33
expectation: () => T,
44
timeout: number = 4500,
55
interval: number = 50
6-
) {
6+
): Promise<T> {
77
const startTime = Date.now();
88
return new Promise<T>((resolve, reject) => {
99
const rejectOrRerun = error => {

typings/__tests__/index.test.tsx

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import * as React from 'react';
22
import { ReactTestInstance } from 'react-test-renderer';
3-
import { render, fireEvent, shallow, flushMicrotasksQueue, debug } from '../..';
3+
import {
4+
render,
5+
fireEvent,
6+
shallow,
7+
flushMicrotasksQueue,
8+
debug,
9+
waitForElement,
10+
} from '../..';
411

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

7885
debug(<TestComponent />);
7986
debug(getByNameString);
87+
88+
const waitBy: Promise<ReactTestInstance> = waitForElement<ReactTestInstance>(
89+
() => tree.getByName('View')
90+
);
91+
const waitByAll: Promise<Array<ReactTestInstance>> = waitForElement<
92+
Array<ReactTestInstance>
93+
>(() => tree.getAllByName('View'), 1000, 50);

typings/index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export type FireEventAPI = FireEventFunction & {
4545
scroll: (element: ReactTestInstance, data?: any) => any;
4646
};
4747

48+
export type WaitForElementFunction = <T = any>(
49+
expectation: () => T,
50+
timeout?: number,
51+
interval?: number
52+
) => Promise<T>;
53+
4854
export declare const render: (
4955
component: React.ReactElement<any>,
5056
options?: RenderOptions
@@ -57,3 +63,4 @@ export declare const debug: (
5763
instance: ReactTestInstance | React.ReactElement<any>
5864
) => void;
5965
export declare const fireEvent: FireEventAPI;
66+
export declare const waitForElement: WaitForElementFunction;

0 commit comments

Comments
 (0)