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

108
Views
React / Jest / react testing library test case for function but not getting covered

I am currently trying to test the handleClick() function in React / Jest / React-testing-library:

    // state
    const [toggle, setToggle] = React.useState<boolean>(true);

    const Toggle: React.FunctionComponent = () => {
    const handleClick = () => setToggle(toggle => !toggle);
    return (
       <button onClick={handleClick}>{toggle ? 'Open' : 'Close'}</button>
    )
}

test case:

it('Should cover the setToggle function', () => {
        const toggleFunction = jest.fn((toggle => !toggle));
        let tree = create(<button onClick={toggleFunction}>Item</button>);
        const button = tree.root.findByType('button')
        button.props.onClick()
        expect(toggleFunction).toBeCalledTimes(1);
    });

This test passes, however the setToggle(toggle => !toggle highlights as not covered in code coverage? What am I doing wrong?

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!