-1

我想模拟 redux 存储并将针对 redux-store 的测试直接写入存储。我不希望任何角度逻辑介于两者之间。有人可以帮忙吗?

4

1 回答 1

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;
};
于 2019-11-16T21:29:41.373 回答