Skip to content

Commit 85819c3

Browse files
committed
Update docs
1 parent 686e453 commit 85819c3

File tree

1 file changed

+55
-42
lines changed

1 file changed

+55
-42
lines changed

README.md

+55-42
Original file line numberDiff line numberDiff line change
@@ -72,18 +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-
* [`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)
8080
* [`wait`](#wait)
8181
* [`waitForElement`](#waitforelement)
8282
* [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event)
8383
* [Custom Jest Matchers](#custom-jest-matchers)
8484
* [Using other assertion libraries](#using-other-assertion-libraries)
8585
* [`TextMatch`](#textmatch)
86-
* [ExactTextMatch](#exacttextmatch)
86+
* [Precision](#precision)
87+
* [TextMatch Examples](#textmatch-examples)
8788
* [`query` APIs](#query-apis)
8889
* [`queryAll` and `getAll` APIs](#queryall-and-getall-apis)
8990
* [`bindElementToQueries`](#bindelementtoqueries)
@@ -109,7 +110,10 @@ npm install --save-dev dom-testing-library
109110

110111
## Usage
111112

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

114118
```javascript
115119
// src/__tests__/example.js
@@ -178,7 +182,7 @@ test('examples of some things', async () => {
178182
})
179183
```
180184

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

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

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

231235
This will search for all elements that have a text node with `textContent`
232236
matching the given [`TextMatch`](#textmatch).
@@ -236,7 +240,7 @@ matching the given [`TextMatch`](#textmatch).
236240
const aboutAnchorNode = getByText(container, 'about')
237241
```
238242

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

241245
This will return the element (normally an `<img>`) that has the given `alt`
242246
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)
250254
const incrediblesPosterImg = getByAltText(container, /incredibles.*poster$/i)
251255
```
252256

253-
### `getByTestId(container: HTMLElement, text: ExactTextMatch): HTMLElement`
257+
### `getByTestId(container: HTMLElement, text: ExactTextMatch, {exact: boolean = true}): HTMLElement`
254258

255259
A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` (and it
256260
also accepts an [`ExactTextMatch`](#exacttextmatch)).
@@ -459,43 +463,52 @@ and add it here!
459463
Several APIs accept a `TextMatch` which can be a `string`, `regex` or a
460464
`function` which returns `true` for a match and `false` for a mismatch.
461465

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
463482

464483
```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:
481489
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
483494
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
487500
488-
```js
489-
// <button data-testid="submit-button">Go</button>
501+
// Matching with a custom function:
502+
getByText(container, (content, element) => content.startsWith('Hello'))
490503
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:
495505
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+
})
499512
```
500513

501514
## `query` APIs

0 commit comments

Comments
 (0)