testing-library / testing-library/user-event

Cannot test datalist elements with user-event

Open
#1,088 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

accuracy
Dominant language
TypeScript
Stars
2.3k
Forks
258
PR merge metrics
No merged PRs in 30d

Description

Reproduction example

https://github.com/SirRegion/user-event-datalist-example

Prerequisites

To reproduce you need an input with a datalist

<label htmlFor="browser">Choose your browser from the list:</label>
        <input list="browsers" name="browser" id="browser"/>

        <datalist id="browsers">
          <option value="Edge"/>
          <option value="Firefox"/>
          <option value="Chrome"/>
          <option value="Opera"/>
          <option value="Safari"/>
        </datalist>

and this test:

test('shows datalist', async () => {
  userEvent.setup();
  render(<App />);
  const datalist = screen.getByLabelText("Choose your browser from the list:");
  expect(datalist).toBeInTheDocument();
  await userEvent.selectOptions(datalist,"Edge")
  expect(datalist).toHaveValue("Edge")
});
Expected behavior

This should select the element "Edge" and set the value of the input to "Edge"

Actual behavior

The test fails with

TestingLibraryElementError: Value "Edge" not found in options

    Ignored nodes: comments, script, style
    <input
      id="browser"
      list="browsers"
      name="browser"
    />

I think this is caused by the testing library excluding the datalist element and only searching in the input element, which does not have any children.

User-event version

14.4.3

Environment

Testing Library framework:

JS framework:

Test environment:

DOM implementation:

Additional context

I'm willing to submit a PR, if someone can point me into the right direction on how to fix this issue.

Thanks in advance for your help :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction example at https://github.com/SirRegion/user-event-datalist-example and run the shown test against the reported versions. Then trace selectOptions handling for an input associated with a datalist; done means the option "Edge" is selected and the input value assertion passes with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.