如何使用mocha正确测试redux-thunk动作

时间:2017-10-24 20:53:14

标签: unit-testing redux mocha redux-thunk

我遇到了为redux-thunk删除异步操作的麻烦。 Mocha不断给我超时

// actions.js
const fetchUserThings = userId => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve([{ foo: 'bar' }]);
    }
  });
};

const displayUserThings = userThings => ({
  type: 'DISPLAY',
  userThings
});

export const loadUserThings = userId => {
  return dispatch => {
    fetchUserThings(userId)
      .then(userThings => displayUserThings(userThings));
  };
}

这是我的摩卡测试。我使用的是redux on version 3.3.1redux-mock-store on version 0.0.6

import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import * as actions from './actions';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

describe('actions', () => {
  it('creates DISPLAY after fetching', done => {
    const userId = 1;
    const userThings = [{ foo: 'bar' }];
    const expectedActions = [
      { type: 'DISPLAY', userThings }
    ];

    const store = mockStore({userThings: []}, expectedActions, done);

    store.dispatch(actions.loadUserThings(userId);
  });
});

我错过了什么?

0 个答案:

没有答案