Skip to content

Commit 9f7e62d

Browse files
committed
support aria-label
1 parent 2d445be commit 9f7e62d

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/queries.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ function queryByAltText(container, alt) {
1717
)
1818
}
1919

20-
function queryByPlaceholderText(container, text) {
20+
function queryByAttribute(attribute, container, text) {
2121
return query (
22-
container, '[placeholder]', elem => elem.getAttribute('placeholder'), text
22+
container, `[${attribute}]`, elem => elem.getAttribute(attribute), text
2323
)
2424
}
2525

26+
const queryByPlaceholderText = queryByAttribute.bind(null, 'placeholder')
27+
const queryByTestId = queryByAttribute.bind(null, 'data-testid')
28+
2629
function queryLabelByText(container, text) {
2730
return query (
2831
container, 'label', elem => elem.textContent, text
@@ -39,7 +42,7 @@ function queryByLabelText(container, text, { selector = '*' } = {}) {
3942
const label = queryLabelByText(container, text)
4043

4144
if (!label) {
42-
return null
45+
return queryByAttribute('aria-label', container, text)
4346
}
4447

4548
/* istanbul ignore if */
@@ -65,15 +68,6 @@ function queryByLabelText(container, text, { selector = '*' } = {}) {
6568
}
6669
}
6770

68-
function queryByTestId(container, id) {
69-
const found = container.find(getDataTestIdSelector(id))
70-
return found && found.element ? found.element : null
71-
}
72-
73-
function getDataTestIdSelector(id) {
74-
return `[data-testid="${id}"]`
75-
}
76-
7771
function getText(node) {
7872
if (!node) {
7973
return null
@@ -96,7 +90,7 @@ function getByTestId(container, id, ...rest) {
9690
const el = queryByTestId(container, id, ...rest)
9791
if (!el) {
9892
throw new Error(
99-
`Unable to find an element by: ${getDataTestIdSelector(id)}`,
93+
`Unable to find an element by: [data-testid="${id}"]`,
10094
)
10195
}
10296
return el

0 commit comments

Comments
 (0)