Skip to content

Commit 738b608

Browse files
author
Artemis330
committed
Reorganize query documentation (#33)
* reorganize query documentation * Add searchable text for each query variant
1 parent 8cd19f7 commit 738b608

File tree

2 files changed

+76
-35
lines changed

2 files changed

+76
-35
lines changed

docs/api-helpers.md

+17
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ const buttonText = getNodeText(container.querySelector('input[type=button]')) //
9595
These elements use the attribute `value` and display its value to the user.
9696
```
9797

98+
## `within` and `getQueriesForElement` APIs
99+
100+
`within` (an alias to `getQueriesForElement`) takes a DOM element and binds it
101+
to the raw query functions, allowing them to be used without specifying a
102+
container. It is the recommended approach for libraries built on this API and is
103+
in use in `react-testing-library` and `vue-testing-library`.
104+
105+
Example: To get the text 'hello' only within a section called 'messages', you
106+
could do:
107+
108+
```javascript
109+
import { within } from 'dom-testing-library'
110+
111+
const { getByText } = within(document.getElementById('messages'))
112+
const helloMessage = getByText('hello')
113+
```
114+
98115
## Debugging
99116

100117
When you use any `get` calls in your test cases, the current state of the

docs/api-queries.md

+59-35
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,44 @@ id: api-queries
33
title: Queries
44
---
55

6+
## Variants
7+
8+
> `getBy` queries are shown by default in the [query documentation](#queries)
9+
> below.
10+
11+
### getBy
12+
13+
[`getBy*`](#query-apis) queries returns the first matching node for a query, and
14+
throw an error if no elements match.
15+
16+
### getAllBy
17+
18+
[`getAllBy*`](#queryall-and-getall-apis) queries return an array of all matching
19+
nodes for a query, and throw an error if no elements match.
20+
21+
### queryBy
22+
23+
[`queryBy*`](#query-apis) queries returns the first matching node for a query,
24+
and return `null` if no elements match.
25+
26+
### queryAllBy
27+
28+
[`queryAllBy*`](#queryall-and-getall-apis) queries return an array of all
29+
matching nodes for a query, and return an empty array (`[]`) if no elements
30+
match.
31+
32+
## Options
33+
34+
The argument to a query can be a _string_, _regular expression_, or _function_.
35+
There are also options to adjust how node text is parsed.
36+
37+
See [TextMatch](#textmatch) for documentation on what can be passed to a query.
38+
639
## Queries
740

8-
> **Note**
9-
>
10-
> - Each of the `get` APIs below have a matching
11-
> [`getAll`](#queryall-and-getall-apis) API that returns all elements instead
12-
> of just the first one, and
13-
> [`query`](#query-apis)/[`queryAll`](#queryall-and-getall-apis) that return
14-
> `null`/`[]` instead of throwing an error.
15-
> - See [TextMatch](#textmatch) for details on the `exact`, `trim`, and
16-
> `collapseWhitespace` options.
41+
### `ByLabelText`
1742

18-
### `getByLabelText`
43+
> getByLabelText, queryByLabelText, getAllByLabelText, queryAllByLabelText
1944
2045
```typescript
2146
getByLabelText(
@@ -69,7 +94,10 @@ const inputNode = getByLabelText(container, 'username', { selector: 'input' })
6994
> this behavior (for example you wish to assert that it doesn't exist), then use
7095
> `queryByLabelText` instead.
7196
72-
### `getByPlaceholderText`
97+
### `ByPlaceholderText`
98+
99+
> getByPlaceholderText, queryByPlaceholderText, getAllByPlaceholderText,
100+
> queryAllByPlaceholderText
73101
74102
```typescript
75103
getByPlaceholderText(
@@ -94,7 +122,9 @@ const inputNode = getByPlaceholderText(container, 'Username')
94122
> A placeholder is not a good substitute for a label so you should generally use
95123
> `getByLabelText` instead.
96124
97-
### `getByText`
125+
### `ByText`
126+
127+
> getByText, queryByText, getAllByText, queryAllByText
98128
99129
```typescript
100130
getByText(
@@ -137,7 +167,9 @@ content is in an inline script file, then the script tag could be returned.
137167

138168
If you'd rather disable this behavior, set `ignore` to `false`.
139169

140-
### `getByAltText`
170+
### `ByAltText`
171+
172+
> getByAltText, queryByAltText, getAllByAltText, queryAllByAltText
141173
142174
```typescript
143175
getByAltText(
@@ -163,7 +195,9 @@ as it's deprecated).
163195
const incrediblesPosterImg = getByAltText(container, /incredibles.*poster$/i)
164196
```
165197

166-
### `getByTitle`
198+
### `ByTitle`
199+
200+
> getByTitle, queryByTitle, getAllByTitle, queryAllByTitle
167201
168202
```typescript
169203
getByTitle(
@@ -189,7 +223,10 @@ Will also find a `title` element within an SVG.
189223
const closeElement = getByTitle(container, 'Close')
190224
```
191225

192-
### `getByDisplayValue`
226+
### `ByDisplayValue`
227+
228+
> getByDisplayValue, queryByDisplayValue, getAllByDisplayValue,
229+
> queryAllByDisplayValue
193230
194231
```typescript
195232
getByDisplayValue(
@@ -238,7 +275,9 @@ const selectElement = getByDisplayName(container, 'Alaska')
238275
In case of `select`, this will search for a `<select>` whose selected `<option>`
239276
matches the given [`TextMatch`](#textmatch).
240277

241-
### `getByRole`
278+
### `ByRole`
279+
280+
> getByRole, queryByRole, getAllByRole, queryAllByRole
242281
243282
```typescript
244283
getByRole(
@@ -258,7 +297,9 @@ accepts a [`TextMatch`](#textmatch)).
258297
const dialogContainer = getByRole(container, 'dialog')
259298
```
260299

261-
### `getByTestId`
300+
### `ByTestId`
301+
302+
> getByTestId, queryByTestId, getAllByTestId, queryAllByTestId
262303
263304
```typescript
264305
getByTestId(
@@ -391,7 +432,7 @@ getByText(container, (content, element) => {
391432

392433
## `query` APIs
393434

394-
Each of the `get` APIs listed in [the 'Usage'](#usage) section above have a
435+
Each of the `get` APIs listed in the [queries](#queries) section above have a
395436
complimentary `query` API. The `get` APIs will throw errors if a proper node
396437
cannot be found. This is normally the desired effect. However, if you want to
397438
make an assertion that an element is _not_ present in the DOM, then you can use
@@ -415,20 +456,3 @@ const submitButtons = queryAllByText(container, 'submit')
415456
expect(submitButtons).toHaveLength(3) // expect 3 elements
416457
expect(submitButtons[0]).toBeTruthy()
417458
```
418-
419-
## `within` and `getQueriesForElement` APIs
420-
421-
`within` (an alias to `getQueriesForElement`) takes a DOM element and binds it
422-
to the raw query functions, allowing them to be used without specifying a
423-
container. It is the recommended approach for libraries built on this API and is
424-
in use in `react-testing-library` and `vue-testing-library`.
425-
426-
Example: To get the text 'hello' only within a section called 'messages', you
427-
could do:
428-
429-
```javascript
430-
import { within } from 'dom-testing-library'
431-
432-
const { getByText } = within(document.getElementById('messages'))
433-
const helloMessage = getByText('hello')
434-
```

0 commit comments

Comments
 (0)