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

142
Views
Assertion mismatch due to setState callback

I am making an assertion in the Jest test case where clicking on a checkbox should update a setState callback (onSelect in my case, coming in as a prop to my component). But since the onSelect uses a callback, my test case is failing. How can I test this? I am using Jest and React Testing Library.

    it('should render a selection list', () => {

      const onSelect = jest.fn();

      const { getByTestId } = render(
        <Selection onSelect={ onSelect }>{ listItems }</Selection>
      );
      const itemToBeSelected = getByTestId('list-item-checkbox-3');

      fireEvent.click(itemToBeSelected);

      expect(onSelect).toBeCalledWith([ 3 ]);
    });



 Expected: [3]
    Received
           1: [Function anonymous]
           2: [Function anonymous]
           3: [Function anonymous]

In the implementation file the onSelect looks like this

onSelect((state) => [ ...state, itemId ] ]);
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!