Skip to content

Commit a2cf53a

Browse files
committed
format docs
1 parent 4d969f9 commit a2cf53a

27 files changed

+417
-330
lines changed

.prettierrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
proseWrap: 'always',
3+
singleQuote: true,
4+
semi: false,
5+
trailingComma: 'es5',
6+
}

docs/api-async.md

+51-23
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function wait(
1111
options?: {
1212
timeout?: number
1313
interval?: number
14-
},
14+
}
1515
): Promise<void>
1616
```
1717

@@ -30,8 +30,8 @@ getByLabelText(container, 'username').value = 'chucknorris'
3030
// ...
3131
```
3232

33-
This can be useful if you have a unit test that mocks API calls and you need
34-
to wait for your mock promises to all resolve.
33+
This can be useful if you have a unit test that mocks API calls and you need to
34+
wait for your mock promises to all resolve.
3535

3636
The default `callback` is a no-op function (used like `await wait()`). This can
3737
be helpful if you only need to wait for one tick of the event loop (in the case
@@ -53,12 +53,14 @@ function waitForElement<T>(
5353
container?: HTMLElement
5454
timeout?: number
5555
mutationObserverOptions?: MutationObserverInit
56-
},
56+
}
5757
): Promise<T>
5858
```
5959

60-
When in need to wait for DOM elements to appear, disappear, or change you can use `waitForElement`.
61-
The `waitForElement` function is a small wrapper around the [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
60+
When in need to wait for DOM elements to appear, disappear, or change you can
61+
use `waitForElement`. The `waitForElement` function is a small wrapper around
62+
the
63+
[`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
6264

6365
Here's a simple example:
6466

@@ -71,7 +73,7 @@ Here's a simple example:
7173
// and returns the value returned by the callback.
7274
const usernameElement = await waitForElement(
7375
() => getByLabelText(container, 'username'),
74-
{container},
76+
{ container }
7577
)
7678
usernameElement.value = 'chucknorris'
7779
// ...
@@ -85,19 +87,30 @@ const [usernameElement, passwordElement] = await waitForElement(
8587
getByLabelText(container, 'username'),
8688
getByLabelText(container, 'password'),
8789
],
88-
{container},
90+
{ container }
8991
)
9092
```
9193

92-
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.
94+
Using
95+
[`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
96+
is more efficient than polling the DOM at regular intervals with `wait`. This
97+
library sets up a
98+
[`'mutationobserver-shim'`](https://github.com/megawac/MutationObserver.js) on
99+
the global `window` object for cross-platform compatibility with older browsers
100+
and the [`jsdom`](https://github.com/jsdom/jsdom/issues/639) that is usually
101+
used in Node-based tests.
93102

94-
The default `container` is the global `document`. Make sure the elements you wait for will be attached to it, or set a different `container`.
103+
The default `container` is the global `document`. Make sure the elements you
104+
wait for will be attached to it, or set a different `container`.
95105

96106
The default `timeout` is `4500ms` which will keep you under
97107
[Jest's default timeout of `5000ms`](https://facebook.github.io/jest/docs/en/jest-object.html#jestsettimeouttimeout).
98108

99-
<a name="mutationobserveroptions"></a>The default `mutationObserverOptions` is `{subtree: true, childList: true, attributes: true, characterData: true}` which will detect
100-
additions and removals of child elements (including text nodes) in the `container` and any of its descendants. It will also detect attribute changes.
109+
<a name="mutationobserveroptions"></a>The default `mutationObserverOptions` is
110+
`{subtree: true, childList: true, attributes: true, characterData: true}` which
111+
will detect additions and removals of child elements (including text nodes) in
112+
the `container` and any of its descendants. It will also detect attribute
113+
changes.
101114

102115
### `waitForDomChange`
103116

@@ -109,33 +122,37 @@ function waitForDomChange<T>(options?: {
109122
}): Promise<T>
110123
```
111124

112-
When in need to wait for the DOM to change you can use `waitForDomChange`. The `waitForDomChange`
113-
function is a small wrapper around the
125+
When in need to wait for the DOM to change you can use `waitForDomChange`. The
126+
`waitForDomChange` function is a small wrapper around the
114127
[`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
115128

116-
Here is an example where the promise will be resolved because the container is changed:
129+
Here is an example where the promise will be resolved because the container is
130+
changed:
117131

118132
```javascript
119133
const container = document.createElement('div')
120-
waitForDomChange({container})
134+
waitForDomChange({ container })
121135
.then(() => console.log('DOM changed!'))
122136
.catch(err => console.log(`Error you need to deal with: ${err}`))
123137
container.append(document.createElement('p'))
124138
// if 👆 was the only code affecting the container and it was not run,
125139
// waitForDomChange would throw an error
126140
```
127141

128-
The promise will resolve with a [`mutationsList`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver) which you can use to determine what kind of a change (or changes) affected the container
142+
The promise will resolve with a
143+
[`mutationsList`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver)
144+
which you can use to determine what kind of a change (or changes) affected the
145+
container
129146

130147
```javascript
131148
const container = document.createElement('div')
132149
container.setAttribute('data-cool', 'true')
133-
waitForDomChange({container}).then(mutationsList => {
150+
waitForDomChange({ container }).then(mutationsList => {
134151
const mutation = mutationsList[0]
135152
console.log(
136153
`was cool: ${mutation.oldValue}\ncurrently cool: ${
137154
mutation.target.dataset.cool
138-
}`,
155+
}`
139156
)
140157
})
141158
container.setAttribute('data-cool', 'false')
@@ -146,12 +163,23 @@ container.setAttribute('data-cool', 'false')
146163
*/
147164
```
148165

149-
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.
166+
Using
167+
[`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
168+
is more efficient than polling the DOM at regular intervals with `wait`. This
169+
library sets up a
170+
[`'mutationobserver-shim'`](https://github.com/megawac/MutationObserver.js) on
171+
the global `window` object for cross-platform compatibility with older browsers
172+
and the [`jsdom`](https://github.com/jsdom/jsdom/issues/639) that is usually
173+
used in Node-based tests.
150174

151-
The default `container` is the global `document`. Make sure the elements you wait for will be attached to it, or set a different `container`.
175+
The default `container` is the global `document`. Make sure the elements you
176+
wait for will be attached to it, or set a different `container`.
152177

153178
The default `timeout` is `4500ms` which will keep you under
154179
[Jest's default timeout of `5000ms`](https://facebook.github.io/jest/docs/en/jest-object.html#jestsettimeouttimeout).
155180

156-
<a name="mutationobserveroptions"></a>The default `mutationObserverOptions` is `{subtree: true, childList: true, attributes: true, characterData: true}` which will detect
157-
additions and removals of child elements (including text nodes) in the `container` and any of its descendants. It will also detect attribute changes.
181+
<a name="mutationobserveroptions"></a>The default `mutationObserverOptions` is
182+
`{subtree: true, childList: true, attributes: true, characterData: true}` which
183+
will detect additions and removals of child elements (including text nodes) in
184+
the `container` and any of its descendants. It will also detect attribute
185+
changes.

docs/api-configuration.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ title: Configuration
77

88
The library can be configured via the `configure` function, which accepts:
99

10-
- a plain JS object; this will be merged into the existing configuration. e.g. `configure({testIdAttribute: 'not-data-testid'})`
11-
- a function; the function will be given the existing configuration, and should return a plain JS object which will be merged as above, e.g.
10+
- a plain JS object; this will be merged into the existing configuration. e.g.
11+
`configure({testIdAttribute: 'not-data-testid'})`
12+
- a function; the function will be given the existing configuration, and should
13+
return a plain JS object which will be merged as above, e.g.
1214
`configure(existingConfig => ({something: [...existingConfig.something, 'extra value for the something array']}))`
1315

1416
Configuration options:
1517

1618
`testIdAttribute`: The attribute used by `getByTestId` and related queries.
17-
Defaults to `data-testid`. See [`getByTestId`](#getbytestid).
19+
Defaults to `data-testid`. See [`getByTestId`](#getbytestid).

docs/api-events.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fireEvent(
1818
new MouseEvent('click', {
1919
bubbles: true,
2020
cancelable: true,
21-
}),
21+
})
2222
)
2323
```
2424

@@ -34,7 +34,7 @@ for a full list as well as default `eventProperties`.
3434

3535
```javascript
3636
// <button>Submit</button>
37-
const rightClick = {button: 2}
37+
const rightClick = { button: 2 }
3838
fireEvent.click(getByText('Submit'), rightClick)
3939
// default `button` property for click events is set to `0` which is a left click.
4040
```
@@ -47,22 +47,25 @@ those properties will be assigned to the node which is receiving the event.
4747
This is particularly useful for a change event:
4848

4949
```javascript
50-
fireEvent.change(getByLabelText(/username/i), {target: {value: 'a'}})
50+
fireEvent.change(getByLabelText(/username/i), { target: { value: 'a' } })
5151

5252
// note: attempting to manually set the files property of an HTMLInputElement
5353
// results in an error as the files property is read-only.
5454
// this feature works around that by using Object.defineProperty.
5555
fireEvent.change(getByLabelText(/picture/i), {
5656
target: {
57-
files: [new File(['(⌐□_□)'], 'chucknorris.png', {type: 'image/png'})],
57+
files: [new File(['(⌐□_□)'], 'chucknorris.png', { type: 'image/png' })],
5858
},
5959
})
6060
```
6161

62-
**Keyboard events**: There are three event types related to keyboard input - `keyPress`, `keyDown`, and `keyUp`. When firing these you need to reference an element in the DOM and the key you want to fire.
62+
**Keyboard events**: There are three event types related to keyboard input -
63+
`keyPress`, `keyDown`, and `keyUp`. When firing these you need to reference an
64+
element in the DOM and the key you want to fire.
6365

6466
```javascript
65-
fireEvent.keyDown(domNode, {key: 'Enter', code: 13})
67+
fireEvent.keyDown(domNode, { key: 'Enter', code: 13 })
6668
```
6769

68-
You can find out which key code to use at [https://keycode.info/](https://keycode.info/).
70+
You can find out which key code to use at
71+
[https://keycode.info/](https://keycode.info/).

docs/api-helpers.md

+23-17
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,32 @@ title: Helpers
55

66
## Custom Queries
77

8-
`dom-testing-library` exposes many of the helper functions that are used to implement the default queries. You can use the helpers to build custom queries. For example, the code below shows a way to override the default `testId` queries to use a different data-attribute. (Note: test files would import `test-utils.js` instead of using `dom-testing-library` directly).
8+
`dom-testing-library` exposes many of the helper functions that are used to
9+
implement the default queries. You can use the helpers to build custom queries.
10+
For example, the code below shows a way to override the default `testId` queries
11+
to use a different data-attribute. (Note: test files would import
12+
`test-utils.js` instead of using `dom-testing-library` directly).
913

1014
```js
1115
// test-utils.js
1216
const domTestingLib = require('dom-testing-library')
13-
const {queryHelpers} = domTestingLib
17+
const { queryHelpers } = domTestingLib
1418

1519
export const queryByTestId = queryHelpers.queryByAttribute.bind(
1620
null,
17-
'data-test-id',
21+
'data-test-id'
1822
)
1923
export const queryAllByTestId = queryHelpers.queryAllByAttribute.bind(
2024
null,
21-
'data-test-id',
25+
'data-test-id'
2226
)
2327

2428
export function getAllByTestId(container, id, ...rest) {
2529
const els = queryAllByTestId(container, id, ...rest)
2630
if (!els.length) {
2731
throw queryHelpers.getElementError(
2832
`Unable to find an element by: [data-test-id="${id}"]`,
29-
container,
33+
container
3034
)
3135
}
3236
return els
@@ -93,16 +97,16 @@ These elements use the attribute `value` and display its value to the user.
9397

9498
## Debugging
9599

96-
When you use any `get` calls in your test cases, the current state of the `container`
97-
(DOM) gets printed on the console. For example:
100+
When you use any `get` calls in your test cases, the current state of the
101+
`container` (DOM) gets printed on the console. For example:
98102

99103
```javascript
100104
// <div>Hello world</div>
101105
getByText(container, 'Goodbye world') // will fail by throwing error
102106
```
103107

104-
The above test case will fail, however it prints the state of your DOM under test,
105-
so you will get to see:
108+
The above test case will fail, however it prints the state of your DOM under
109+
test, so you will get to see:
106110

107111
```
108112
Unable to find an element with the text: Goodbye world. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
@@ -114,18 +118,19 @@ Here is the state of your container:
114118
</div>
115119
```
116120

117-
Note: Since the DOM size can get really large, you can set the limit of DOM content
118-
to be printed via environment variable `DEBUG_PRINT_LIMIT`. The default value is
119-
`7000`. You will see `...` in the console, when the DOM content is stripped off,
120-
because of the length you have set or due to default size limit. Here's how you
121-
might increase this limit when running tests:
121+
Note: Since the DOM size can get really large, you can set the limit of DOM
122+
content to be printed via environment variable `DEBUG_PRINT_LIMIT`. The default
123+
value is `7000`. You will see `...` in the console, when the DOM content is
124+
stripped off, because of the length you have set or due to default size limit.
125+
Here's how you might increase this limit when running tests:
122126

123127
```
124128
DEBUG_PRINT_LIMIT=10000 npm test
125129
```
126130

127-
This works on macOS/linux, you'll need to do something else for windows. If you'd
128-
like a solution that works for both, see [`cross-env`](https://www.npmjs.com/package/cross-env)
131+
This works on macOS/linux, you'll need to do something else for windows. If
132+
you'd like a solution that works for both, see
133+
[`cross-env`](https://www.npmjs.com/package/cross-env)
129134

130135
### `prettyDOM`
131136

@@ -153,4 +158,5 @@ console.log(prettyDOM(div))
153158
// </div>
154159
```
155160

156-
This function is what also powers [the automatic debugging output described above](#debugging).
161+
This function is what also powers
162+
[the automatic debugging output described above](#debugging).

0 commit comments

Comments
 (0)