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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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', }); }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.