繁体   English   中英

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

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

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

由于 angular-redux 在内部使用普通的 redux ,因此您应该能够只调用减速器 function 本身。 如果没有 Angular,则不需要 mocking。 只需传递当前的 state 和给定的操作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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