Skip to content

Add semantic HTML elements implicit role support to queryByRole/getByRole selectors #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
szabototo89 opened this issue May 21, 2019 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@szabototo89
Copy link
Contributor

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:

@kentcdodds
Copy link
Member

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__

@kentcdodds kentcdodds added enhancement New feature or request help wanted Extra attention is needed labels May 21, 2019
@szabototo89
Copy link
Contributor Author

szabototo89 commented May 22, 2019

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.

@kentcdodds
Copy link
Member

That package sounds perfect 👍

@szabototo89
Copy link
Contributor Author

I've started to implement it.

szabototo89 added a commit to szabototo89/dom-testing-library that referenced this issue Jun 8, 2019
…/getByRole selectors (testing-library#262)

* Add aria-query library as a dependency

* Extend queryByRole to use aria-query ARIA roles to identify semantic HTML elements
szabototo89 added a commit to szabototo89/dom-testing-library that referenced this issue Jun 8, 2019
…/getByRole selectors (testing-library#262)

* Add aria-query library as a dependency

* Extend queryByRole to use aria-query ARIA roles to identify semantic HTML elements
kentcdodds pushed a commit that referenced this issue Jun 8, 2019
…/getByRole selectors (#262) (#280)

* Add aria-query library as a dependency

* Extend queryByRole to use aria-query ARIA roles to identify semantic HTML elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants