Skip to content

Commit 507aa49

Browse files
committed
Suggested changes pet Kent C. Dodds
1 parent 97bd1e8 commit 507aa49

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

README.md

+19-18
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,10 @@ Returns the element that has the matching value.
351351
const lastNameInput = getByValue('Norris')
352352
```
353353

354-
### `getByTestId`
354+
### `getByRole`
355355

356356
```typescript
357-
getByTestId(
357+
getByRole(
358358
container: HTMLElement,
359359
text: TextMatch,
360360
options?: {
@@ -364,26 +364,18 @@ getByTestId(
364364
}): HTMLElement`
365365
```
366366

367-
A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` (and it
367+
A shortcut to `` container.querySelector(`[role="${yourRole}"]`) `` (and it
368368
also accepts a [`TextMatch`](#textmatch)).
369369

370370
```javascript
371-
// <input data-testid="username-input" />
372-
const usernameInputElement = getByTestId(container, 'username-input')
371+
// <div role="dialog">...</div>
372+
const dialogContainer = getByTestrole(container, 'dialog')
373373
```
374374

375-
> In the spirit of [the guiding principles](#guiding-principles), it is
376-
> recommended to use this only after the other queries don't work for your use
377-
> case. Using data-testid attributes do not resemble how your software is used
378-
> and should be avoided if possible. That said, they are _way_ better than
379-
> querying based on DOM structure or styling css class names. Learn more about
380-
> `data-testid`s from the blog post
381-
> ["Making your UI tests resilient to change"][data-testid-blog-post]
382-
383-
### `getByRole`
375+
### `getByTestId`
384376

385377
```typescript
386-
getByRole(
378+
getByTestId(
387379
container: HTMLElement,
388380
text: TextMatch,
389381
options?: {
@@ -393,14 +385,22 @@ getByRole(
393385
}): HTMLElement`
394386
```
395387

396-
A shortcut to `` container.querySelector(`[role="${yourRole}"]`) `` (and it
388+
A shortcut to `` container.querySelector(`[data-testid="${yourId}"]`) `` (and it
397389
also accepts a [`TextMatch`](#textmatch)).
398390

399391
```javascript
400-
// <div role="dialog">...</div>
401-
const dialogContainer = getByTestrole(container, 'dialog')
392+
// <input data-testid="username-input" />
393+
const usernameInputElement = getByTestId(container, 'username-input')
402394
```
403395

396+
> In the spirit of [the guiding principles](#guiding-principles), it is
397+
> recommended to use this only after the other queries don't work for your use
398+
> case. Using data-testid attributes do not resemble how your software is used
399+
> and should be avoided if possible. That said, they are _way_ better than
400+
> querying based on DOM structure or styling css class names. Learn more about
401+
> `data-testid`s from the blog post
402+
> ["Making your UI tests resilient to change"][data-testid-blog-post]
403+
404404
### `wait`
405405

406406
```typescript
@@ -955,6 +955,7 @@ Thanks goes to these people ([emoji key][emojis]):
955955
| [<img src="https://avatars3.githubusercontent.com/u/725236?v=4" width="100px;"/><br /><sub><b>Alex Cook</b></sub>](https://github.com/alecook)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [💡](#example-alecook "Examples") | [<img src="https://avatars3.githubusercontent.com/u/10348212?v=4" width="100px;"/><br /><sub><b>Daniel Cook</b></sub>](https://github.com/dfcook)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Tests") | [<img src="https://avatars2.githubusercontent.com/u/21194045?s=400&v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [<img src="https://avatars1.githubusercontent.com/u/28659384?v=4" width="100px;"/><br /><sub><b>Tim Deschryver</b></sub>](https://github.com/tdeschryver)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=maddijoyce "Code") | [<img src="https://avatars1.githubusercontent.com/u/25429764?v=4" width="100px;"/><br /><sub><b>Peter Kamps</b></sub>](https://github.com/npeterkamps)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Anpeterkamps "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Tests") |
956956
| [<img src="https://avatars2.githubusercontent.com/u/21689428?v=4" width="100px;"/><br /><sub><b>Jonathan Stoye</b></sub>](http://jonathanstoye.de)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonathanStoye "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/4126644?v=4" width="100px;"/><br /><sub><b>Sanghyeon Lee</b></sub>](https://github.com/yongdamsh)<br />[💡](#example-yongdamsh "Examples") | [<img src="https://avatars3.githubusercontent.com/u/8015514?v=4" width="100px;"/><br /><sub><b>Justice Mba </b></sub>](https://github.com/Dajust)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Documentation") [🤔](#ideas-Dajust "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/340761?v=4" width="100px;"/><br /><sub><b>Wayne Crouch</b></sub>](https://github.com/wgcrouch)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wgcrouch "Code") | [<img src="https://avatars1.githubusercontent.com/u/4996462?v=4" width="100px;"/><br /><sub><b>Ben Elliott</b></sub>](http://benjaminelliott.co.uk)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=benelliott "Code") | [<img src="https://avatars3.githubusercontent.com/u/577921?v=4" width="100px;"/><br /><sub><b>Ruben Costa</b></sub>](http://nuances.co)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=rubencosta "Code") | [<img src="https://avatars2.githubusercontent.com/u/4982001?v=4" width="100px;"/><br /><sub><b>Robert Smith</b></sub>](http://rbrtsmith.com/)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Arbrtsmith "Bug reports") [🤔](#ideas-rbrtsmith "Ideas, Planning, & Feedback") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=rbrtsmith "Documentation") |
957957
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") | [<img src="https://avatars1.githubusercontent.com/u/186971?v=4" width="100px;"/><br /><sub><b>Neil Kistner</b></sub>](https://neilkistner.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wyze "Code") | [<img src="https://avatars3.githubusercontent.com/u/1448597?v=4" width="100px;"/><br /><sub><b>Ben Chauvette</b></sub>](http://bdchauvette.net/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=bdchauvette "Code") | [<img src="https://avatars2.githubusercontent.com/u/777527?v=4" width="100px;"/><br /><sub><b>Jeff Baumgardt</b></sub>](https://github.com/JeffBaumgardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Documentation") |
958+
958959
<!-- ALL-CONTRIBUTORS-LIST:END -->
959960

960961
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/element-queries.js

-6
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,7 @@ test('using jest helpers to check element role', () => {
390390
</div>
391391
`)
392392

393-
expect(getByRole('dialog')).toBeTruthy()
394393
expect(getByRole('dialog')).toHaveTextContent('Contents')
395-
396-
expect(() => expect(getByRole('dialog2')).not.toBeTruthy()).toThrowError()
397-
expect(() =>
398-
expect(getByRole('dialog')).toHaveTextContent('Something'),
399-
).toThrowError()
400394
})
401395

402396
test('test the debug helper prints the dom state here', () => {

0 commit comments

Comments
 (0)