samedi 14 septembre 2019

How to test (Unit testing) reducer in React which filtered an array of objects

I have a problem can't write a correct test for one of my reducers.

My reducer look:

case "FILTER_BY_VALUE": {
      return {
        ...state,
        search: payload,
        filteredProducts: [
          ...state.products.filter(product =>
            product.name.toLowerCase().startsWith(payload.toLowerCase())
          )
        ]
      };
    }

My reducer filter an array of objects by value (in my expamle it is payload)

My test look :

it("FILTER_BY_VALUE", () => {
    const action = {
      type: "FILTER_BY_VALUE",
      payload: "dsf"
    };
    expect(data(initialState, action)).toEqual({
      ...initialState,
      search: action.payload
      //   filteredProducts: Object
    });
  });

Error is in image: enter image description here

Aucun commentaire:

Enregistrer un commentaire