Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
byRole is not returning the DOM element

I am migrating some of my unit test cases which were previously written using Jest and Enzyme to React Testing Library. I am using Material UI's Select component and I know that, in order to open the dropdown, we have to trigger the mouseDown event on the corresponding div. Here is how I did it in Enzyme (working):

wrapper.find('[role="button"]').simulate('mousedown', { button: 0 });

I am trying to achieve the same using React Testing Library in the following manner, which is not working:

const { container, getAllByRole, getByRole } = renderComponent(mockProps);
fireEvent.mouseDown(getByRole('button')); // trigger the mouseDown on div having role=button

After this I am trying to access the listbox element which is ul element:

getByRole('listbox')

which throws an error and says:

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

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

I have verified and the ul element is visible (neither it nor its parent have display none or visibility hidden)

enter image description here

UPDATE - 1

I have tried all the following approach to wait for element to appear in DOM:

  • waitFor
  • findByRole instead of getByRole

in both the case it throws error that Unable to find role="listbox"

What is wrong?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

If the element is not accessible at the first render this error could appear, you can try passing the option hidden to the options key in the second argument of the getByRole, if this not works you can try using findByRole, that method wait for the element, and if you want to be sure of the visibility of the element you can try adding a waitFor inside of the test.

getByRole('listbox', { options: { hidden: true } });
about 4 years ago · Juan Pablo Isaza Denunciar

0

Make sure animations are not compromising your results. If the animations change opacity or display and they did not finish before assertion from jest... it is likely to throw an error.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I found the root cause of this issue. I am using the Select component from MUI in disablePortal mode, which make the menu list to be rendered inside the parent component instead of document body. But while MUI does that, it doesn't remove the aria-hidden attribute from the parent component's div, and because of that testing library is not able to locate the listbox (ul) element inside.

There is an issue reported here: https://github.com/mui/material-ui/issues/19450

So as a work around, I passed the data-testid to the menu component (MenuListProps) and using it to get the listbox.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda