@@ -72,19 +72,19 @@ when a real user uses it.
72
72
73
73
* [ Installation] ( #installation )
74
74
* [ Usage] ( #usage )
75
- * [ ` getByLabelText(container: HTMLElement, text: TextMatch, options: {selector: string = '*'}): HTMLElement ` ] ( #getbylabeltextcontainer-htmlelement-text-textmatch-options-selector-string---htmlelement )
76
- * [ ` getByPlaceholderText(container: HTMLElement, text: TextMatch): HTMLElement ` ] ( #getbyplaceholdertextcontainer-htmlelement-text-textmatch-htmlelement )
77
- * [ ` getByText(container: HTMLElement, text: TextMatch): HTMLElement ` ] ( #getbytextcontainer-htmlelement-text-textmatch-htmlelement )
78
- * [ ` getByAltText(container: HTMLElement, text: TextMatch): HTMLElement ` ] ( #getbyalttextcontainer-htmlelement-text-textmatch-htmlelement )
79
- * [ ` getByTitle(container: HTMLElement, title: ExactTextMatch): HTMLElement ` ] ( #getbytitlecontainer-htmlelement-title-exacttextmatch-htmlelement )
80
- * [ ` getByTestId(container: HTMLElement, text: ExactTextMatch): HTMLElement ` ] ( #getbytestidcontainer-htmlelement-text-exacttextmatch-htmlelement )
75
+ * [ ` getByLabelText(container: HTMLElement, text: TextMatch, options: {selector: string = '*', exact: boolean = true}): HTMLElement ` ] ( #getbylabeltextcontainer-htmlelement-text-textmatch-options-selector-string---exact-boolean--true-htmlelement )
76
+ * [ ` getByPlaceholderText(container: HTMLElement, text: TextMatch, {exact: boolean = true}): HTMLElement ` ] ( #getbyplaceholdertextcontainer-htmlelement-text-textmatch-exact-boolean--true-htmlelement )
77
+ * [ ` getByText(container: HTMLElement, text: TextMatch, {exact: boolean = true}): HTMLElement ` ] ( #getbytextcontainer-htmlelement-text-textmatch-exact-boolean--true-htmlelement )
78
+ * [ ` getByAltText(container: HTMLElement, text: TextMatch, {exact: boolean = true}): HTMLElement ` ] ( #getbyalttextcontainer-htmlelement-text-textmatch-exact-boolean--true-htmlelement )
79
+ * [ ` getByTestId(container: HTMLElement, text: ExactTextMatch, {exact: boolean = true}): HTMLElement ` ] ( #getbytestidcontainer-htmlelement-text-exacttextmatch-exact-boolean--true-htmlelement )
81
80
* [ ` wait ` ] ( #wait )
82
81
* [ ` waitForElement ` ] ( #waitforelement )
83
82
* [ ` fireEvent(node: HTMLElement, event: Event) ` ] ( #fireeventnode-htmlelement-event-event )
84
83
* [ Custom Jest Matchers] ( #custom-jest-matchers )
85
84
* [ Using other assertion libraries] ( #using-other-assertion-libraries )
86
85
* [ ` TextMatch ` ] ( #textmatch )
87
- * [ ExactTextMatch] ( #exacttextmatch )
86
+ * [ Precision] ( #precision )
87
+ * [ TextMatch Examples] ( #textmatch-examples )
88
88
* [ ` query ` APIs] ( #query-apis )
89
89
* [ ` queryAll ` and ` getAll ` APIs] ( #queryall-and-getall-apis )
90
90
* [ ` bindElementToQueries ` ] ( #bindelementtoqueries )
@@ -110,7 +110,10 @@ npm install --save-dev dom-testing-library
110
110
111
111
## Usage
112
112
113
- Note: each of the ` get ` APIs below have a matching [ ` getAll ` ] ( #queryall-and-getall-apis ) API that returns all elements instead of just the first one, and [ ` query ` ] ( #query-apis ) /[ ` getAll ` ] ( #queryall-and-getall-apis ) that return ` null ` /` [] ` instead of throwing an error.
113
+ Note:
114
+
115
+ * Each of the ` get ` APIs below have a matching [ ` getAll ` ] ( #queryall-and-getall-apis ) API that returns all elements instead of just the first one, and [ ` query ` ] ( #query-apis ) /[ ` getAll ` ] ( #queryall-and-getall-apis ) that return ` null ` /` [] ` instead of throwing an error.
116
+ * Setting ` exact: false ` in the final option argument of a ` get ` API causes the query to use fuzzy matching. See [ TextMatch] ( #textmatch ) for details.
114
117
115
118
``` javascript
116
119
// src/__tests__/example.js
@@ -179,7 +182,7 @@ test('examples of some things', async () => {
179
182
})
180
183
```
181
184
182
- ### ` getByLabelText(container: HTMLElement, text: TextMatch, options: {selector: string = '*'}): HTMLElement `
185
+ ### ` getByLabelText(container: HTMLElement, text: TextMatch, options: {selector: string = '*', exact: boolean = true }): HTMLElement `
183
186
184
187
This will search for the label that matches the given [ ` TextMatch ` ] ( #textmatch ) ,
185
188
then find the element associated with that label.
@@ -214,7 +217,7 @@ const inputNode = getByLabelText(container, 'username', {selector: 'input'})
214
217
> want this behavior (for example you wish to assert that it doesn't exist),
215
218
> then use ` queryByLabelText ` instead.
216
219
217
- ### ` getByPlaceholderText(container: HTMLElement, text: TextMatch): HTMLElement `
220
+ ### ` getByPlaceholderText(container: HTMLElement, text: TextMatch, {exact: boolean = true} ): HTMLElement `
218
221
219
222
This will search for all elements with a placeholder attribute and find one
220
223
that matches the given [ ` TextMatch ` ] ( #textmatch ) .
@@ -227,7 +230,7 @@ const inputNode = getByPlaceholderText(container, 'Username')
227
230
> NOTE: a placeholder is not a good substitute for a label so you should
228
231
> generally use ` getByLabelText ` instead.
229
232
230
- ### ` getByText(container: HTMLElement, text: TextMatch): HTMLElement `
233
+ ### ` getByText(container: HTMLElement, text: TextMatch, {exact: boolean = true} ): HTMLElement `
231
234
232
235
This will search for all elements that have a text node with ` textContent `
233
236
matching the given [ ` TextMatch ` ] ( #textmatch ) .
@@ -237,7 +240,7 @@ matching the given [`TextMatch`](#textmatch).
237
240
const aboutAnchorNode = getByText (container, ' about' )
238
241
```
239
242
240
- ### ` getByAltText(container: HTMLElement, text: TextMatch): HTMLElement `
243
+ ### ` getByAltText(container: HTMLElement, text: TextMatch, {exact: boolean = true} ): HTMLElement `
241
244
242
245
This will return the element (normally an ` <img> ` ) that has the given ` alt `
243
246
text. Note that it only supports elements which accept an ` alt ` attribute:
@@ -260,7 +263,7 @@ This will return the element that has the matching `title` attribute.
260
263
const deleteElement = getByTitle (container, ' Delete' )
261
264
```
262
265
263
- ### ` getByTestId(container: HTMLElement, text: ExactTextMatch): HTMLElement `
266
+ ### ` getByTestId(container: HTMLElement, text: ExactTextMatch, {exact: boolean = true} ): HTMLElement `
264
267
265
268
A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` (and it
266
269
also accepts an [ ` ExactTextMatch ` ] ( #exacttextmatch ) ).
@@ -469,43 +472,52 @@ and add it here!
469
472
Several APIs accept a ` TextMatch ` which can be a ` string ` , ` regex ` or a
470
473
` function ` which returns ` true ` for a match and ` false ` for a mismatch .
471
474
472
- Here ' s an example
475
+ ### Precision
476
+
477
+ Queries that search inner tag content (i .e ., ` queryByLabelText ` ,
478
+ ` queryByText ` ) collapse and trim whitespace (newlines , spaces , tabs );
479
+ queries using attributes do not .
480
+
481
+ Some APIs accept an object as the final argument that can contain options that
482
+ affect the precision of string matching :
483
+
484
+ * ` exact ` : Defaults to ` true ` ; matches full strings , case -sensitive . When false ,
485
+ matches substrings and is not case -sensitive .
486
+ * ` exact ` has no effect on ` regex ` or ` function ` arguments .
487
+ * In most cases using a regex instead of a string gives you more control over
488
+ fuzzy matching and should be preferred over ` { exact: false } ` .
489
+
490
+ ### TextMatch Examples
473
491
474
492
` ` ` javascript
475
- // <div>Hello World</div>
476
- // all of the following will find the div
477
- getByText(container, 'Hello World') // full match
478
- getByText(container, 'llo worl') // substring match
479
- getByText(container, 'hello world') // strings ignore case
480
- getByText(container, /Hello W?oRlD/i) // regex
481
- getByText(container, (content, element) => content.startsWith('Hello')) // function
482
-
483
- // all of the following will NOT find the div
484
- getByText(container, 'Goodbye World') // non-string match
485
- getByText(container, /hello world/) // case-sensitive regex with different case
486
- // function looking for a span when it's actually a div
487
- getByText(container, (content, element) => {
488
- return element.tagName.toLowerCase() === 'span' && content.startsWith('Hello')
489
- })
490
- ` ` `
493
+ // <div>
494
+ // Hello World
495
+ // </div>
496
+
497
+ // WILL find the div:
491
498
492
- ### ExactTextMatch
499
+ // Matching a string:
500
+ getByText(container, 'Hello World') // full string match
501
+ getByText(container, 'llo Worl'), {exact: false} // substring match
502
+ getByText(container, 'hello world', {exact: false}) // ignore case
493
503
494
- Some APIs use ExactTextMatch , which is the same as TextMatch but case -sensitive
495
- and does not match substrings ; however , regexes and functions are also accepted
496
- for custom matching .
504
+ // Matching a regex:
505
+ getByText(container, /World/) // substring match
506
+ getByText(container, /world/i) // substring match, ignore case
507
+ getByText(container, /^hello world$/i) // full string match, ignore case
508
+ getByText(container, /Hello W?oRlD/i) // advanced regex
497
509
498
- ` ` ` js
499
- // <button data-testid="submit-button">Go</button>
510
+ // Matching with a custom function:
511
+ getByText(container, (content, element) => content.startsWith('Hello'))
500
512
501
- // all of the following will find the button
502
- getByTestId(container, 'submit-button') // exact match
503
- getByTestId(container, /submit*/) // regex match
504
- getByTestId(container, content => content.startsWith('submit')) // function
513
+ // WILL NOT find the div:
505
514
506
- // all of the following will NOT find the button
507
- getByTestId(container, 'submit-') // no substrings
508
- getByTestId(container, 'Submit-Button') // case-sensitive
515
+ getByText(container, 'Goodbye World') // full string does not match
516
+ getByText(container, /hello world/) // case-sensitive regex with different case
517
+ // function looking for a span when it's actually a div:
518
+ getByText(container, (content, element) => {
519
+ return element.tagName.toLowerCase() === 'span' && content.startsWith('Hello')
520
+ })
509
521
` ` `
510
522
511
523
## ` query ` APIs
0 commit comments