testing-library / react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.
https://testing-library.com/react
MIT License
19.04k stars 1.11k forks source link

*ByRole "status" for <output/> element doesn't handle name attribute correctly #1359

Closed Yimiprod closed 2 months ago

Yimiprod commented 2 months ago

Relevant code or config:

const Test = () => {
  return (
    <>
      <output name="not-found">NOT FOUND</output>
      <output aria-label="found">IS FOUND</output>
    </>
  );
};

describe('not founding status role with standard name attribute', () => {
  it('should have found both status', () => {
    const { getByRole } = render(<Test />);

    const statusOne = getByRole('status', { name: 'not-found' }); // this one will fail
    const statusTwo = getByRole('status', { name: 'found' });

    expect(statusOne).toHaveValue('NOT FOUND');
    expect(statusTwo).toHaveValue('IS FOUND');
  });
});

What you did:

Tried to find by role a "status" with <output/> element, with the attribute name on it.

What happened:

It is flagged as an empty name:

image

Reproduction:

Run example code in jest environnement. The one named with aria-label is correctly named, the one with name attribute isn't.

Problem description:

The name attribute is a standard for input and output naming, the fact that we have to use aria-label mean that the name is lost in the process.

Suggested solution:

*ByRole() for <output/> should find by name for aria-label and name both.

MatanBobi commented 2 months ago

Hi @Yimiprod. The name attribute is not an accessible name which is what the name property means, it's only a name for development use cases and is not reflected to our users. More on this is available in our docs.