@@ -3,19 +3,44 @@ id: api-queries
33title : 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 
2146getByLabelText (
@@ -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 
75103getByPlaceholderText (
@@ -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 
100130getByText (
@@ -137,7 +167,9 @@ content is in an inline script file, then the script tag could be returned.
137167
138168If you'd rather disable this behavior, set ` ignore `  to ` false ` .
139169
140- ### ` getByAltText `  
170+ ### ` ByAltText `  
171+ 
172+ >  getByAltText, queryByAltText, getAllByAltText, queryAllByAltText
141173
142174``` typescript 
143175getByAltText (
@@ -163,7 +195,9 @@ as it's deprecated).
163195const  incrediblesPosterImg  =  getByAltText (container, / incredibles. * poster$ / i )
164196``` 
165197
166- ### ` getByTitle `  
198+ ### ` ByTitle `  
199+ 
200+ >  getByTitle, queryByTitle, getAllByTitle, queryAllByTitle
167201
168202``` typescript 
169203getByTitle (
@@ -189,7 +223,10 @@ Will also find a `title` element within an SVG.
189223const  closeElement  =  getByTitle (container, ' Close' 
190224``` 
191225
192- ### ` getByDisplayValue `  
226+ ### ` ByDisplayValue `  
227+ 
228+ >  getByDisplayValue, queryByDisplayValue, getAllByDisplayValue,
229+ >  queryAllByDisplayValue
193230
194231``` typescript 
195232getByDisplayValue (
@@ -238,7 +275,9 @@ const selectElement = getByDisplayName(container, 'Alaska')
238275In case of ` select ` , this will search for a ` <select> `  whose selected ` <option> ` 
239276matches the given [ ` TextMatch ` ] ( #textmatch ) .
240277
241- ### ` getByRole `  
278+ ### ` ByRole `  
279+ 
280+ >  getByRole, queryByRole, getAllByRole, queryAllByRole
242281
243282``` typescript 
244283getByRole (
@@ -258,7 +297,9 @@ accepts a [`TextMatch`](#textmatch)).
258297const  dialogContainer  =  getByRole (container, ' dialog' 
259298``` 
260299
261- ### ` getByTestId `  
300+ ### ` ByTestId `  
301+ 
302+ >  getByTestId, queryByTestId, getAllByTestId, queryAllByTestId
262303
263304``` typescript 
264305getByTestId (
@@ -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
395436complimentary ` query `  API. The ` get `  APIs will throw errors if a proper node
396437cannot be found. This is normally the desired effect. However, if you want to
397438make an assertion that an element is _ not_  present in the DOM, then you can use
@@ -415,20 +456,3 @@ const submitButtons = queryAllByText(container, 'submit')
415456expect (submitButtons).toHaveLength (3 ) //  expect 3 elements
416457expect (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