@ angular-redux / store:如何为redux商店编写单元测试?

时间:2019-11-16 21:10:28

标签: angular redux angular-redux

我想模拟Redux存储并将针对Redux存储的测试直接写到存储中。我不希望介于两者之间。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

由于angular-redux在内部使用普通redux,因此您应该能够仅调用reducer函数本身。如果没有Angular,则无需模拟。只需传递当前状态和给定的动作即可。

// 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;
};
相关问题