Skip to content

Commit b7158cf

Browse files
committed
CR: tweak docs examples for wait and waitForElement
- use `container` in the examples as this is a more popular use case than the default value of global `document` - use full sentences with capital first letter and period in the example comments
1 parent 88bab87 commit b7158cf

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ Here's a simple example:
285285

286286
```javascript
287287
// ...
288-
// wait until the callback does not throw an error. In this case, that means
289-
// it'll wait until we can get a form control with a label that matches "username"
288+
// Wait until the callback does not throw an error. In this case, that means
289+
// it'll wait until we can get a form control with a label that matches "username".
290290
await wait(() => getByLabelText(container, 'username'))
291291
getByLabelText(container, 'username').value = 'chucknorris'
292292
// ...
@@ -329,23 +329,29 @@ Here's a simple example:
329329

330330
```javascript
331331
// ...
332-
// wait until the callback does not throw an error and returns a truthy value. In this case, that means
333-
// it'll wait until we can get a form control with a label that matches "username"
334-
// the difference from `wait` is that rather than running your callback on
332+
// Wait until the callback does not throw an error and returns a truthy value. In this case, that means
333+
// it'll wait until we can get a form control with a label that matches "username".
334+
// The difference from `wait` is that rather than running your callback on
335335
// an interval, it's run as soon as there are DOM changes in the container
336-
// and returns the value returned by the callback
337-
const usernameElement = await waitForElement(() => getByLabelText(container, 'username'))
336+
// and returns the value returned by the callback.
337+
const usernameElement = await waitForElement(
338+
() => getByLabelText(container, 'username'),
339+
{container},
340+
)
338341
usernameElement.value = 'chucknorris'
339342
// ...
340343
```
341344

342345
You can also wait for multiple elements at once:
343346

344347
```javascript
345-
const [usernameElement, passwordElement] = waitForElement(() => [
346-
getByLabelText(container, 'username'),
347-
getByLabelText(container, 'password'),
348-
])
348+
const [usernameElement, passwordElement] = waitForElement(
349+
() => [
350+
getByLabelText(container, 'username'),
351+
getByLabelText(container, 'password'),
352+
],
353+
{container},
354+
)
349355
```
350356

351357
Using [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) is more efficient than polling the DOM at regular intervals with `wait`. This library sets up a [`'mutationobserver-shim'`](https://github.com/megawac/MutationObserver.js) on the global `window` object for cross-platform compatibility with older browsers and the [`jsdom`](https://github.com/jsdom/jsdom/issues/639) that is usually used in Node-based tests.

0 commit comments

Comments
 (0)