@@ -72,18 +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
- * [ ` 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 )
80
80
* [ ` wait ` ] ( #wait )
81
81
* [ ` waitForElement ` ] ( #waitforelement )
82
82
* [ ` fireEvent(node: HTMLElement, event: Event) ` ] ( #fireeventnode-htmlelement-event-event )
83
83
* [ Custom Jest Matchers] ( #custom-jest-matchers )
84
84
* [ Using other assertion libraries] ( #using-other-assertion-libraries )
85
85
* [ ` TextMatch ` ] ( #textmatch )
86
- * [ ExactTextMatch] ( #exacttextmatch )
86
+ * [ Precision] ( #precision )
87
+ * [ TextMatch Examples] ( #textmatch-examples )
87
88
* [ ` query ` APIs] ( #query-apis )
88
89
* [ ` queryAll ` and ` getAll ` APIs] ( #queryall-and-getall-apis )
89
90
* [ ` bindElementToQueries ` ] ( #bindelementtoqueries )
@@ -109,7 +110,10 @@ npm install --save-dev dom-testing-library
109
110
110
111
## Usage
111
112
112
- 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.
113
117
114
118
``` javascript
115
119
// src/__tests__/example.js
@@ -178,7 +182,7 @@ test('examples of some things', async () => {
178
182
})
179
183
```
180
184
181
- ### ` getByLabelText(container: HTMLElement, text: TextMatch, options: {selector: string = '*'}): HTMLElement `
185
+ ### ` getByLabelText(container: HTMLElement, text: TextMatch, options: {selector: string = '*', exact: boolean = true }): HTMLElement `
182
186
183
187
This will search for the label that matches the given [ ` TextMatch ` ] ( #textmatch ) ,
184
188
then find the element associated with that label.
@@ -213,7 +217,7 @@ const inputNode = getByLabelText(container, 'username', {selector: 'input'})
213
217
> want this behavior (for example you wish to assert that it doesn't exist),
214
218
> then use ` queryByLabelText ` instead.
215
219
216
- ### ` getByPlaceholderText(container: HTMLElement, text: TextMatch): HTMLElement `
220
+ ### ` getByPlaceholderText(container: HTMLElement, text: TextMatch, {exact: boolean = true} ): HTMLElement `
217
221
218
222
This will search for all elements with a placeholder attribute and find one
219
223
that matches the given [ ` TextMatch ` ] ( #textmatch ) .
@@ -226,7 +230,7 @@ const inputNode = getByPlaceholderText(container, 'Username')
226
230
> NOTE: a placeholder is not a good substitute for a label so you should
227
231
> generally use ` getByLabelText ` instead.
228
232
229
- ### ` getByText(container: HTMLElement, text: TextMatch): HTMLElement `
233
+ ### ` getByText(container: HTMLElement, text: TextMatch, {exact: boolean = true} ): HTMLElement `
230
234
231
235
This will search for all elements that have a text node with ` textContent `
232
236
matching the given [ ` TextMatch ` ] ( #textmatch ) .
@@ -236,7 +240,7 @@ matching the given [`TextMatch`](#textmatch).
236
240
const aboutAnchorNode = getByText (container, ' about' )
237
241
```
238
242
239
- ### ` getByAltText(container: HTMLElement, text: TextMatch): HTMLElement `
243
+ ### ` getByAltText(container: HTMLElement, text: TextMatch, {exact: boolean = true} ): HTMLElement `
240
244
241
245
This will return the element (normally an ` <img> ` ) that has the given ` alt `
242
246
text. Note that it only supports elements which accept an ` alt ` attribute:
@@ -250,7 +254,7 @@ and [`<area>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area)
250
254
const incrediblesPosterImg = getByAltText (container, / incredibles. * poster$ / i )
251
255
```
252
256
253
- ### ` getByTestId(container: HTMLElement, text: ExactTextMatch): HTMLElement `
257
+ ### ` getByTestId(container: HTMLElement, text: ExactTextMatch, {exact: boolean = true} ): HTMLElement `
254
258
255
259
A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` (and it
256
260
also accepts an [ ` ExactTextMatch ` ] ( #exacttextmatch ) ).
@@ -459,43 +463,52 @@ and add it here!
459
463
Several APIs accept a ` TextMatch ` which can be a ` string ` , ` regex ` or a
460
464
` function ` which returns ` true ` for a match and ` false ` for a mismatch .
461
465
462
- Here ' s an example
466
+ ### Precision
467
+
468
+ Queries that search inner tag content (i .e ., ` queryByLabelText ` ,
469
+ ` queryByText ` ) collapse and trim whitespace (newlines , spaces , tabs );
470
+ queries using attributes do not .
471
+
472
+ Some APIs accept an object as the final argument that can contain options that
473
+ affect the precision of string matching :
474
+
475
+ * ` exact ` : Defaults to ` true ` ; matches full strings , case -sensitive . When false ,
476
+ matches substrings and is not case -sensitive .
477
+ * ` exact ` has no effect on ` regex ` or ` function ` arguments .
478
+ * In most cases using a regex instead of a string gives you more control over
479
+ fuzzy matching and should be preferred over ` { exact: false } ` .
480
+
481
+ ### TextMatch Examples
463
482
464
483
` ` ` javascript
465
- // <div>Hello World</div>
466
- // all of the following will find the div
467
- getByText(container, 'Hello World') // full match
468
- getByText(container, 'llo worl') // substring match
469
- getByText(container, 'hello world') // strings ignore case
470
- getByText(container, /Hello W?oRlD/i) // regex
471
- getByText(container, (content, element) => content.startsWith('Hello')) // function
472
-
473
- // all of the following will NOT find the div
474
- getByText(container, 'Goodbye World') // non-string match
475
- getByText(container, /hello world/) // case-sensitive regex with different case
476
- // function looking for a span when it's actually a div
477
- getByText(container, (content, element) => {
478
- return element.tagName.toLowerCase() === 'span' && content.startsWith('Hello')
479
- })
480
- ` ` `
484
+ // <div>
485
+ // Hello World
486
+ // </div>
487
+
488
+ // WILL find the div:
481
489
482
- ### ExactTextMatch
490
+ // Matching a string:
491
+ getByText(container, 'Hello World') // full string match
492
+ getByText(container, 'llo Worl'), {exact: false} // substring match
493
+ getByText(container, 'hello world', {exact: false}) // ignore case
483
494
484
- Some APIs use ExactTextMatch , which is the same as TextMatch but case -sensitive
485
- and does not match substrings ; however , regexes and functions are also accepted
486
- for custom matching .
495
+ // Matching a regex:
496
+ getByText(container, /World/) // substring match
497
+ getByText(container, /world/i) // substring match, ignore case
498
+ getByText(container, /^hello world$/i) // full string match, ignore case
499
+ getByText(container, /Hello W?oRlD/i) // advanced regex
487
500
488
- ` ` ` js
489
- // <button data-testid="submit-button">Go</button>
501
+ // Matching with a custom function:
502
+ getByText(container, (content, element) => content.startsWith('Hello'))
490
503
491
- // all of the following will find the button
492
- getByTestId(container, 'submit-button') // exact match
493
- getByTestId(container, /submit*/) // regex match
494
- getByTestId(container, content => content.startsWith('submit')) // function
504
+ // WILL NOT find the div:
495
505
496
- // all of the following will NOT find the button
497
- getByTestId(container, 'submit-') // no substrings
498
- getByTestId(container, 'Submit-Button') // case-sensitive
506
+ getByText(container, 'Goodbye World') // full string does not match
507
+ getByText(container, /hello world/) // case-sensitive regex with different case
508
+ // function looking for a span when it's actually a div:
509
+ getByText(container, (content, element) => {
510
+ return element.tagName.toLowerCase() === 'span' && content.startsWith('Hello')
511
+ })
499
512
` ` `
500
513
501
514
## ` query ` APIs
0 commit comments