testing-library / dom-testing-library

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

ByRole queries don't support <section> as region with only a `title` #1146

Open Sawtaytoes opened 2 years ago

Sawtaytoes commented 2 years ago

Relevant code or config:

<section title="My Section">
  some text
</section>

What you did:

screen.getByRole('region')

What happened:

TestingLibraryElementError: Unable to find an accessible element with the role "region"

There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the `hidden` option to `true`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole

Ignored nodes: comments, <script />, <style />

<body
  data-gr-ext-installed=""
  data-new-gr-c-s-check-loaded="14.1067.0"
>
  <div
    id="root"
  >
    <section
      title="My Section"
    >
      some text
    </section>
  </div>

Reproduction:

https://codesandbox.io/s/bold-bush-64s8n0?file=/src/App.js

Problem description:

image

<section> elements should have an internal browser role of section until you give them an accessible name; in which case, they implicitly have the role region: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section

Implicit ARIA role region if the element has an accessible name, otherwise no corresponding role

Potential accessibility names are either an alt label, ARIA label, title, or the text content for certain elements. For whatever reason, Testing Library is not treating this <section> as a 'region' when it has an accessible name. Works fine for aria-label and aria-labelledby but not for title.

Suggested solution:

Either fix the aria-query library or @testing-library/dom so it includes proper support for accessibility names using the HTML title attribute. Not sure if this is limited to <section> or if it happens for other elements too.