Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to test combineReducers with jest

I am trying to test combineReducers but getting following error.

TypeError: _testReducer.testReducer.test11 is not a function

Following is the reducer

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

Following is the test case

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

It worked if I export all functions in reducer and import individual in test case but thats not the idea case.

like image 648
swapnilpatil24 Avatar asked Nov 30 '25 04:11

swapnilpatil24


1 Answers

combineReducers returns a function that merges the states of all the reducers you put in it. It does not allow you to access individual reducers and call them individually.

You should use it this way for your case:

expect(
    testReducer(true, {type: "temp11"}).test11
).toEqual(returnTrueValue);
like image 129
Moti Azu Avatar answered Dec 03 '25 00:12

Moti Azu