jeudi 4 juin 2020

What is the best way to add props to a enzyme/jest test?

I am running my test using enzyme/jest with react. In my component, I am using .map function to pull data out of an array, which is passed down in props. I am getting an error when running the test, that the .map function is not found. How do I correct this error?

ERROR:

FAIL src/components/content/tests/ContentCard.test.js (42.25s) ● renders correctly enzyme

TypeError: Cannot read property 'map' of undefined

   5 |   <div>
   6 |     <div className="resource-list">
>  7 |       {props.data.map((item) => (
     |                   ^
   8 |         <a key={item.id} href={item.link} className="resource-card-link mr-3">
   9 |           <div className="card resource-card mb-2">
  10 |             <div className="card-header">

  at _default (src/components/content/ContentCard.js:7:19)

TEST:

import React from "react";
import ContentCard from "../ContentCard";

import { shallow } from "enzyme";

it("renders correctly enzyme", () => {
  const wrapper = shallow(<ContentCard />);

  expect(wrapper).toMatchSnapshot();
});

Aucun commentaire:

Enregistrer un commentaire