Skip to content

Commit 6e0c752

Browse files
alexkrolickKent C. Dodds
authored and
Kent C. Dodds
committed
feat(queries): add queryAll/getAll methods (#28)
* Add queryAll/getAll methods Adds "all" methods for: - byAltText - byTestId * Add tests for empty matches that return [] * Add contrib * Add rest of getAll queries * Document queryAll and getAll * Make getAll throw when nothing matches * Update docs on queryAll/getAll * Refactor get* to use getAll*
1 parent cfafd9a commit 6e0c752

File tree

4 files changed

+217
-71
lines changed

4 files changed

+217
-71
lines changed

.all-contributorsrc

+9
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@
191191
"code",
192192
"test"
193193
]
194+
},
195+
{
196+
"login": "alexkrolick",
197+
"name": "Alex Krolick",
198+
"avatar_url": "https://avatars3.githubusercontent.com/u/1571667?v=4",
199+
"profile": "https://alexkrolick.com",
200+
"contributions": [
201+
"code"
202+
]
194203
}
195204
]
196205
}

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-18-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-19-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -83,6 +83,7 @@ when a real user uses it.
8383
* [Using other assertion libraries](#using-other-assertion-libraries)
8484
* [`TextMatch`](#textmatch)
8585
* [`query` APIs](#query-apis)
86+
* [`queryAll` and `getAll` APIs](#queryall-and-getall-apis)
8687
* [`bindElementToQueries`](#bindelementtoqueries)
8788
* [Debugging](#debugging)
8889
* [`prettyDOM`](#prettydom)
@@ -106,6 +107,8 @@ npm install --save-dev dom-testing-library
106107

107108
## Usage
108109

110+
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.
111+
109112
```javascript
110113
// src/__tests__/example.js
111114
// query utilities:
@@ -489,6 +492,16 @@ expect(submitButton).toBeNull() // it doesn't exist
489492
expect(submitButton).not.toBeInTheDOM()
490493
```
491494

495+
## `queryAll` and `getAll` APIs
496+
497+
Each of the `query` APIs have a corresponsing `queryAll` version that always returns an Array of matching nodes. `getAll` is the same but throws when the array has a length of 0.
498+
499+
```javascript
500+
const submitButtons = queryAllByText(container, 'submit')
501+
expect(submitButtons).toHaveLength(3) // expect 3 elements
502+
expect(submitButtons[0]).toBeInTheDOM()
503+
```
504+
492505
## `bindElementToQueries`
493506

494507
`bindElementToQueries` takes a DOM element and binds it to the raw query functions, allowing them
@@ -711,7 +724,7 @@ Thanks goes to these people ([emoji key][emojis]):
711724
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
712725
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
713726
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Tests") |
714-
| [<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") |
727+
| [<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") |
715728

716729
<!-- ALL-CONTRIBUTORS-LIST:END -->
717730

src/__tests__/element-queries.js

+72
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,78 @@ test('get element by its alt text', () => {
117117
expect(getByAltText(/fin.*nem.*poster$/i).src).toBe('/finding-nemo.png')
118118
})
119119

120+
test('getAll* matchers return an array', () => {
121+
const {
122+
getAllByAltText,
123+
getAllByTestId,
124+
getAllByLabelText,
125+
getAllByPlaceholderText,
126+
getAllByText,
127+
} = render(`
128+
<div>
129+
<img
130+
data-testid="poster"
131+
alt="finding nemo poster"
132+
src="/finding-nemo.png" />
133+
<img
134+
data-testid="poster"
135+
alt="finding dory poster"
136+
src="/finding-dory.png" />
137+
<img
138+
data-testid="poster"
139+
alt="jumanji poster"
140+
src="/jumanji.png" />
141+
<p>Where to next?</p>
142+
<label for="username">User Name</label>
143+
<input id="username" placeholder="Dwayne 'The Rock' Johnson" />
144+
</div>,
145+
`)
146+
expect(getAllByAltText(/finding.*poster$/i)).toHaveLength(2)
147+
expect(getAllByAltText('jumanji')).toHaveLength(1)
148+
expect(getAllByTestId('poster')).toHaveLength(3)
149+
expect(getAllByPlaceholderText(/The Rock/)).toHaveLength(1)
150+
expect(getAllByLabelText('User Name')).toHaveLength(1)
151+
expect(getAllByText('where')).toHaveLength(1)
152+
})
153+
154+
test('getAll* matchers throw for 0 matches', () => {
155+
const {
156+
getAllByAltText,
157+
getAllByTestId,
158+
getAllByLabelText,
159+
getAllByPlaceholderText,
160+
getAllByText,
161+
} = render(`
162+
<div>
163+
<label>No Matches Please</label>
164+
</div>,
165+
`)
166+
expect(() => getAllByTestId('nope')).toThrow()
167+
expect(() => getAllByAltText('nope')).toThrow()
168+
expect(() => getAllByLabelText('nope')).toThrow()
169+
expect(() => getAllByLabelText('no matches please')).toThrow()
170+
expect(() => getAllByPlaceholderText('nope')).toThrow()
171+
expect(() => getAllByText('nope')).toThrow()
172+
})
173+
174+
test('queryAll* matchers return an array for 0 matches', () => {
175+
const {
176+
queryAllByAltText,
177+
queryAllByTestId,
178+
queryAllByLabelText,
179+
queryAllByPlaceholderText,
180+
queryAllByText,
181+
} = render(`
182+
<div>
183+
</div>,
184+
`)
185+
expect(queryAllByTestId('nope')).toHaveLength(0)
186+
expect(queryAllByAltText('nope')).toHaveLength(0)
187+
expect(queryAllByLabelText('nope')).toHaveLength(0)
188+
expect(queryAllByPlaceholderText('nope')).toHaveLength(0)
189+
expect(queryAllByText('nope')).toHaveLength(0)
190+
})
191+
120192
test('using jest helpers to assert element states', () => {
121193
const {queryByTestId} = render(`<span data-testid="count-value">2</span>`)
122194

0 commit comments

Comments
 (0)