@angular-redux/store : How to write unit test for the redux store?

664 Views Asked by At

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

There are 1 best solutions below

2
ChrisY On

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