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

128
Views
How to call test this callback that sets state in React Enzyme Jest

Let's say I need to test this Component. It gets state and setter for it trough props. How should test look like, I wasted 4 hours trying to figure it out but nothing really works.

Component.jsx

export function Component({ isShowing, setShow }) {
  return (
    <>
      <button
        onClick={() => {
          setShow(!isShowing);
        }}
      >
        Toggle
      </button>
      {isShowing && <h1>Showing</h1>}
    </>
  );
}

This is something I was thinking about but it doesn't pass.

Component.test.js

import Enzyme, { mount } from 'enzyme';
import EnzymeAdapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new EnzymeAdapter() });

const mountSetup = (props = {}) => {
  return mount(<Component {...props} />);
};

const defaultProps = {
  isShowing: false,
  setShow: () => {},
};

describe('Component works as expected', () => {
  test('Component toggles ', () => {
    const wrapper = mountSetup(defaultProps);
    // expect(wrapper.prop('setShow')).toBeTruthy();
    wrapper.find('button').simulate('click');
    expect(wrapper.find('h1').text()).toBe("Showing")
    wrapper.unmount();
  });
});
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!