簡體   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