From 61388a6f8d825a8023e3d58568e651e1cd24d9fc Mon Sep 17 00:00:00 2001 From: Kacper Wiszczuk Date: Wed, 10 Oct 2018 21:34:57 +0200 Subject: [PATCH 1/3] feat Add Typescript types to waitForElement --- src/waitForElement.js | 2 +- typings/__tests__/index.test.tsx | 16 +++++++++++++++- typings/index.d.ts | 7 +++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/waitForElement.js b/src/waitForElement.js index af08e2b15..211da73dd 100644 --- a/src/waitForElement.js +++ b/src/waitForElement.js @@ -1,5 +1,5 @@ // @flow -export default function waitForExpect( +export default function waitForExpect( expectation: () => T, timeout: number = 4500, interval: number = 50 diff --git a/typings/__tests__/index.test.tsx b/typings/__tests__/index.test.tsx index 7a73f256b..e5e03ac7d 100644 --- a/typings/__tests__/index.test.tsx +++ b/typings/__tests__/index.test.tsx @@ -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; @@ -77,3 +84,10 @@ const waitForFlush: Promise = flushMicrotasksQueue(); debug(); debug(getByNameString); + +const waitBy: Promise = waitForElement( + () => tree.getByName('View') +); +const waitByAll: Promise> = waitForElement< + Array +>(() => tree.getAllByName('View'), 1000, 50); diff --git a/typings/index.d.ts b/typings/index.d.ts index 49aff95cb..bbf57539e 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -45,6 +45,12 @@ export type FireEventAPI = FireEventFunction & { scroll: (element: ReactTestInstance, data?: any) => any; }; +export type WaitForElementFunction = ( + expectation: () => T, + timeout?: number, + interval?: number +) => Promise; + export declare const render: ( component: React.ReactElement, options?: RenderOptions @@ -57,3 +63,4 @@ export declare const debug: ( instance: ReactTestInstance | React.ReactElement ) => void; export declare const fireEvent: FireEventAPI; +export declare const waitForElement: WaitForElementFunction; From 78b938a8d6e5c9632adcee81d6176b20bf5152ec Mon Sep 17 00:00:00 2001 From: Kacper Wiszczuk Date: Wed, 10 Oct 2018 21:36:53 +0200 Subject: [PATCH 2/3] feat Update docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c25ee6b53..8aaa92a2b 100644 --- a/README.md +++ b/README.md @@ -259,7 +259,7 @@ fireEvent.scroll(getByTestId('scroll-view'), eventData); Defined as: ```jsx -function waitForExpect( +function waitForExpect( expectation: () => T, timeout: number = 4500, interval: number = 50 From 7ba28fc5d445bb62701efd884b89ad77b4964913 Mon Sep 17 00:00:00 2001 From: Kacper Wiszczuk Date: Wed, 10 Oct 2018 21:44:20 +0200 Subject: [PATCH 3/3] feat add missing return type --- src/waitForElement.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/waitForElement.js b/src/waitForElement.js index 211da73dd..f8008c5de 100644 --- a/src/waitForElement.js +++ b/src/waitForElement.js @@ -3,7 +3,7 @@ export default function waitForExpect( expectation: () => T, timeout: number = 4500, interval: number = 50 -) { +): Promise { const startTime = Date.now(); return new Promise((resolve, reject) => { const rejectOrRerun = error => {