Dom-testing-library: Add semantic HTML elements implicit role support to queryByRole/getByRole selectors

Created on 21 May 2019  路  4Comments  路  Source: testing-library/dom-testing-library

Describe the feature you'd like:

queryByRole/getByRole selectors can return only those HTML elements which specified role explicitly (attribute role is defined explicitly). However semantic HTML elements (table column headers, table cells, textbox, etc.) have implicit ARIA roles and selectors should support those either. Example: https://codesandbox.io/s/domtestinglibraryariaroles-q5vkk?fontsize=14&module=%2Fsrc%2Findex.spec.tsx

Suggested implementation:

The following unit test shouldn't fail:

test("should get textbox (with implicit ARIA textbox role)", () => {
  const { queryByRole } = render(
    <div>
      <input type="text" />
    </div>
  );

  // this should also work
  expect(queryByRole("textbox")).toBeInTheDocument();
});

Describe alternatives you've considered:

The only alternative is to provide ARIA roles explicitly, but it increases redundancy and it's not so elegant either.

Teachability, Documentation, Adoption, Migration Strategy:

enhancement help wanted

Most helpful comment

That package sounds perfect 馃憤

All 4 comments

Ah, that's tricky. I'd be open to expanding role to handle these implicit/semantic HTML roles though.

I don't currently have the bandwidth to do it myself, but here's the source file: https://github.com/testing-library/dom-testing-library/blob/master/src/queries/role.js and I'd probably recommend making a test file for this specifically in: https://github.com/testing-library/dom-testing-library/tree/master/src/__tests__

https://www.npmjs.com/package/aria-query npm package could help us to start expanding role and I can see it within node_modules in the dom-testing-library (however it's not listed in package.json as a dependency). What do you think about its usage?

Another approach to implementing it ourselves based on W3C specification.

That package sounds perfect 馃憤

I've started to implement it.

Was this page helpful?
0 / 5 - 0 ratings