Skip to content

Commit 1dfcd73

Browse files
alexkrolickKent C. Dodds
authored and
Kent C. Dodds
committed
fix(TS): examples + types (#77)
* Update README with new match examples * Update types (probably invalid typescript syntax) * Remove example that messes up TOC * Fix union type
1 parent e7809dd commit 1dfcd73

File tree

2 files changed

+63
-25
lines changed

2 files changed

+63
-25
lines changed

README.md

+30-14
Original file line numberDiff line numberDiff line change
@@ -473,22 +473,37 @@ fireEvent.click(getElementByText('Submit'), rightClick)
473473
Several APIs accept a `TextMatch` which can be a `string`, `regex` or a
474474
`function` which returns `true` for a match and `false` for a mismatch.
475475

476-
Here's an example
476+
See [dom-testing-library#textmatch][dom-testing-lib-textmatch] for options.
477+
478+
Examples:
477479

478480
```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) => {
492507
return element.tagName.toLowerCase() === 'span' && content.startsWith('Hello')
493508
})
494509
```
@@ -863,6 +878,7 @@ Links:
863878
[set-immediate]: https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate
864879
[guiding-principle]: https://twitter.com/kentcdodds/status/977018512689455106
865880
[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
866882
[bugs]: https://github.com/kentcdodds/react-testing-library/issues?q=is%3Aissue+is%3Aopen+label%3Abug+sort%3Acreated-desc
867883
[requests]: https://github.com/kentcdodds/react-testing-library/issues?q=is%3Aissue+sort%3Areactions-%2B1-desc+label%3Aenhancement+is%3Aopen
868884
[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"+

typings/index.d.ts

+33-11
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,44 @@ import {Simulate as ReactSimulate} from 'react-dom/test-utils'
22

33
type TextMatchFunction = (content: string, element: HTMLElement) => boolean
44
type TextMatch = string | RegExp | TextMatchFunction
5-
type ExactTextMatch = string | RegExp | TextMatchFunction
5+
type TextMatchOptions = {
6+
exact?: boolean = false
7+
trim?: boolean = true
8+
collapseWhitespace?: boolean = true
9+
}
610

711
interface RenderResult {
812
container: HTMLDivElement
913
rerender: (ui: React.ReactElement<any>) => void
1014
unmount: VoidFunction
11-
queryByTestId: (id: ExactTextMatch) => HTMLElement | null
12-
getByTestId: (id: ExactTextMatch) => HTMLElement
13-
queryByText: (id: TextMatch) => HTMLElement | null
14-
getByText: (text: TextMatch) => HTMLElement
15-
queryByPlaceholderText: (id: TextMatch) => HTMLElement | null
16-
getByPlaceholderText: (text: TextMatch) => HTMLElement
17-
queryByLabelText: (text: TextMatch) => HTMLElement | null
18-
getByLabelText: (id: TextMatch, options?: {selector: string}) => HTMLElement
19-
queryByAltText: (text: TextMatch) => HTMLElement | null
20-
getByAltText: (text: TextMatch) => HTMLElement
15+
queryByTestId: (
16+
id: TextMatch,
17+
options?: TextMatchOptions,
18+
) => HTMLElement | null
19+
getByTestId: (id: TextMatch, options?: TextMatchOptions) => HTMLElement
20+
queryByText: (id: TextMatch, options?: TextMatchOptions) => HTMLElement | null
21+
getByText: (text: TextMatch, options?: TextMatchOptions) => HTMLElement
22+
queryByPlaceholderText: (
23+
id: TextMatch,
24+
options?: TextMatchOptions,
25+
) => HTMLElement | null
26+
getByPlaceholderText: (
27+
text: TextMatch,
28+
options?: TextMatchOptions,
29+
) => HTMLElement
30+
queryByLabelText: (
31+
text: TextMatch,
32+
options?: TextMatchOptions,
33+
) => HTMLElement | null
34+
getByLabelText: (
35+
id: TextMatch,
36+
options?: {selector?: string} & TextMatchOptions,
37+
) => HTMLElement
38+
queryByAltText: (
39+
text: TextMatch,
40+
options?: TextMatchOptions,
41+
) => HTMLElement | null
42+
getByAltText: (text: TextMatch, options?: TextMatchOptions) => HTMLElement
2143
}
2244

2345
export function render(

0 commit comments

Comments
 (0)