如何以相等的道具获得相同成分的数量

时间:2019-09-05 05:07:05

标签: reactjs testing jestjs enzyme

我正在尝试获得多少组件在反应测试中具有相同的道具。我映射并排列数组,然后根据一个条件返回是否已选中的复选框。我渲染5个复选框并选中3。

如何验证是否真的选中了3个复选框?

props.services是所有可能的服务(一个复选框),而props.activeCoupon.services是所有将被选中的复选框

import React from "react";
import Adapter from "enzyme-adapter-react-16";
import expect from "expect";
import { configure, shallow } from "enzyme";
import CouponsServices from "./CouponsServices";
import { Checkbox } from "react-bootstrap";

configure({ adapter: new Adapter() });

const props = {
  services: [
    {
      id: 1,
      name: "Service 1"
    },
    {
      id: 2,
      name: "Service 2"
    },
    {
      id: 3,
      name: "Service 3"
    },
    {
      id: 4,
      name: "Service 4"
    },
    {
      id: 5,
      name: "Service 5"
    }
  ],
  activeCoupon: {
    id: 1,
    code: "Code 1",
    startDate: "2018-02-01",
    endDate: "2018-02-28",
    services: [
      {
        id: 1,
        name: "Service 1"
      },
      {
        id: 3,
        name: "Service 3"
      },
      {
        id: 4,
        name: "Service 4"
      }
    ],
    value: 20,
    isPercent: false
  },
  setState: jest.fn()
};

describe("<CouponsServices />", () => {
  const wrapper = shallow(<CouponsServices {...props} />);
  it("lists all available services", () => {
    expect(wrapper.find(Checkbox).length).toEqual(props.services.length);
  });
});

0 个答案:

没有答案