-
Notifications
You must be signed in to change notification settings - Fork 472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more robust selector handling for getByLabelText #373
Changes from 2 commits
019a1c3
2a832cd
7b41148
581c451
696f309
d7a9c2a
ad90d4e
a7ad2fb
dc48a12
8eb8c9f
d5ca2f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,9 +40,10 @@ function queryAllByLabelText( | |
normalizer: matchNormalizer, | ||
}) | ||
const labelledElements = labels | ||
.map(label => { | ||
.reduce((matchedElements, label) => { | ||
const elementsForLabel = [] | ||
if (label.control) { | ||
return label.control | ||
elementsForLabel.push(label.control) | ||
} | ||
/* istanbul ignore if */ | ||
if (label.getAttribute('for')) { | ||
|
@@ -51,21 +52,25 @@ function queryAllByLabelText( | |
// <label for="someId">text</label><input id="someId" /> | ||
|
||
// .control support has landed in jsdom (https://github.com/jsdom/jsdom/issues/2175) | ||
return container.querySelector(`[id="${label.getAttribute('for')}"]`) | ||
elementsForLabel.push( | ||
container.querySelector(`[id="${label.getAttribute('for')}"]`), | ||
) | ||
} | ||
if (label.getAttribute('id')) { | ||
// <label id="someId">text</label><input aria-labelledby="someId" /> | ||
return container.querySelector( | ||
`[aria-labelledby~="${label.getAttribute('id')}"]`, | ||
) | ||
container | ||
.querySelectorAll(`[aria-labelledby~="${label.getAttribute('id')}"]`) | ||
.forEach(element => elementsForLabel.push(element)) | ||
} | ||
if (label.childNodes.length) { | ||
// <label>text: <input /></label> | ||
return label.querySelector(selector) | ||
label | ||
.querySelectorAll(selector) | ||
.forEach(element => elementsForLabel.push(element)) | ||
} | ||
return null | ||
}) | ||
.filter(label => label !== null) | ||
return matchedElements.concat(elementsForLabel) | ||
}, []) | ||
.filter(element => element !== null) | ||
.concat(queryAllByAttribute('aria-label', container, text, {exact})) | ||
|
||
const possibleAriaLabelElements = queryAllByText(container, text, { | ||
|
@@ -89,7 +94,13 @@ function queryAllByLabelText( | |
[], | ||
) | ||
|
||
return Array.from(new Set([...labelledElements, ...ariaLabelledElements])) | ||
const allMatches = Array.from( | ||
new Set([...labelledElements, ...ariaLabelledElements]), | ||
) | ||
const elementsMatchingSelector = new Set(container.querySelectorAll(selector)) | ||
return allMatches.filter(matchingElement => | ||
elementsMatchingSelector.has(matchingElement), | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'd be better off with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had a feeling this wasn't the best way. Probably could have done a little more digging haha. |
||
} | ||
|
||
// the getAll* query would normally look like this: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add another test for the situation I found myself in where we had two elements that had the same label text?
Thanks!