-1

I want to mock the redux store and write tests against the redux-store directly to the store. I don't want any angular logic to come in between. Can somebody help?

1 Answers1

1

Since angular-redux is using plain redux inside you should be able to just invoke the reducer function itself. Without Angular, no mocking is needed. Just pass a current state and a given action.

// reducers.spec.ts
import {ticketsReducer} from './reducer.ts'

describe('Ticket Reducer Test', () => {

  it('should add one ticket', () => {
    // given
    const currentstate = 1;
    const action = { type: 'ADD_TICKET '};
    // when
    const state = ticketsReducer(state, action);
    // then
    expect(state).toBe(2);
  })

});

// reducer.ts
export const ticketsReducer: Reducer<number> = (state = 0, action: Action) => {
  switch (action.type) {
    case ADD_TICKET:
      return state + 1;
    case REMOVE_TICKET:
      return Math.max(0, state - 1);
  }
  return state;
};
ChrisY
  • 1,681
  • 10
  • 12
  • Great suggestion. I was actually thinking, can we use any mock store library. So that I can test end to end app state. Say I create a singleton store and uses it in every test case – MAHESH VALIYA VEETIL Nov 17 '19 at 07:59
  • Of course you can import the same state object from a different file in every test case to get the same initial state, but I would not modify it over multiple test cases because so you don't have independent tests anymore. One test fails and potentially all others fail too. Anyways I don't think you need any library regardless of what you are trying to achieve. You can decide whether you share your state object over multiple `it()` or `describe()` blocks. – ChrisY Nov 17 '19 at 09:04