reduxredux-mock-store

What is the second argument for?


I find this code in a tutorial

...
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();
        });
});

and the whole bit with expectedActions doesn't make sense.

The docs say that if there is a second argument to store, it should be a function; (no explanation telling what that function would do though).

At first I thought it was forcing some actions into the store for some reason, but a quick console.log told me that wasn't the case.

Because only dispatch causes actions to accumulate.

So is it a mistake in the text or some wisdom to explore further?


Solution

  • This feature was removed in version 1, but you can find the example in the pre 1 docs.

    The parameter expectedActions is used for testing. You can create a mock store with an array of actions, and then dispatch an the 1st action. This action will cause the other other actions to forwarded (dispatch / next) via thunks/api middleware/etc... The test will check if all of the actions in the expectedActions array have acted on the store:

    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);
        })