0

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.

1 Answer 1

1

use payload instead of product

     const action = {
        type: ActionTypes.FETCH_PRODUCTS,
        payload: [{ name: "test" }],
      };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.