@@ -473,22 +473,37 @@ fireEvent.click(getElementByText('Submit'), rightClick)
473
473
Several APIs accept a ` TextMatch ` which can be a ` string ` , ` regex ` or a
474
474
` function ` which returns ` true ` for a match and ` false ` for a mismatch .
475
475
476
- Here ' s an example
476
+ See [dom - testing - library #textmatch ][dom - testing - lib - textmatch ] for options .
477
+
478
+ Examples :
477
479
478
480
` ` ` javascript
479
- // <div>Hello World</div>
480
- // all of the following will find the div
481
- getByText('Hello World') // full match
482
- getByText('llo worl') // substring match
483
- getByText('hello world') // strings ignore case
484
- getByText(/Hello W?oRlD/i) // regex
485
- getByText((content, element) => content.startsWith('Hello')) // function
486
-
487
- // all of the following will NOT find the div
488
- getByText('Goodbye World') // non-string match
489
- getByText(/hello world/) // case-sensitive regex with different case
490
- // function looking for a span when it's actually a div
491
- getByText((content, element) => {
481
+ // <div>
482
+ // Hello World
483
+ // </div>
484
+
485
+ // WILL find the div:
486
+
487
+ // Matching a string:
488
+ getByText(container, 'Hello World') // full string match
489
+ getByText(container, 'llo Worl'), {exact: false} // substring match
490
+ getByText(container, 'hello world', {exact: false}) // ignore case
491
+
492
+ // Matching a regex:
493
+ getByText(container, /World/) // substring match
494
+ getByText(container, /world/i) // substring match, ignore case
495
+ getByText(container, /^hello world$/i) // full string match, ignore case
496
+ getByText(container, /Hello W?oRlD/i) // advanced regex
497
+
498
+ // Matching with a custom function:
499
+ getByText(container, (content, element) => content.startsWith('Hello'))
500
+
501
+ // WILL NOT find the div:
502
+
503
+ getByText(container, 'Goodbye World') // full string does not match
504
+ getByText(container, /hello world/) // case-sensitive regex with different case
505
+ // function looking for a span when it's actually a div:
506
+ getByText(container, (content, element) => {
492
507
return element.tagName.toLowerCase() === 'span' && content.startsWith('Hello')
493
508
})
494
509
` ` `
@@ -863,6 +878,7 @@ Links:
863
878
[set -immediate ]: https : // developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate
864
879
[guiding -principle ]: https : // twitter.com/kentcdodds/status/977018512689455106
865
880
[data -testid -blog -post ]: https : // blog.kentcdodds.com/making-your-ui-tests-resilient-to-change-d37a6ee37269
881
+ [dom -testing -lib -textmatch ]: https : // github.com/kentcdodds/dom-testing-library#textmatch
866
882
[bugs ]: https : // github.com/kentcdodds/react-testing-library/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Acreated-desc
867
883
[requests ]: https : // github.com/kentcdodds/react-testing-library/issues?q=is%3Aissue+sort%3Areactions-%2B1-desc+label%3Aenhancement+is%3Aopen
868
884
[good -first -issue ]: https : // github.com/kentcdodds/react-testing-library/issues?utf8=✓&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3A"good+first+issue"+
0 commit comments