Skip to content

Commit 6ed5eef

Browse files
matchaiKent C. Dodds
authored and
Kent C. Dodds
committed
fix(queryAllBySelectText): search using reactive properties (testing-library#124)
<!-- Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated! Please make sure that you are familiar with and follow the Code of Conduct for this project (found in the CODE_OF_CONDUCT.md file). Also, please make sure you're familiar with and follow the instructions in the contributing guidelines (found in the CONTRIBUTING.md file). If you're new to contributing to open source projects, you might find this free video course helpful: http://kcd.im/pull-request Please fill out the information below to expedite the review and (hopefully) merge of your pull request! --> <!-- What changes are being made? (What feature/bug is being fixed here?) --> **What**: Replace the existing select query mechanism which uses `HTMLSelectElement .selectedOptions` with a query that searches for the `selected` attribute of an `HTMLOptionElement`. <!-- Why are these changes necessary? --> **Why**: Closes testing-library#115. Since `selectedOptions` is not a reactive property, JSDOM was not updating to match the selected option in a select Element. The `selected` attribute is reactive. <!-- How were these changes implemented? --> **How**: Instead of iterating through `selectedOptions` of an HTMLSelectElement, we iterate through all HTMLSelectOptions and isolate the ones that are selected. <!-- Have you done all of these things? --> I've gone ahead and tested this locally to make sure that it would be reactive! 👍 **Checklist**: <!-- add "N/A" to the end of each line that's irrelevant to your changes --> <!-- to check an item, place an "x" in the box like so: "- [x] Documentation" --> - [ ] Documentation N/A - [ ] Tests N/A - [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? --> - [x] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions --> <!-- feel free to add additional comments -->
1 parent fadab1e commit 6ed5eef

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/queries.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,14 @@ function queryAllBySelectText(
134134
) {
135135
const matcher = exact ? matches : fuzzyMatches
136136
const matchOpts = {collapseWhitespace, trim}
137-
return Array.from(container.querySelectorAll('select')).filter(selectNode =>
138-
Array.from(selectNode.selectedOptions).some(optionNode =>
137+
return Array.from(container.querySelectorAll('select')).filter(selectNode => {
138+
const selectedOptions = Array.from(selectNode.options).filter(
139+
option => option.selected,
140+
)
141+
return selectedOptions.some(optionNode =>
139142
matcher(getNodeText(optionNode), optionNode, text, matchOpts),
140-
),
141-
)
143+
)
144+
})
142145
}
143146

144147
function queryBySelectText(...args) {

0 commit comments

Comments
 (0)