mercredi 27 février 2019

How to test the then part after promise in jest

I'm working on a React project and I'm using jest to write tests for my code.

This is the code I want to test.

const handleSubmit = (handleSuccess, handleErrors) => {
  signupAPI(user)
    .then(handleSuccess)
    .catch(handleErrors);
};

Here's the test code:

test('should call handleSuccess', () => {
  signupAPI.mockImplementation((user) => Promise.resolve(user));
  handleSuccess = jest.fn();
  handleErrors = jest.fn();

  handleSubmit(handleSuccess, handleErrors); 

  expect(signupAPI).toHaveBeenCalled(); // test passes

  expect(handleSuccess).toHaveBeenCalled(); // test fails
});

When I run the test, it never moves to the 'then' part after the promise. How do I test that the function inside the then part is actually called?

Aucun commentaire:

Enregistrer un commentaire