0

编辑 check-checkbox-hidden-with-jsdom

我已经尽可能简化了 CodeSandbox 来重现我的问题。您可以在 CodeSandbox 中看到失败的测试正在运行。

在我的示例中,我有一个名为的组件MyCheckbox,它只是 material-ui 的包装器Checkbox。它需要一个data只是一个数组的道具。如果数组中有东西,则复选框获取,opacity : 1否则获取opacity : 0

import React from "react";
import "./styles.css";
import { Checkbox, makeStyles } from "@material-ui/core";

const useStyles = makeStyles({
  checkboxHiddenStyle: {
    opacity: 0
  }
});

export default function MyCheckbox(props) {
  const styles = useStyles(props);
  return (
    <div>
      <Checkbox
        {...props}
        className={props.data.length === 0 && styles.checkboxHiddenStyle}
      />
    </div>
  );
}

MyCheckbox我创建了两个in实例MyCheckboxesInUse,一个可见,另一个不可见。

import React from "react";
import MyCheckbox from "./MyCheckbox";
import "./styles.css";

export default function MyCheckboxesInUse() {
  const arrayWithNothing = [];
  const arrayWithSomething = [1];
  return (
    <div className="App">
      <h1>Hidden Checkbox</h1>
      <MyCheckbox data={arrayWithNothing} />
      <h1>Visible Checkbox</h1>
      <MyCheckbox data={arrayWithSomething} />
    </div>
  );
}

....在浏览器中导致以下结果

在此处输入图像描述

然后我有一个简单的测试,检查第一个复选框是隐藏的,第二个是可见的

import React from "react";

import Enzyme, { mount } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import "@testing-library/jest-dom";
import MyCheckboxesInUse from "./MyCheckboxesInUse";
import MyCheckbox from "./MyCheckbox";

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

test("Check that one checkbox is hidden and the other is visible", () => {
  const wrapper = mount(<MyCheckboxesInUse />);

  const checkboxes = wrapper.find(MyCheckbox).find('input[type="checkbox"]');
  expect(checkboxes).toHaveLength(2);


  expect(checkboxes.at(0).getDOMNode()).not.toBeVisible();
  //This checkbox is in fact visible but the following test step is failing ??
  expect(checkboxes.at(1).getDOMNode()).toBeVisible();
});

即使第二个复选框清晰可见,测试也会失败并出现以下错误。这是一个错误jest还是jest-dom

expect(element).toBeVisible()

Received element is not visible:
  <input class="PrivateSwitchBase-input-5" data-indeterminate="false" type="checkbox" value="" />
4

0 回答 0