diff --git a/docs/api-async.md b/docs/api-async.md index c725a72..e49bf9f 100644 --- a/docs/api-async.md +++ b/docs/api-async.md @@ -1,7 +1,7 @@ --- id: api-async -title: Async -sidebar_label: Async +title: Async Utilities +sidebar_label: Async Utilities --- Several utilities are provided for dealing with asynchronous code. These can be useful to wait for @@ -26,8 +26,8 @@ expectations to pass. The `wait` function is a small wrapper around the example: ```javascript -await wait(() => getByLabelText(container, 'username')); -getByLabelText(container, 'username').value = 'chucknorris'; +await wait(() => getByLabelText('username')); +getByLabelText('username').value = 'chucknorris'; ``` This can be useful if you have a unit test that mocks API calls and you need to wait for your mock @@ -49,38 +49,60 @@ the event loop (in a `setTimeout`) before starting the intervals. function waitForElement( callback: () => T, options?: { - container?: ReactTestInstance; timeout?: number; interval?: number; }, ): Promise; ``` -When in need to wait for DOM elements to appear, disappear, or change you can use `waitForElement`. -The `waitForElement` function is a similar to `wait`, but is a helper specifically intended to wait -for an element. +When you need to wait for elements to appear, you can use `waitForElement`. The `waitForElement` +function is a similar to `wait`, but is specifically intended to wait for an element to appear. +Additionally, the result is returned for you to use. Here's a simple example: ```javascript -const usernameElement = await waitForElement(() => getByA11yLabel(container, 'username'), { - container, -}); -expect(usernameElement.props.children).toBe('chucknorris'); +const usernameElement = await waitForElement(() => getByLabelText('username')); +expect(usernameElement).toHaveTextContent('chucknorris'); ``` You can also wait for multiple elements at once: ```javascript -const [usernameElement, passwordElement] = await waitForElement( - () => [getByA11yLabel(container, 'username'), getByA11yLabel(container, 'password')], - { container }, -); +const [usernameElement, passwordElement] = await waitForElement(() => [ + getByLabelText('username'), + getByLabelText('password'), +]); ``` -The default `container` is the root `ReactTestInstance` that is the result of `render`. Make sure -the elements you wait for will be attached to it, or set a different `ReactTestInstance` as a -container. +The default `timeout` is `4500ms` which will keep you under +[Jest's default timeout of `5000ms`](https://facebook.github.io/jest/docs/en/jest-object.html#jestsettimeouttimeout). + +The default `interval` is `50ms`. However it will run your callback immediately on the next tick of +the event loop (in a `setTimeout`) before starting the intervals. + +## `waitForElementToBeRemoved` + +```typescript +function waitForElementToBeRemoved( + callback: () => T, + options?: { + timeout?: number; + interval?: number; + }, +): Promise; +``` + +When you need to wait for elements to be removed, or you can use `waitForElementToBeRemoved`. The +`waitForElementToBeRemoved` function is a similar to `wait`, but is a helper specifically intended +to wait for an element to be removed from the tree. Similarly to `waitForElement` the result of the +callback is returned as a Promise, but in most cases you won't need it. + +Here's a simple example: + +```javascript +await waitForElementToBeRemoved(() => queryAllByLabelText('list-item')); +``` The default `timeout` is `4500ms` which will keep you under [Jest's default timeout of `5000ms`](https://facebook.github.io/jest/docs/en/jest-object.html#jestsettimeouttimeout). diff --git a/docs/api-events.md b/docs/api-events.md index bc7d996..187768e 100644 --- a/docs/api-events.md +++ b/docs/api-events.md @@ -7,21 +7,21 @@ sidebar_label: Firing Events ## Basic example ```javascript -import { fireEvent, NativeEvent, render } from 'native-testing-library'; +import { fireEvent, NativeTestEvent, render } from 'native-testing-library'; const { getByText } = render(