testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.27k stars 467 forks source link

`*byRole("listbox")` does not properly query <select /> elements #1082

Closed nicklemmon closed 2 years ago

nicklemmon commented 2 years ago

Relevant code or config:

<label for="my-select">My Select</label>

<select id="my-select"></select>
// Using `dom-testing-library` via `cypress-testing-library`
cy.findByRole("listbox", { name: "My Select" }); // Doesn't work, element not found
cy.findByRole("combobox", { name: "My Select" }); // Does work! But this isn't the right ARIA role

What you did:

What happened:

Reproduction:

Codesandbox reproduction

Problem description:

Suggested solution:

Thanks for all of your great work on this library - it's truly a game changer in the space 🎉

MatanBobi commented 2 years ago

Hi @nicklemmon, thanks for taking the time to open this one :)

For the issue, looking at the code this looks like an expected behavior.

From the MDN you attached:

"The listbox role is used for lists from which a user may select one or more items which are static and, unlike HTML <select>elements, may contain images."

In order to enable multi selection on select, you'll have to add the multiple attribute to it and that's what's being verified in the code. Looking at the codesandbox attached, if you add multiple to the select, the query will work. Thanks again for your time and effort!

nicklemmon commented 2 years ago

Thanks for the quick response, @MatanBobi! That's not actually how I read the spec - to me, it seems that role="listbox" should be the equivalent of a select or select with the multiple attribute - not just with the multiple attribute solely. See the official WAI-ARIA listbox example - looks like either multi or single select is supported here.

That being said, combobox definitely refers to something other than a select. The official WAI-ARIA combobox example is pretty different from an interaction standpoint from a native select.

Then again, I see a newer demo that uses role="combobox" and it works just like a native select!

😕 This is all very confusing.

MatanBobi commented 2 years ago

Hmm, I see what you're saying here, though it's important to note that if you'd do something like this:

      <ul id="my-select" role="listbox">
        <li>Option 1</li>
        <li>Option 2</li>
      </ul>

Then screen.findByRole("listbox", { name: "My Select" }); will work (tested in your codesandbox). So just to see I'm getting it right, the only issue you're talking about is that you believe select element should also have listbox role without having the multiple attribute?

@eps1lon wdyt about this one?

nicklemmon commented 2 years ago

you believe select element should also have listbox role without having the multiple attribute?

@MatanBobi yes, you're hearing me right - though the spec is unclear to me. I'm pretty sure that should be the case, though not 100% positive.

MatanBobi commented 2 years ago

@nicklemmon I looked into the spec a little more and found this:

image

Since this is the wanted behavior, can we close this one? :)

nicklemmon commented 2 years ago

@MatanBobi we sure can! Appreciate your help and your work on this library. Happy New Year to you as well.

MatanBobi commented 2 years ago

Thanks Nick! You too :)