Skip to content

Commit 7060a98

Browse files
committed
feat(add getAllByTestId & queryAllByTestId functions)
**What**: Add new query capabilities for improved tests - getAllByTestId - queryAllByTestId **Why**: These will include querying for multiple elements rendered in the dom It will improve usability of the module. add tests for queryAllByTestId functions add tests for getAllByTestId function **How**: - Added the respective functions in the `queries.js` file - Added tests for functions in the `__tests__/element-queries.js` file **Checklist**: * [ ] Documentation * [x] Tests * [x] Ready to be merged * [x] Added myself to contributors table
1 parent 12c7ed1 commit 7060a98

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@
7676
"contributions": [
7777
"platform"
7878
]
79+
},
80+
{
81+
"login": "bahdcoder",
82+
"name": "Kati Frantz",
83+
"avatar_url": "https://avatars2.githubusercontent.com/u/19477966?v=4",
84+
"profile": "https://github.com/bahdcoder",
85+
"contributions": [
86+
"code",
87+
"test"
88+
]
7989
}
8090
]
8191
}

README.md

Lines changed: 2 additions & 1 deletion
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-7-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-8-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -564,6 +564,7 @@ Thanks goes to these people ([emoji key][emojis]):
564564
<!-- prettier-ignore -->
565565
| [<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/react-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-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/react-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/react-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/react-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/react-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/react-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/react-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") |
566566
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
567+
| [<img src="https://avatars2.githubusercontent.com/u/19477966?v=4" width="100px;"/><br /><sub><b>Kati Frantz</b></sub>](https://github.com/bahdcoder)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=bahdcoder "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=bahdcoder "Tests") |
567568

568569
<!-- ALL-CONTRIBUTORS-LIST:END -->
569570

src/__tests__/__snapshots__/element-queries.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ exports[`get throws a useful error message 3`] = `"Unable to find an element wit
88

99
exports[`get throws a useful error message 4`] = `"Unable to find an element by: [data-testid=\\"LucyRicardo\\"]"`;
1010

11+
exports[`getAllByTestId throws a useful error message if no matching elements are found 1`] = `"Unable to find any elements by: [data-testid=\\"LucyRicardo\\"]"`;
12+
1113
exports[`label with no form control 1`] = `"Found a label with the text of: alone, however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly."`;
1214

1315
exports[`totally empty label 1`] = `"Found a label with the text of: , however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly."`;

src/__tests__/element-queries.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@ test('query can return null', () => {
1414
expect(queryByText('LucyRicardo')).toBeNull()
1515
})
1616

17+
test('queryAllByTestId returns an empty node list', () => {
18+
const {queryAllByTestId} = render(<div />)
19+
const results = queryAllByTestId('LucyRicardo')
20+
expect(results).toBeInstanceOf(NodeList)
21+
expect(results.length).toBe(0)
22+
})
23+
24+
test('getAllByTestId throws a useful error message if no matching elements are found', () => {
25+
const {getAllByTestId} = render(<div />)
26+
expect(() => getAllByTestId('LucyRicardo')).toThrowErrorMatchingSnapshot()
27+
})
28+
29+
test('getAllByTestId returns a node list of elements if they are found', () => {
30+
const {getAllByTestId} = render(
31+
<div>
32+
<ul>
33+
<li data-testid="LucyRicardo" />
34+
<li data-testid="LucyRicardo" />
35+
</ul>
36+
</div>,
37+
)
38+
39+
const results = getAllByTestId('LucyRicardo')
40+
expect(results).toBeInstanceOf(NodeList)
41+
expect(results.length).toBe(2)
42+
})
43+
1744
test('get throws a useful error message', () => {
1845
const {getByLabelText, getByPlaceholderText, getByText, getByTestId} = render(
1946
<div />,

src/queries.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ function queryByTestId(container, id) {
5656
return container.querySelector(getDataTestIdSelector(id))
5757
}
5858

59+
function queryAllByTestId(container, id) {
60+
return container.querySelectorAll(getDataTestIdSelector(id))
61+
}
62+
5963
function getDataTestIdSelector(id) {
6064
return `[data-testid="${id}"]`
6165
}
@@ -94,6 +98,18 @@ function getByTestId(container, id, ...rest) {
9498
return el
9599
}
96100

101+
function getAllByTestId(container, id, ...rest) {
102+
const elements = queryAllByTestId(container, id, ...rest)
103+
104+
if (elements.length === 0) {
105+
throw new Error(
106+
`Unable to find any elements by: ${getDataTestIdSelector(id)}`,
107+
)
108+
}
109+
110+
return elements
111+
}
112+
97113
function getByPlaceholderText(container, text, ...rest) {
98114
const el = queryByPlaceholderText(container, text, ...rest)
99115
if (!el) {
@@ -138,6 +154,8 @@ export {
138154
getByLabelText,
139155
queryByTestId,
140156
getByTestId,
157+
queryAllByTestId,
158+
getAllByTestId,
141159
}
142160

143161
/* eslint complexity:["error", 14] */

0 commit comments

Comments
 (0)