selectOptions by option content
See original GitHub issuehttps://github.com/testing-library/user-event#selectoptionselement-values
Users do not interact with <select/>
elements by the option value
because it is not visible. Users typically select by the text content of each option that they can see.
This is possibly a separate issue, but the current example in the readme is unclear about whether the option content or the value is used to select because the values and content are the same.
<option data-testid="val1" value="1">
1
</option>
userEvent.selectOptions(getByTestId("select-multiple"), ["1", "3"]);
While the example is unclear, there is some text:
The values parameter can be either an array of values or a singular scalar value.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:5 (1 by maintainers)
Top Results From Across the Web
HTMLSelectElement.selectedOptions - Web APIs | MDN
The list of selected options is an HTMLCollection object with one entry per currently selected option. An option is considered selected if it ......
Read more >How do you select a particular option in a SELECT element in ...
I wanted a solution where selecting from an arbitrarily grouped select options automatically selected another select input on that same page. So for...
Read more >How do I get the text value of a selected option?
Select elements typically have two values that you want to access. First there's the value to be sent to the server, which is...
Read more >HTML DOM Select options Collection - W3Schools
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you ......
Read more >ion-select: Select One or Multiple Value Boxes or Placeholders
If value is set on the <ion-select> , the selected option will be chosen based on ... but the newly retrieved select options...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think this PR handles this best, so I’ll go ahead and close this.
Hello, I come from Ruby and Rails land, where the syntax for selecting an option using Capybara looks like this:
The option is selected based on the visible text of the
<option>
not thevalue
.Docs for this method are here: https://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FNode%2FActions:select
I was, therefore, expecting
userEvent.selectOptions
to work in a similar fashion and have just spent a good long while trying to work out why my test wasn’t working 😃I would agree with @jsphstls that the default behaviour should be selecting by the
<option>
text, and “special” behaviour should be supported by passing in option nodes directly.Additionally, if you do something like:
You don’t get a warning that the option value doesn’t exist in the
<select>
, which meant it wasn’t obvious to work out that “nothing” was being selected when I calledselectOptions
.