Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!