mardi 23 mai 2017

Can't test effects of resolved promise

I have the following code in user:

 import { redirectTo } from 'urlUtils';
 export function create(user) {
    api.postUser(user).then((response) => {
      redirectTo(response.userUrl);
    })
 }

And I have the following test:

import * as api from 'api'
import * as user from 'user'

sinon.stub(api, 'poserUser').returns(
  Promise.resolve({ userUrl: 'a-url' })
);
sinon.spy(urlUtils, 'redirectTo');
const userData = {id: 2};

user.create(userData);
expect(api.postUser.calledWith(userData)).toBe(true);  // This passes
expect(urlUtils.redirectTo.calledOnce).toBe(true); // This fails

I have been able to test it on the browser, and the redirect is happening. What am I missing here? Is the promise not being resolved before I write the expectation?

Aucun commentaire:

Enregistrer un commentaire