简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM