I'm trying to test some very simple functionality with Flux store that on specific event call to service that make http request and return Promise, store looks like:
case UserActions.FETCH_USER_BY_ID:
const userService = new UserService();
userService.fetchUserById(action.id)
then(user => {
this.user = user;
this.emit(USER_FETCH_COMPLETED);
});
For the testing I'm using Jasmine, and my test case looks like:
it('should fetch user by id', () => {
const userStore = require('../stores/userStore');
const mockUser = {name: 'test', id: 123};
spyOn(UserService.prototype, 'fetchUserById')
.and.returnValue(Promise.resolve(mockUser));
dispatchEvent();
expect(userStore.user).toEqual(mockUser);
})
As expected this test if failed, because of asynchronous behavior of Promise, I'm understand the problem here but I'm can not find solution how to say to test wait until Promise from userService resolved.