user-event icon indicating copy to clipboard operation
user-event copied to clipboard

`selectOptions` selects by `innerHTML` not `innerText`

Open hasparus opened this issue 2 years ago • 2 comments

Reproduction example

https://codesandbox.io/s/testing-library-selectoptions-innertext-iw1bbk?file=/src/App.test.js

Prerequisites

  1. Render a role="listbox" select with role="option"` items where each option contains aria-hidden decorative icon.
  2. Call selectOptions just 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.

hasparus avatar May 10 '23 13:05 hasparus

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);
});

jsit avatar Sep 21 '23 14:09 jsit

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).

MattMcCherry avatar Dec 19 '24 14:12 MattMcCherry