Test functions with Jest and Enzyme?

Asked

Viewed 78 times

0

How can I test this function with Jest and Enzyme?

addProducts = id => {
   toast.success("Product added", {
      position: toast.POSITION.TOP_RIGHT
   });
   history.push(`/product/${id}`);
};

I was using this bit of code, but it wasn’t enough ...

it("Must return added addProduct with success message", () => {
   const componente = shallow(<AddProduct />);

   const spy = jest.spyOn(wrapper.instance(), "addProducts");

   expect(spy).toBeTruthy();
});
  • 1

    Can you show more code? what is wrapper for example?

  • 1

    Victor, it’s not an answer to your question, but it’s a suggestion: Use the React-testing-library. You may fall into an error testing implementation and not the code itself because of Enzyme. Anyway, to test there you need to check if Toast appeared on the screen. It seems to me that in your test code you are testing if addProducts appears and it is not actually rendered but Toast.

No answers

Browser other questions tagged

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