Test React component method is calling function pass as a prop Test React component method is calling function pass as a prop reactjs reactjs

Test React component method is calling function pass as a prop


Assuming your customMethod is a component method, I would test it like this:

(1) Fake your trackEvent prop as a jest.fn() when you create the wrapper.

(2) Call your customMethod using wrapper.instance().customMethod();

(3) Ensure props.trackEvent to haveBeenCalledWith the argument you mentioned.

As an example:

test('customMethod should call trackEvent with the correct argument', () => {  const baseProps = {    // whatever fake props you want passed to the component    // ...    trackEvent: jest.fn(),  };  const wrapper = shallow(<AdPage {...baseProps} />);  wrapper.instance().customMethod();  expect(baseProps.trackEvent).toHaveBeenCalledTimes(1);  expect(baseProps.trackEvent).toHaveBeenCalledWith({    category: 'eventCategory',    action: 'eventAction',    label: 'eventAction',  });});