Skip to content

Commit 3bf940a

Browse files
committed
Update docs
1 parent 5b0a717 commit 3bf940a

File tree

1 file changed

+55
-43
lines changed

1 file changed

+55
-43
lines changed

README.md

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ when a real user uses it.
7272

7373
* [Installation](#installation)
7474
* [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)
8180
* [`wait`](#wait)
8281
* [`waitForElement`](#waitforelement)
8382
* [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event)
8483
* [Custom Jest Matchers](#custom-jest-matchers)
8584
* [Using other assertion libraries](#using-other-assertion-libraries)
8685
* [`TextMatch`](#textmatch)
87-
* [ExactTextMatch](#exacttextmatch)
86+
* [Precision](#precision)
87+
* [TextMatch Examples](#textmatch-examples)
8888
* [`query` APIs](#query-apis)
8989
* [`queryAll` and `getAll` APIs](#queryall-and-getall-apis)
9090
* [`bindElementToQueries`](#bindelementtoqueries)
@@ -110,7 +110,10 @@ npm install --save-dev dom-testing-library
110110

111111
## Usage
112112

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.
114117

115118
```javascript
116119
// src/__tests__/example.js
@@ -179,7 +182,7 @@ test('examples of some things', async () => {
179182
})
180183
```
181184

182-
### `getByLabelText(container: HTMLElement, text: TextMatch, options: {selector: string = '*'}): HTMLElement`
185+
### `getByLabelText(container: HTMLElement, text: TextMatch, options: {selector: string = '*', exact: boolean = true}): HTMLElement`
183186

184187
This will search for the label that matches the given [`TextMatch`](#textmatch),
185188
then find the element associated with that label.
@@ -214,7 +217,7 @@ const inputNode = getByLabelText(container, 'username', {selector: 'input'})
214217
> want this behavior (for example you wish to assert that it doesn't exist),
215218
> then use `queryByLabelText` instead.
216219
217-
### `getByPlaceholderText(container: HTMLElement, text: TextMatch): HTMLElement`
220+
### `getByPlaceholderText(container: HTMLElement, text: TextMatch, {exact: boolean = true}): HTMLElement`
218221

219222
This will search for all elements with a placeholder attribute and find one
220223
that matches the given [`TextMatch`](#textmatch).
@@ -227,7 +230,7 @@ const inputNode = getByPlaceholderText(container, 'Username')
227230
> NOTE: a placeholder is not a good substitute for a label so you should
228231
> generally use `getByLabelText` instead.
229232
230-
### `getByText(container: HTMLElement, text: TextMatch): HTMLElement`
233+
### `getByText(container: HTMLElement, text: TextMatch, {exact: boolean = true}): HTMLElement`
231234

232235
This will search for all elements that have a text node with `textContent`
233236
matching the given [`TextMatch`](#textmatch).
@@ -237,7 +240,7 @@ matching the given [`TextMatch`](#textmatch).
237240
const aboutAnchorNode = getByText(container, 'about')
238241
```
239242

240-
### `getByAltText(container: HTMLElement, text: TextMatch): HTMLElement`
243+
### `getByAltText(container: HTMLElement, text: TextMatch, {exact: boolean = true}): HTMLElement`
241244

242245
This will return the element (normally an `<img>`) that has the given `alt`
243246
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.
260263
const deleteElement = getByTitle(container, 'Delete')
261264
```
262265

263-
### `getByTestId(container: HTMLElement, text: ExactTextMatch): HTMLElement`
266+
### `getByTestId(container: HTMLElement, text: ExactTextMatch, {exact: boolean = true}): HTMLElement`
264267

265268
A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` (and it
266269
also accepts an [`ExactTextMatch`](#exacttextmatch)).
@@ -469,43 +472,52 @@ and add it here!
469472
Several APIs accept a `TextMatch` which can be a `string`, `regex` or a
470473
`function` which returns `true` for a match and `false` for a mismatch.
471474

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
473491

474492
```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:
491498
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
493503
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
497509
498-
```js
499-
// <button data-testid="submit-button">Go</button>
510+
// Matching with a custom function:
511+
getByText(container, (content, element) => content.startsWith('Hello'))
500512
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:
505514
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+
})
509521
```
510522

511523
## `query` APIs

0 commit comments

Comments
 (0)