dom-testing-library icon indicating copy to clipboard operation
dom-testing-library copied to clipboard

can't find element by role `code`

Open chiptus opened this issue 3 years ago • 5 comments

  • @testing-library/dom version: 12.1.2
  • Testing Framework and version: jest 27.4.3
  • DOM Environment: jsdom 16.6.0

Relevant code or config:

component

export default ({ name }) => <code>{name}</code>;

test

import "@testing-library/jest-dom/extend-expect";
import React from "react";
import { render } from "@testing-library/react";
import Code from "../hello";

test("To render a code element", () => {
  const { getByRole } = render(<Code name="Jill" />);
  expect(getByRole("code")).toBeInTheDocument();
});

What you did:

I searched the code element by role code, I expected to find it by https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/structural_roles

What happened:

The element is not found

Reproduction:

https://codesandbox.io/s/fail-to-find-role-code-qpzrs?file=/src/tests/hello.js

Problem description:

Per testing-library docs, it seems like it should work, as when I check the role of the element in chrome dev tools, I see 'code' as expected.

Suggested solution:

not sure, I've seen that aria-query doesn't support this role.

chiptus avatar Jan 30 '22 15:01 chiptus

Hi @chiptus! Thanks for taking the time to open this one :) Since the issue you're experiencing here is related to implicit roles, aria-query is indeed the package we're using for that. Though, it's important to note that the Chrome DevTools isn't our source of truth. We follow the ARIA standards and the way they define implicit roles. As you can see here, the standard doesn't dictate an implicit role for code element and that's also the reason why aria-query aren't supporting that either. image

For this specific use-case, I suggest either setting an explicit role on your own or query the element with a different approach :)

I'm closing this one as this is the wanted behavior in this scenario. Thanks again!

MatanBobi avatar Jan 30 '22 19:01 MatanBobi

@MatanBobi Hello, I'm sorry but I think it's better to reopen this issue, because there is corresponding role "code", now:

Снимок экрана 2022-12-26 в 16 07 54

I have run into this problem today, so it will be cool if you're add this role. Thank you.

EmeraldWeb avatar Dec 26 '22 12:12 EmeraldWeb

Thanks @EmeraldWeb! I see that the spec was updated. I've opened an issue for aria-query regarding this :)

MatanBobi avatar Dec 28 '22 19:12 MatanBobi

Bumping because this issue still isn't resolved

CalebJamesStevens avatar May 03 '23 20:05 CalebJamesStevens

@CalebJamesStevens, there's no need to bump this issue, we're waiting for aria-query to release a new version, once they will release it, we will upgrade our dependency. @eps1lon do you know if there's a roadmap for aria-query release?

MatanBobi avatar May 08 '23 10:05 MatanBobi

v10.0.0 is now live with this update.

jlp-craigmorten avatar Apr 10 '24 13:04 jlp-craigmorten

Thanks @jlp-craigmorten!

@all-contributors please add @jlp-craigmorten for question

MatanBobi avatar Apr 10 '24 13:04 MatanBobi

@MatanBobi

I've put up a pull request to add @jlp-craigmorten! :tada:

allcontributors[bot] avatar Apr 10 '24 13:04 allcontributors[bot]