`selectOptions` selects by `innerHTML` not `innerText`
Reproduction example
https://codesandbox.io/s/testing-library-selectoptions-innertext-iw1bbk?file=/src/App.test.js
Prerequisites
- Render a
role="listbox" select withrole="option"` items where each option contains aria-hidden decorative icon. - Call
selectOptionsjust with the option text, not entire inner HTML.
Expected behavior
The option with innerText equal to the argument passed to selectOptions gets selected.
Actual behavior
The test fails with "Value ${optionText} not found in options".
User-event version
^14.4.3
Environment
Testing Library framework: @testing-library/[email protected]
JS framework: [email protected]
Test environment: [email protected] and @storybook/[email protected]
DOM implementation: [email protected]
Additional context
The relevant code in user-event is here: https://github.com/testing-library/user-event/blob/7a305dee9ab833d6f338d567fc2e862b4838b76a/src/utility/selectOptions.ts#L45-L49
I propose adding || o.innerText === val.
I've gotten around this by doing:
const OptionOne = screen.getByRole('option', { name: 'Option 1' });
const ListBox = screen.getByRole('listbox');
await act(async () => {
await userEvent.selectOptions(ListBox, OptionOne);
});
https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element
I agree with this suggestion, following W3C guidance this should be innerText or potentially textContext (if we want to include hidden text).