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

186
Vistas
Migrating from enzyme to react-testing-library: how to select the nth element from list

I have this code:

wrapper.find('.table-year-head button').at(1).simulate('click');
wrapper
  .find('.table-months')
  .at(1)
  .find('input[type="checkbox"]')
  .forEach(node => {
    expect(node.props().checked).toEqual(true);
});

I know that I can use data-testid for the selectors but how can I select the nth?

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

0

You can use a getAllBy query:

const table = wrapper.getAllByRole('table')[1]
fireEvent.click(within(table).getByRole('button'));

The problem is that you wouldn't be able to use CSS selectors the way you are doing with Enzyme. React testing library is all about semantic. I would recommend you to add labels to your tables using aria-label attribute:

<table className="years" aria-label="Year table">
...

This way, you would be able to do:

const yearTable = wrapper.getByRole('table', {name: 'Year table'});
fireEvent.click(within(yearTable).getByRole('button'));

const monthTable = screen.getByRole('table', {name: 'February table'});
const checkbox = within(monthTable).getByRole('checkbox')
fireEvent.click(checkbox);
expect(checkbox).toBeChecked();
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