Skip to content

Commit 34c974d

Browse files
author
Kent C. Dodds
authored
fix(label): support aria-label (#42)
1 parent 825cd48 commit 34c974d

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ const inputNode = getByLabelText('Username')
197197
// For this case, you can provide a `selector` in the options:
198198
const inputNode = getByLabelText('username', {selector: 'input'})
199199
// and that would work
200+
// Note that <input aria-label="username" /> will also work, but take
201+
// care because this is not a label that users can see on the page. So
202+
// the purpose of your input should be obvious for those users.
200203
```
201204

202205
> Note: This method will throw an error if it cannot find the node. If you don't
@@ -712,12 +715,10 @@ light-weight, simple, and understandable.
712715
Thanks goes to these people ([emoji key][emojis]):
713716
714717
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
715-
716718
<!-- prettier-ignore -->
717719
| [<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") |
718720
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
719721
| [<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/react-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/react-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/react-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/react-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/react-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/react-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<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/react-testing-library/commits?author=jday3 "Code") |
720-
721722
<!-- ALL-CONTRIBUTORS-LIST:END -->
722723
723724
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/element-queries.js

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ test('totally empty label', () => {
7474
expect(() => getByLabelText('')).toThrowErrorMatchingSnapshot()
7575
})
7676

77+
test('getByLabelText with aria-label', () => {
78+
// not recommended normally, but supported for completeness
79+
const {queryByLabelText} = render(<input aria-label="batman" />)
80+
expect(queryByLabelText('bat')).toBeInTheDOM()
81+
})
82+
7783
test('get element by its alt text', () => {
7884
const {getByAltText} = render(
7985
<div>

src/queries.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function queryLabelByText(container, text) {
1515
function queryByLabelText(container, text, {selector = '*'} = {}) {
1616
const label = queryLabelByText(container, text)
1717
if (!label) {
18-
return null
18+
return queryByAttribute('aria-label', container, text)
1919
}
2020
/* istanbul ignore if */
2121
if (label.control) {
@@ -46,22 +46,21 @@ function queryByText(container, text, {selector = '*'} = {}) {
4646
)
4747
}
4848

49-
function queryByPlaceholderText(container, text) {
49+
// this is just a utility and not an exposed query.
50+
// There are no plans to expose this.
51+
function queryByAttribute(attribute, container, text) {
5052
return (
51-
Array.from(container.querySelectorAll('[placeholder]')).find(node =>
52-
matches(node.getAttribute('placeholder'), node, text),
53+
Array.from(container.querySelectorAll(`[${attribute}]`)).find(node =>
54+
matches(node.getAttribute(attribute), node, text),
5355
) || null
5456
)
5557
}
5658

57-
function queryByTestId(container, id) {
58-
return container.querySelector(getDataTestIdSelector(id))
59-
}
60-
61-
function getDataTestIdSelector(id) {
62-
return `[data-testid="${id}"]`
63-
}
59+
const queryByPlaceholderText = queryByAttribute.bind(null, 'placeholder')
60+
const queryByTestId = queryByAttribute.bind(null, 'data-testid')
6461

62+
// this is just a utility and not an exposed query.
63+
// There are no plans to expose this.
6564
function getText(node) {
6665
return Array.from(node.childNodes)
6766
.filter(
@@ -79,9 +78,7 @@ function getText(node) {
7978
function getByTestId(container, id, ...rest) {
8079
const el = queryByTestId(container, id, ...rest)
8180
if (!el) {
82-
throw new Error(
83-
`Unable to find an element by: ${getDataTestIdSelector(id)}`,
84-
)
81+
throw new Error(`Unable to find an element by: [data-testid="${id}"]`)
8582
}
8683
return el
8784
}

0 commit comments

Comments
 (0)