Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Testing Redux reducer fails to pass

New to unit testing in general and especially in Redux, forgive the dumb question.

I’m trying to test a simple reducer but can’t get to make it work.

Reducer:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { ActionTypes } from "../constants/action-types";

const initialState = {
  products: [],
};

export const productReducer = (state = initialState, { type, payload }) => {
  switch (type) {
    case ActionTypes.FETCH_PRODUCTS:
      return { ...state, products: payload };
    case ActionTypes.RESET_PRODUCTS:
      return {};
    default:
      return state;
  }
};

Test:

import { ActionTypes } from "../../redux/constants/action-types";
import { productReducer } from ".././../redux/reducers/productReducer";

describe("Product reducer", () => {
  describe("fetching products", () => {
    it("adds products", () => {
      const action = {
        type: ActionTypes.FETCH_PRODUCTS,
        product: [{ name: "test" }],
      };

      const initialState = undefined;
      const nextState = productReducer(initialState, action);

      expect(nextState).toEqual([{ name: "test" }]);
    });
  });
});

That’s what I get:

expect(received).toEqual(expected) // deep equality

Expected: [{"name": "test"}]
Received: {"products": undefined}

Just don’t understand how to test it.

>Solution :

use payload instead of product

     const action = {
        type: ActionTypes.FETCH_PRODUCTS,
        payload: [{ name: "test" }],
      };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading