@@ -3,19 +3,44 @@ id: api-queries
3
3
title : Queries
4
4
---
5
5
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
+
6
39
## Queries
7
40
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 `
17
42
18
- ### ` getByLabelText `
43
+ > getByLabelText, queryByLabelText, getAllByLabelText, queryAllByLabelText
19
44
20
45
``` typescript
21
46
getByLabelText (
@@ -69,7 +94,10 @@ const inputNode = getByLabelText(container, 'username', { selector: 'input' })
69
94
> this behavior (for example you wish to assert that it doesn't exist), then use
70
95
> ` queryByLabelText ` instead.
71
96
72
- ### ` getByPlaceholderText `
97
+ ### ` ByPlaceholderText `
98
+
99
+ > getByPlaceholderText, queryByPlaceholderText, getAllByPlaceholderText,
100
+ > queryAllByPlaceholderText
73
101
74
102
``` typescript
75
103
getByPlaceholderText (
@@ -94,7 +122,9 @@ const inputNode = getByPlaceholderText(container, 'Username')
94
122
> A placeholder is not a good substitute for a label so you should generally use
95
123
> ` getByLabelText ` instead.
96
124
97
- ### ` getByText `
125
+ ### ` ByText `
126
+
127
+ > getByText, queryByText, getAllByText, queryAllByText
98
128
99
129
``` typescript
100
130
getByText (
@@ -137,7 +167,9 @@ content is in an inline script file, then the script tag could be returned.
137
167
138
168
If you'd rather disable this behavior, set ` ignore ` to ` false ` .
139
169
140
- ### ` getByAltText `
170
+ ### ` ByAltText `
171
+
172
+ > getByAltText, queryByAltText, getAllByAltText, queryAllByAltText
141
173
142
174
``` typescript
143
175
getByAltText (
@@ -163,7 +195,9 @@ as it's deprecated).
163
195
const incrediblesPosterImg = getByAltText (container, / incredibles. * poster$ / i )
164
196
```
165
197
166
- ### ` getByTitle `
198
+ ### ` ByTitle `
199
+
200
+ > getByTitle, queryByTitle, getAllByTitle, queryAllByTitle
167
201
168
202
``` typescript
169
203
getByTitle (
@@ -189,7 +223,10 @@ Will also find a `title` element within an SVG.
189
223
const closeElement = getByTitle (container, ' Close' )
190
224
```
191
225
192
- ### ` getByDisplayValue `
226
+ ### ` ByDisplayValue `
227
+
228
+ > getByDisplayValue, queryByDisplayValue, getAllByDisplayValue,
229
+ > queryAllByDisplayValue
193
230
194
231
``` typescript
195
232
getByDisplayValue (
@@ -238,7 +275,9 @@ const selectElement = getByDisplayName(container, 'Alaska')
238
275
In case of ` select ` , this will search for a ` <select> ` whose selected ` <option> `
239
276
matches the given [ ` TextMatch ` ] ( #textmatch ) .
240
277
241
- ### ` getByRole `
278
+ ### ` ByRole `
279
+
280
+ > getByRole, queryByRole, getAllByRole, queryAllByRole
242
281
243
282
``` typescript
244
283
getByRole (
@@ -258,7 +297,9 @@ accepts a [`TextMatch`](#textmatch)).
258
297
const dialogContainer = getByRole (container, ' dialog' )
259
298
```
260
299
261
- ### ` getByTestId `
300
+ ### ` ByTestId `
301
+
302
+ > getByTestId, queryByTestId, getAllByTestId, queryAllByTestId
262
303
263
304
``` typescript
264
305
getByTestId (
@@ -391,7 +432,7 @@ getByText(container, (content, element) => {
391
432
392
433
## ` query ` APIs
393
434
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
395
436
complimentary ` query ` API. The ` get ` APIs will throw errors if a proper node
396
437
cannot be found. This is normally the desired effect. However, if you want to
397
438
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')
415
456
expect (submitButtons).toHaveLength (3 ) // expect 3 elements
416
457
expect (submitButtons[0 ]).toBeTruthy ()
417
458
```
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