Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda