Test React component method is calling function pass as a prop

Posted by Andre Scott | Updated on

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:


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.

Related Questions & Answers