@@ -72,19 +72,20 @@ 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 ` ] ( #getbylabeltext )
76
+ * [ ` getByPlaceholderText ` ] ( #getbyplaceholdertext )
77
+ * [ ` getByText ` ] ( #getbytext )
78
+ * [ ` getByAltText ` ] ( #getbyalttext )
79
+ * [ ` getByTitle ` ] ( #getbytitle )
80
+ * [ ` getByTestId ` ] ( #getbytestid )
81
81
* [ ` wait ` ] ( #wait )
82
82
* [ ` waitForElement ` ] ( #waitforelement )
83
- * [ ` fireEvent(node: HTMLElement, event: Event) ` ] ( #fireeventnode-htmlelement-event-event )
83
+ * [ ` fireEvent ` ] ( #fireevent )
84
84
* [ Custom Jest Matchers] ( #custom-jest-matchers )
85
85
* [ Using other assertion libraries] ( #using-other-assertion-libraries )
86
86
* [ ` TextMatch ` ] ( #textmatch )
87
- * [ ExactTextMatch] ( #exacttextmatch )
87
+ * [ Precision] ( #precision )
88
+ * [ TextMatch Examples] ( #textmatch-examples )
88
89
* [ ` query ` APIs] ( #query-apis )
89
90
* [ ` queryAll ` and ` getAll ` APIs] ( #queryall-and-getall-apis )
90
91
* [ ` bindElementToQueries ` ] ( #bindelementtoqueries )
@@ -110,7 +111,10 @@ npm install --save-dev dom-testing-library
110
111
111
112
## Usage
112
113
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.
114
+ Note:
115
+
116
+ * 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.
117
+ * See [ TextMatch] ( #textmatch ) for details on the ` exact ` , ` trim ` , and ` collapseWhitespace ` options.
114
118
115
119
``` javascript
116
120
// src/__tests__/example.js
@@ -179,7 +183,19 @@ test('examples of some things', async () => {
179
183
})
180
184
```
181
185
182
- ### ` getByLabelText(container: HTMLElement, text: TextMatch, options: {selector: string = '*'}): HTMLElement `
186
+ ### ` getByLabelText `
187
+
188
+ ``` typescript
189
+ getByLabelText (
190
+ container : HTMLElement ,
191
+ text : TextMatch ,
192
+ options ?: {
193
+ selector?: string = ' *' ,
194
+ exact?: boolean = true ,
195
+ collapseWhitespace?: boolean = true ,
196
+ trim?: boolean = true ,
197
+ }): HTMLElement
198
+ ```
183
199
184
200
This will search for the label that matches the given [ ` TextMatch ` ] ( #textmatch ) ,
185
201
then find the element associated with that label.
@@ -214,7 +230,18 @@ const inputNode = getByLabelText(container, 'username', {selector: 'input'})
214
230
> want this behavior (for example you wish to assert that it doesn't exist),
215
231
> then use ` queryByLabelText ` instead.
216
232
217
- ### ` getByPlaceholderText(container: HTMLElement, text: TextMatch): HTMLElement `
233
+ ### ` getByPlaceholderText `
234
+
235
+ ``` typescript
236
+ getByPlaceholderText (
237
+ container : HTMLElement ,
238
+ text : TextMatch ,
239
+ options ?: {
240
+ exact?: boolean = true ,
241
+ collapseWhitespace?: boolean = false ,
242
+ trim?: boolean = true ,
243
+ }): HTMLElement
244
+ ```
218
245
219
246
This will search for all elements with a placeholder attribute and find one
220
247
that matches the given [ ` TextMatch ` ] ( #textmatch ) .
@@ -227,7 +254,18 @@ const inputNode = getByPlaceholderText(container, 'Username')
227
254
> NOTE: a placeholder is not a good substitute for a label so you should
228
255
> generally use ` getByLabelText ` instead.
229
256
230
- ### ` getByText(container: HTMLElement, text: TextMatch): HTMLElement `
257
+ ### ` getByText `
258
+
259
+ ``` typescript
260
+ getByText (
261
+ container : HTMLElement ,
262
+ text : TextMatch ,
263
+ options ?: {
264
+ exact?: boolean = true ,
265
+ collapseWhitespace?: boolean = true ,
266
+ trim?: boolean = true ,
267
+ }): HTMLElement
268
+ ```
231
269
232
270
This will search for all elements that have a text node with ` textContent `
233
271
matching the given [ ` TextMatch ` ] ( #textmatch ) .
@@ -237,7 +275,18 @@ matching the given [`TextMatch`](#textmatch).
237
275
const aboutAnchorNode = getByText (container, ' about' )
238
276
```
239
277
240
- ### ` getByAltText(container: HTMLElement, text: TextMatch): HTMLElement `
278
+ ### ` getByAltText `
279
+
280
+ ``` typescript
281
+ getByAltText (
282
+ container : HTMLElement ,
283
+ text : TextMatch ,
284
+ options ?: {
285
+ exact?: boolean = true ,
286
+ collapseWhitespace?: boolean = false ,
287
+ trim?: boolean = true ,
288
+ }): HTMLElement
289
+ ```
241
290
242
291
This will return the element (normally an ` <img> ` ) that has the given ` alt `
243
292
text. Note that it only supports elements which accept an ` alt ` attribute:
@@ -251,19 +300,41 @@ and [`<area>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/area)
251
300
const incrediblesPosterImg = getByAltText (container, / incredibles. * poster$ / i )
252
301
```
253
302
254
- ### ` getByTitle(container: HTMLElement, title: ExactTextMatch): HTMLElement `
303
+ ### ` getByTitle `
255
304
256
- This will return the element that has the matching ` title ` attribute.
305
+ ``` typescript
306
+ getByTitle (
307
+ container : HTMLElement ,
308
+ title : TextMatch ,
309
+ options ?: {
310
+ exact?: boolean = true ,
311
+ collapseWhitespace?: boolean = false ,
312
+ trim?: boolean = true ,
313
+ }): HTMLElement
314
+ ```
315
+
316
+ Returns the element that has the matching ` title ` attribute.
257
317
258
318
``` javascript
259
319
// <span title="Delete" id="2" />
260
320
const deleteElement = getByTitle (container, ' Delete' )
261
321
```
262
322
263
- ### ` getByTestId(container: HTMLElement, text: ExactTextMatch): HTMLElement `
323
+ ### ` getByTestId `
324
+
325
+ ``` typescript
326
+ getByTestId (
327
+ container : HTMLElement ,
328
+ text : TextMatch ,
329
+ options ?: {
330
+ exact?: boolean = true ,
331
+ collapseWhitespace?: boolean = false ,
332
+ trim?: boolean = true ,
333
+ }): HTMLElement `
334
+ ` ` `
264
335
265
336
A shortcut to ` ` container .querySelector (` [data-testid="${yourId }"] ` ) ` ` (and it
266
- also accepts an [ ` ExactTextMatch ` ] ( #exacttextmatch ) ).
337
+ also accepts a [` TextMatch ` ](#textmatch )).
267
338
268
339
` ` ` javascript
269
340
// <input data-testid="username-input" />
@@ -280,8 +351,6 @@ const usernameInputElement = getByTestId(container, 'username-input')
280
351
281
352
### `wait `
282
353
283
- Defined as:
284
-
285
354
```typescript
286
355
function wait (
287
356
callback ?: () => void ,
@@ -323,8 +392,6 @@ intervals.
323
392
324
393
### ` waitForElement `
325
394
326
- Defined as :
327
-
328
395
``` typescript
329
396
function waitForElement<T >(
330
397
callback ? : () => T | null | undefined ,
@@ -383,7 +450,11 @@ The default `timeout` is `4500ms` which will keep you under
383
450
additions and removals of child elements (including text nodes ) in the ` container ` and any of its descendants .
384
451
It won ' t detect attribute changes unless you add `attributes: true` to the options.
385
452
386
- ### ` fireEvent(node: HTMLElement, event: Event) `
453
+ ### ` fireEvent `
454
+
455
+ ` ` ` typescript
456
+ fireEvent(node: HTMLElement, event: Event)
457
+ ` ` `
387
458
388
459
Fire DOM events .
389
460
@@ -398,7 +469,11 @@ fireEvent(
398
469
)
399
470
` ` `
400
471
401
- #### ` fireEvent[eventName](node: HTMLElement, eventProperties: Object) `
472
+ #### ` fireEvent[eventName] `
473
+
474
+ ` ` ` typescript
475
+ fireEvent[eventName](node: HTMLElement, eventProperties: Object)
476
+ ` ` `
402
477
403
478
Convenience methods for firing DOM events . Check out
404
479
[src /events .js ](https :// github.com/kentcdodds/dom-testing-library/blob/master/src/events.js)
@@ -411,7 +486,11 @@ fireEvent.click(getElementByText('Submit'), rightClick)
411
486
// default ` button ` property for click events is set to ` 0 ` which is a left click.
412
487
` ` `
413
488
414
- #### ` getNodeText(node: HTMLElement) `
489
+ #### ` getNodeText `
490
+
491
+ ` ` ` typescript
492
+ getNodeText(node: HTMLElement)
493
+ ` ` `
415
494
416
495
Returns the complete text content of a html element , removing any extra
417
496
whitespace . The intention is to treat text in nodes exactly as how it is
@@ -469,43 +548,50 @@ and add it here!
469
548
Several APIs accept a ` TextMatch ` which can be a ` string ` , ` regex ` or a
470
549
` function ` which returns ` true ` for a match and ` false ` for a mismatch .
471
550
472
- Here ' s an example
551
+ ### Precision
552
+
553
+ Some APIs accept an object as the final argument that can contain options that
554
+ affect the precision of string matching :
555
+
556
+ * ` exact ` : Defaults to ` true ` ; matches full strings , case -sensitive . When false ,
557
+ matches substrings and is not case -sensitive .
558
+ * ` exact ` has no effect on ` regex ` or ` function ` arguments .
559
+ * In most cases using a regex instead of a string gives you more control over
560
+ fuzzy matching and should be preferred over ` { exact: false } ` .
561
+ * ` trim ` : Defaults to ` true ` ; trim leading and trailing whitespace .
562
+ * ` collapseWhitespace ` : Defaults to ` true ` . Collapses inner whitespace (newlines , tabs , repeated spaces ) into a single space .
563
+
564
+ ### TextMatch Examples
473
565
474
566
` ` ` 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
- ` ` `
567
+ // <div>
568
+ // Hello World
569
+ // </div>
570
+
571
+ // WILL find the div:
491
572
492
- ### ExactTextMatch
573
+ // Matching a string:
574
+ getByText(container, 'Hello World') // full string match
575
+ getByText(container, 'llo Worl'), {exact: false} // substring match
576
+ getByText(container, 'hello world', {exact: false}) // ignore case
493
577
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 .
578
+ // Matching a regex:
579
+ getByText(container, /World/) // substring match
580
+ getByText(container, /world/i) // substring match, ignore case
581
+ getByText(container, /^hello world$/i) // full string match, ignore case
582
+ getByText(container, /Hello W?oRlD/i) // advanced regex
497
583
498
- ` ` ` js
499
- // <button data-testid="submit-button">Go</button>
584
+ // Matching with a custom function:
585
+ getByText(container, (content, element) => content.startsWith('Hello'))
500
586
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
587
+ // WILL NOT find the div:
505
588
506
- // all of the following will NOT find the button
507
- getByTestId(container, 'submit-') // no substrings
508
- getByTestId(container, 'Submit-Button') // case-sensitive
589
+ getByText(container, 'Goodbye World') // full string does not match
590
+ getByText(container, /hello world/) // case-sensitive regex with different case
591
+ // function looking for a span when it's actually a div:
592
+ getByText(container, (content, element) => {
593
+ return element.tagName.toLowerCase() === 'span' && content.startsWith('Hello')
594
+ })
509
595
` ` `
510
596
511
597
## ` query ` APIs
0 commit comments