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:

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" }],
      };

Leave a Reply