我在教程中找到了这段代码
...
import configureMockStore from 'redux-mock-store';
const middleware = [thunk];
const mockStore = configureMockStore(middleware);
...
it('should create BEGIN_AJAX_CALL & LOAD_COURSES_SUCCESS', (done) => {
const expectedActions = [
{type: types.BEGIN_AJAX_CALL},
{type: types.LOAD_COURSES_SUCCESS, body: {
courses: [{id:'clean-code', title:'Clean Code'}]
}}
];
const store = mockStore({courses:[]}, expectedActions);
store
.dispatch(courseActions.loadCourses())
.then(() => {
const actions = store.getActions();
expect(actions[0].type).toEqual(types.BEGIN_AJAX_CALL);
expect(actions[1].type).toEqual(types.LOAD_COURSES_SUCCESS);
done();
});
});expectedActions的全部内容都是毫无意义的。
这些文档说,如果store有第二个参数,它应该是一个函数;(但是没有解释说明这个函数会做什么)。
起初,我认为这是出于某种原因迫使商店采取了一些行动,但一个快速的console.log告诉我,情况并非如此。
因为只有dispatch才会导致操作累积。
那么,这是文本中的错误,还是进一步探索的智慧?
发布于 2016-12-07 05:55:33
此特性在version 1中被删除,但您可以在Pre1 文档中找到该示例。
参数expectedActions用于测试。您可以创建一个包含一系列操作的模拟存储,然后分派第一个操作。此操作将导致其他操作通过组/api中间件/等转发(分派/下一步).测试将检查expectedActions数组中的所有操作是否都对存储区起作用:
import configureStore from 'redux-mock-store';
const middlewares = []; // add your middlewares like `redux-thunk`
const mockStore = configureStore(middlewares);
// Test in mocha
it('should dispatch action', (done) => {
const getState = {}; // initial state of the store
const action = { type: 'ADD_TODO' };
const expectedActions = [action];
const store = mockStore(getState, expectedActions, done);
store.dispatch(action);
})https://stackoverflow.com/questions/41006825
复制相似问题