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

280
Views
Jest test react hook method to get better code coverage

I have a component, that sets state based on the onClick method as follows:

const [active, setActive] = React.useState(0);

<button type="button" onClick={() => {setActive(() => index); inner.fc(index)}} key={inner.id}>{inner.title}</button>

The test itself is:

it('Should test setActive index of button', (): void => {
    const setIndex = jest.fn(() => 1);
    let tree = create(<button type="button" onClick={setIndex} key='1'>1 button</button>);
    const button = tree.root.findByType('button')
    button.props.onClick()
    expect(setIndex()).toBe(1);
});

This passes but when I look at the coverage report, it flags the setActive method as not covered? How do I test that React.useState method correctly?

about 4 years ago · Juan Pablo Isaza
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!