question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`*ByDisplayValue` does not filter by `name`

See original GitHub issue
  • @testing-library/dom version: ^7
  • Testing Framework and version: react-scripts@^4
  • DOM Environment: Chrome@ 88.0.4324.146

(Sorry, I don’t really know how to find some of these requested versions in the sandbox)

Relevant code or config:

  render(
    <React.Fragment>
      <input aria-label="Test hello" value="world" />
      <input aria-label="Test world" value="world" />
    </React.Fragment>
  );
  expect(
    screen.getByDisplayValue("world", { name: "Test world" })
  ).toBeInTheDocument();

What you did:

Ran the above test.

What happened:

The test fails because it finds two elements with a display value of world – even though it was invoked with a name filter.

Reproduction:

See code sandbox: https://codesandbox.io/s/react-testing-library-demo-forked-2swfk?file=/src/__tests__/hello.js:744-1004

Problem description:

AFAIK, the queries should accept various options that filters elements before deciding to throw.

I use await screen.findByDisplayValue to asynchronously wait for my inputs to update. In addition, I sometimes use additional options to assert on intended labels or possibly to help find elements with certain roles.

Suggested solution:

Queries should take into account all filter options before throwing on multiple element. In the codesandbox, the test should not throw.

Aside thought: is looking for all values before filtering the fastest way to find the value? Given a label, it might make sense to filter for a particular label before filtering for a particular value.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
MatanBobicommented, Feb 24, 2021

@juanca not that I’m aware of… The point is that the name is based on the accessible name spec (https://www.w3.org/TR/accname-1.1/#mapping_additional_nd) and the only queries we have that impact the accessible name are the *ByRole ones, that’s why they are the only ones that have name filter in them 😃

0reactions
juancacommented, Mar 30, 2021

I have a use-case for this.

Our application has two inputs. One of the inputs essentially controls the initial value for the other input.

<label>
  Select a fruit:
  <input defaultValue={inputRef.current.value} />
</label>

<label>
  Select the default fruit:
  <input ref={inputRef} />
</label>

My tests are setup to wait for the value to update on the first input.

<type into second input>
await screen.findByDisplayValue('banana', { name: 'Select a fruit:' })
<assertions on first input>

I think adding a name filter to display value queries will greatly enhance the developer experience.

Or better yet:

I think adding a value filter to queries will greatly enhance the developer experience.


Though, I can see the argument against adding yet more complexity to the project. And if that’s the case, perhaps a generalized query builder would be nice on the side. Just fishing for ideas here:

query(<name>, { list of attributes })
Read more comments on GitHub >

github_iconTop Results From Across the Web

ByDisplayValue - Testing Library
Returns the input , textarea , or select element that has the matching display ... In case of select , this will search...
Read more >
getByDisplayValue function - rtl.dom.queries library - Dart API
Returns a single InputElement, TextAreaElement or SelectElement that has the matching value displayed, defaulting to an exact match. Throws if no element is ......
Read more >
Best way to test input value in dom-testing-library or react ...
You can of course search for any attribute you like. One final alternative for testing the value is to find the input by...
Read more >
Queries | React Native Testing Library - Open Source
Options; ByText; ByPlaceholderText; ByDisplayValue; ByTestId; ByLabelText ... This is useful for asserting an element that is not present.
Read more >
Using getByDisplayValue in react - YouTube
In this video we take a look at the getByDisplayValue function, what it is and where and how to use it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found