From 0518418c456f1602b932130fdee5c58fd44f824f Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Wed, 20 May 2020 15:12:26 +0200 Subject: [PATCH 1/3] Deprecated flushMicrotasksQueue --- README.md | 1 - src/flushMicrotasksQueue.js | 8 ++++++++ src/helpers/errors.js | 2 +- src/index.js | 4 ++-- typings/index.d.ts | 6 +++++- website/docs/API.md | 15 --------------- website/docs/Migration20.md | 12 ++++++++++++ 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 549ea139c..882c2a7a0 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,6 @@ The [public API](https://callstack.github.io/react-native-testing-library/docs/a - [`fireEvent`](https://callstack.github.io/react-native-testing-library/docs/api#fireevent) - invokes named event handler on the element. - [`waitFor`](https://callstack.github.io/react-native-testing-library/docs/api#waitfor) - waits for non-deterministic periods of time until your element appears or times out. - [`within`](https://callstack.github.io/react-native-testing-library/docs/api#within) - creates a queries object scoped for given element. -- [`flushMicrotasksQueue`](https://callstack.github.io/react-native-testing-library/docs/api#flushmicrotasksqueue) - waits for microtasks queue to flush. ## Migration Guides diff --git a/src/flushMicrotasksQueue.js b/src/flushMicrotasksQueue.js index caab8614b..da22656ca 100644 --- a/src/flushMicrotasksQueue.js +++ b/src/flushMicrotasksQueue.js @@ -1,7 +1,15 @@ // @flow + +import { printDeprecationWarning } from './helpers/errors'; + /** * Wait for microtasks queue to flush */ export default function flushMicrotasksQueue(): Promise { + printDeprecationWarning('flushMicrotasksQueue'); + return flushMicrotasksQueueInternal(); +} + +export function flushMicrotasksQueueInternal(): Promise { return new Promise((resolve) => setImmediate(resolve)); } diff --git a/src/helpers/errors.js b/src/helpers/errors.js index 3a030ac3d..ce089b873 100644 --- a/src/helpers/errors.js +++ b/src/helpers/errors.js @@ -33,7 +33,7 @@ export function printDeprecationWarning(functionName: string) { console.warn(` Deprecation Warning: - ${functionName} is not recommended for use and will be deleted in react-native-testing-library 2.x. + Use of ${functionName} is not recommended and will be deleted in future versions of react-native-testing-library. `); warned[functionName] = true; diff --git a/src/index.js b/src/index.js index fa21651f4..761900544 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ // @flow -import flushMicrotasksQueue from './flushMicrotasksQueue'; import { cleanup } from './pure'; +import { flushMicrotasksQueueInternal } from './flushMicrotasksQueue'; // If we're running in a test runner that supports afterEach // then we'll automatically run cleanup afterEach test @@ -10,7 +10,7 @@ import { cleanup } from './pure'; if (typeof afterEach === 'function' && !process.env.RNTL_SKIP_AUTO_CLEANUP) { // eslint-disable-next-line no-undef afterEach(async () => { - await flushMicrotasksQueue(); + await flushMicrotasksQueueInternal(); cleanup(); }); } diff --git a/typings/index.d.ts b/typings/index.d.ts index d38f9c0c6..b998fb93a 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -295,7 +295,6 @@ export declare const render: ( options?: RenderOptions ) => RenderAPI; -export declare const flushMicrotasksQueue: () => Promise; export declare const cleanup: () => void; export declare const fireEvent: FireEventAPI; export declare const waitFor: WaitForFunction; @@ -307,6 +306,11 @@ export declare const within: (instance: ReactTestInstance) => Queries; */ export declare const waitForElement: WaitForFunction; +/** + * @deprecated This function has been deprecated and will be removed in the next release. + */ +export declare const flushMicrotasksQueue: () => Promise; + /** * @deprecated This function has been removed. */ diff --git a/website/docs/API.md b/website/docs/API.md index 629fd9226..5d2adc734 100644 --- a/website/docs/API.md +++ b/website/docs/API.md @@ -377,21 +377,6 @@ Use cases for scoped queries include: - queries scoped to a single item inside a FlatList containing many items - queries scoped to a single screen in tests involving screen transitions (e.g. with react-navigation) -## `flushMicrotasksQueue` - -Waits for microtasks queue to flush. Useful if you want to wait for some promises with `async/await`. - -```jsx -import { flushMicrotasksQueue, render } from 'react-native-testing-library'; - -test('fetch data', async () => { - const { getByText } = render(); - getByText('fetch'); - await flushMicrotasksQueue(); - expect(getByText('fetch').props.title).toBe('loaded'); -}); -``` - ## `query` APIs Each of the get APIs listed in the render section above have a complimentary query API. The get APIs will throw errors if a proper node cannot be found. This is normally the desired effect. However, if you want to make an assertion that an element is not present in the hierarchy, then you can use the query API instead: diff --git a/website/docs/Migration20.md b/website/docs/Migration20.md index b97ff7bb5..63dc7a35b 100644 --- a/website/docs/Migration20.md +++ b/website/docs/Migration20.md @@ -113,3 +113,15 @@ If you relied on setting `testID` for your custom components, you should probabl :::caution In general, you should avoid `byTestId` queries when possible and rather use queries that check things that can been seen by the user (e.g. `byText`, `byPlaceholder`, etc) or accessability queries (e.g. `byA11yHint`, `byA11yLabel`, etc). ::: + +## Deprecated `flushMicrotasksQueue` + +We have deprecated `flushMicrotasksQueue` and plan to remove it in the next major version, as currently there are better available alternatives for helping you write async tests: `findBy` async queries and `waitFor` helper. + +If you can't or don't want to migrate your tests, you can get rid of the deprecation warning by copy-pasting function's implementation into your project: + +```js +function flushMicrotasksQueue() { + return new Promise((resolve) => setImmediate(resolve)); +} +``` From aaa689831307e3ba764a89bea96f6db12ebe3d35 Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Wed, 20 May 2020 16:05:45 +0200 Subject: [PATCH 2/3] Update website/docs/Migration20.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michał Pierzchała --- website/docs/Migration20.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/Migration20.md b/website/docs/Migration20.md index 63dc7a35b..d3c516e47 100644 --- a/website/docs/Migration20.md +++ b/website/docs/Migration20.md @@ -116,7 +116,7 @@ In general, you should avoid `byTestId` queries when possible and rather use que ## Deprecated `flushMicrotasksQueue` -We have deprecated `flushMicrotasksQueue` and plan to remove it in the next major version, as currently there are better available alternatives for helping you write async tests: `findBy` async queries and `waitFor` helper. +We have deprecated `flushMicrotasksQueue` and plan to remove it in the next major version, as currently there are better alternatives available for helping you write async tests: `findBy` async queries and `waitFor` helper. If you can't or don't want to migrate your tests, you can get rid of the deprecation warning by copy-pasting function's implementation into your project: From f1fda2d173a7ce00a577662b0a3a6b130c7e3ca5 Mon Sep 17 00:00:00 2001 From: Maciej Jastrzebski Date: Wed, 20 May 2020 16:23:46 +0200 Subject: [PATCH 3/3] Renamed flushMicrotasksQueueInternal to flushMicroTasks --- src/{flushMicrotasksQueue.js => flushMicroTasks.js} | 4 ++-- src/index.js | 4 ++-- src/pure.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/{flushMicrotasksQueue.js => flushMicroTasks.js} (73%) diff --git a/src/flushMicrotasksQueue.js b/src/flushMicroTasks.js similarity index 73% rename from src/flushMicrotasksQueue.js rename to src/flushMicroTasks.js index da22656ca..c4bb67903 100644 --- a/src/flushMicrotasksQueue.js +++ b/src/flushMicroTasks.js @@ -7,9 +7,9 @@ import { printDeprecationWarning } from './helpers/errors'; */ export default function flushMicrotasksQueue(): Promise { printDeprecationWarning('flushMicrotasksQueue'); - return flushMicrotasksQueueInternal(); + return flushMicroTasks(); } -export function flushMicrotasksQueueInternal(): Promise { +export function flushMicroTasks(): Promise { return new Promise((resolve) => setImmediate(resolve)); } diff --git a/src/index.js b/src/index.js index 761900544..d4cb964e8 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ // @flow import { cleanup } from './pure'; -import { flushMicrotasksQueueInternal } from './flushMicrotasksQueue'; +import { flushMicroTasks } from './flushMicroTasks'; // If we're running in a test runner that supports afterEach // then we'll automatically run cleanup afterEach test @@ -10,7 +10,7 @@ import { flushMicrotasksQueueInternal } from './flushMicrotasksQueue'; if (typeof afterEach === 'function' && !process.env.RNTL_SKIP_AUTO_CLEANUP) { // eslint-disable-next-line no-undef afterEach(async () => { - await flushMicrotasksQueueInternal(); + await flushMicroTasks(); cleanup(); }); } diff --git a/src/pure.js b/src/pure.js index 534119714..7765847a6 100644 --- a/src/pure.js +++ b/src/pure.js @@ -2,7 +2,7 @@ import act from './act'; import cleanup from './cleanup'; import fireEvent from './fireEvent'; -import flushMicrotasksQueue from './flushMicrotasksQueue'; +import flushMicrotasksQueue from './flushMicroTasks'; import render from './render'; import shallow from './shallow'; import waitFor, { waitForElement } from './waitFor';