Enzyme - Check whether a function of a Functional Component has been called

Asked

Viewed 10 times

0

I have an React project that had unit tests with Jest and Enzyme. Inside the component I want to create a unit test, I need to know if an internal function (myFunction in the example below) of the component was called, but as I am using Functional Component inves of class Component, I am not able to mock the function. What is the recommended way for me to mock? Export the function to import in the test?

const ModalContent = () => {
  const myFunction = () => {
    return 1 + 1;
  };

  useEffect(() => {
    myFunction();
  }, []);

  return <p className='modal-wrapper'>it's working</p>;
};

export default ModalContent; 
  • Guilherme, if your myFunction function does not return anything on the screen or calls any other function that does any action, there is no way to test it. I guess, according to the example you left, it doesn’t even make sense to test that function. If you have a more real example, share it to help you better.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.