Skip to content

Commit ba44c14

Browse files
Ruben CostaKent C. Dodds
Ruben Costa
authored and
Kent C. Dodds
committed
fix(getByLabelText): Support aria-labelledby attr containing multiple ids (#59)
1 parent e722b3a commit ba44c14

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,15 @@
267267
"contributions": [
268268
"code"
269269
]
270+
},
271+
{
272+
"login": "rubencosta",
273+
"name": "Ruben Costa",
274+
"avatar_url": "https://avatars3.githubusercontent.com/u/577921?v=4",
275+
"profile": "http://nuances.co",
276+
"contributions": [
277+
"code"
278+
]
270279
}
271280
]
272281
}

README.md

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

@@ -873,7 +873,8 @@ Thanks goes to these people ([emoji key][emojis]):
873873
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
874874
| [<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") |
875875
| [<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") |
876-
| [<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") |
876+
| [<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") |
877+
877878
<!-- ALL-CONTRIBUTORS-LIST:END -->
878879

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

src/__tests__/element-queries.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import 'jest-dom/extend-expect'
22
import {render} from './helpers/test-utils'
33

44
beforeEach(() => {
5-
window.Cypress = null;
6-
});
5+
window.Cypress = null
6+
})
77

88
test('query can return null', () => {
99
const {
@@ -82,12 +82,20 @@ test('get can get form controls by label text', () => {
8282
<label for="fourth.id">4th</label>
8383
<input id="fourth.id" />
8484
</div>
85+
<div>
86+
<div>
87+
<label id="fifth-label-one">5th one</label>
88+
<label id="fifth-label-two">5th two</label>
89+
<input aria-labelledby="fifth-label-one fifth-label-two" id="fifth-id" />
90+
</div>
8591
</div>
8692
`)
8793
expect(getByLabelText('1st').id).toBe('first-id')
8894
expect(getByLabelText('2nd').id).toBe('second-id')
8995
expect(getByLabelText('3rd').id).toBe('third-id')
9096
expect(getByLabelText('4th').id).toBe('fourth.id')
97+
expect(getByLabelText('5th one').id).toBe('fifth-id')
98+
expect(getByLabelText('5th two').id).toBe('fifth-id')
9199
})
92100

93101
test('get can get form controls by placeholder', () => {
@@ -174,15 +182,15 @@ test('getAll* matchers return an array', () => {
174182
<div>
175183
<img
176184
data-testid="poster"
177-
alt="finding nemo poster"
185+
alt="finding nemo poster"
178186
src="/finding-nemo.png" />
179187
<img
180188
data-testid="poster"
181-
alt="finding dory poster"
189+
alt="finding dory poster"
182190
src="/finding-dory.png" />
183191
<img
184192
data-testid="poster"
185-
alt="jumanji poster"
193+
alt="jumanji poster"
186194
src="/jumanji.png" />
187195
<p>Where to next?</p>
188196
<label for="username">User Name</label>

src/queries.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {getNodeText} from './get-node-text'
33
import {prettyDOM} from './pretty-dom'
44

55
function debugDOM(htmlElement) {
6-
const limit = process.env.DEBUG_PRINT_LIMIT || 7000
7-
const inNode = (typeof module !== 'undefined' && module.exports)
8-
const inCypress = (typeof window !== 'undefined' && window.Cypress)
6+
const limit = process.env.DEBUG_PRINT_LIMIT || 7000
7+
const inNode = typeof module !== 'undefined' && module.exports
8+
const inCypress = typeof window !== 'undefined' && window.Cypress
99
/* istanbul ignore else */
1010
if (inCypress) {
1111
return ''
@@ -66,7 +66,7 @@ function queryAllByLabelText(
6666
if (label.getAttribute('id')) {
6767
// <label id="someId">text</label><input aria-labelledby="someId" />
6868
return container.querySelector(
69-
`[aria-labelledby="${label.getAttribute('id')}"]`,
69+
`[aria-labelledby~="${label.getAttribute('id')}"]`,
7070
)
7171
}
7272
if (label.childNodes.length) {
@@ -155,7 +155,10 @@ function queryByAltText(...args) {
155155
function getAllByTestId(container, id, ...rest) {
156156
const els = queryAllByTestId(container, id, ...rest)
157157
if (!els.length) {
158-
throw getElementError(`Unable to find an element by: [data-testid="${id}"]`, container)
158+
throw getElementError(
159+
`Unable to find an element by: [data-testid="${id}"]`,
160+
container,
161+
)
159162
}
160163
return els
161164
}
@@ -167,7 +170,10 @@ function getByTestId(...args) {
167170
function getAllByTitle(container, title, ...rest) {
168171
const els = queryAllByTitle(container, title, ...rest)
169172
if (!els.length) {
170-
throw getElementError(`Unable to find an element with the title: ${title}.`, container)
173+
throw getElementError(
174+
`Unable to find an element with the title: ${title}.`,
175+
container,
176+
)
171177
}
172178
return els
173179
}
@@ -179,7 +185,10 @@ function getByTitle(...args) {
179185
function getAllByValue(container, value, ...rest) {
180186
const els = queryAllByValue(container, value, ...rest)
181187
if (!els.length) {
182-
throw getElementError(`Unable to find an element with the value: ${value}.`, container)
188+
throw getElementError(
189+
`Unable to find an element with the value: ${value}.`,
190+
container,
191+
)
183192
}
184193
return els
185194
}
@@ -191,7 +200,10 @@ function getByValue(...args) {
191200
function getAllByPlaceholderText(container, text, ...rest) {
192201
const els = queryAllByPlaceholderText(container, text, ...rest)
193202
if (!els.length) {
194-
throw getElementError(`Unable to find an element with the placeholder text of: ${text}`, container)
203+
throw getElementError(
204+
`Unable to find an element with the placeholder text of: ${text}`,
205+
container,
206+
)
195207
}
196208
return els
197209
}
@@ -205,9 +217,15 @@ function getAllByLabelText(container, text, ...rest) {
205217
if (!els.length) {
206218
const labels = queryAllLabelsByText(container, text, ...rest)
207219
if (labels.length) {
208-
throw getElementError(`Found a label with the text of: ${text}, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.`, container)
220+
throw getElementError(
221+
`Found a label with the text of: ${text}, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.`,
222+
container,
223+
)
209224
} else {
210-
throw getElementError(`Unable to find a label with the text of: ${text}`, container)
225+
throw getElementError(
226+
`Unable to find a label with the text of: ${text}`,
227+
container,
228+
)
211229
}
212230
}
213231
return els
@@ -220,7 +238,10 @@ function getByLabelText(...args) {
220238
function getAllByText(container, text, ...rest) {
221239
const els = queryAllByText(container, text, ...rest)
222240
if (!els.length) {
223-
throw getElementError(`Unable to find an element with the text: ${text}. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.`, container)
241+
throw getElementError(
242+
`Unable to find an element with the text: ${text}. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.`,
243+
container,
244+
)
224245
}
225246
return els
226247
}
@@ -232,7 +253,10 @@ function getByText(...args) {
232253
function getAllByAltText(container, alt, ...rest) {
233254
const els = queryAllByAltText(container, alt, ...rest)
234255
if (!els.length) {
235-
throw getElementError(`Unable to find an element with the alt text: ${alt}`, container)
256+
throw getElementError(
257+
`Unable to find an element with the alt text: ${alt}`,
258+
container,
259+
)
236260
}
237261
return els
238262
}

0 commit comments

Comments
 (0)