Thursday, 21 February 2019

Testing a redux action

I am trying to implement jest with our redux actions. Given the below action foo and it's following test, the following test is failing because store.getActions() is only returning me [{"type": "ACTION_ONE"}] as supposed to [{"type": "ACTION_ONE"}, {"type": "ACTION_TWO"}]. How do I get both dispatched actions when testing? Thanks!

import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

export const foo = () => {
  return (dispatch) => {
    dispatch(actionOne());
    return HttpService.get(`api/sampleUrl`)
      .then(json => dispatch(actionTwo(json.data)))
      .catch(error => handleError(error));
  };
};

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

beforeEach(() => {
  store = mockStore({});
});

describe('sample test', () => {
  test('validates foo complex action', () => {
    const expectedActions = [
      {type: actionTypes.ACTION_ONE},
      {type: actionTypes.ACTION_TWO},
    ];

    return store.dispatch(actions.foo())
      .then(() => {
        expect(store.getActions())
          .toEqual(expectedActions);
      });
  });
});



from Testing a redux action

No comments:

Post a Comment