如何用笑话测试CombineReducer

时间:2019-03-27 08:50:42

标签: reactjs redux react-redux jestjs reactjs-testutils

我正在尝试测试CombineReducers,但出现以下错误。

TypeError:_testReducer.testReducer.test11不是函数

以下是减速器

// testReducer.js
import { combineReducers } from "redux-immutable";
const test11 = (state, action) => {
 switch (action.type) {
    case "temp11":
      return true;
    default:
      return state;
  }
};
const test22 = (state, action) => {
 switch (action.type) {
    case "temp22":
      return false;
    default:
      return state;
  }
};
export const testReducer = combineReducers({
  test11,
  test22,
});

以下是测试案例

// testReducer.test.js
import { testReducer } from "./testReducer.js";
describe("test for testReducer", () => {
  it("test11", () => {
    const returnTrueValue = true;
    expect(
      testReducer.test11(
        true, {
          type: "temp11",
        }
      )
    ).toEqual(returnTrueValue);
  });
  it("test11", () => {
    const returnFalseValue = false;
    expect(
      testReducer.test22(
        true, {
          type: "temp22",
        }
      )
    ).toEqual(returnFalseValue);
  });
});

如果我在reducer中导出所有函数并在测试用例中导入单个函数,那是可行的,但那不是主意。

2 个答案:

答案 0 :(得分:1)

我假设组件 reducer 如下所示:

export const initialState = {
  totalValue: 0
};

const DashboardReducer = (state = initialState, action: any)=> {
  switch(action.type) {
    case 'case1':
     .......
     .......
     .......
    default:
      return state;
 }

我假设 root reducer 如下所示:

export const rootReducer = combineReducers({
  dashboard: DashboardReducer
});

测试文件应如下所示:

import { rootReducer } from './root.reducer';
import { createStore } from 'redux';
import { initialState as dashboardState } from './modules/uv_dashboard/uv_dashboard.reducer';

describe('Root Reducer Suite', () => {

  let store = createStore(rootReducer)

  test('loaded correctly', () => {
    expect(store.getState().dashboard).toEqual(dashboardState);
  });
});

答案 1 :(得分:0)

calendar.add()返回一个函数,该函数合并您放入其中的所有reducer的状态。它不允许您访问单独的异径管并单独调用它们。

您应该以这种方式使用它:

combineReducers