我正在尝试创建类似于Summary.test.js
的SelectColumn.test.js
结果:失败
期望的结果应该是:通过
错误:
react-modal:找不到选择器#root的任何元素。
180 |
181 | render() {
> 182 | ReactModal.setAppElement("#root");
| ^
183 | return (
184 | <ReactModal
185 | className="overlay"
2020年01月03日编辑:
我添加了<div id="root">
,解决了错误:
react-modal:未找到选择器#root的任何元素。
但现在出现了新的错误:
TypeError:parentInstance.children.indexOf不是一个函数
代码如下:
81 | .create(<Overview data={data} />)
82 | .getInstance();
> 83 | const component = renderer.create(component_func(overview));
| ^
84 |
85 | expect(component).toMatchSnapshot();
86 | });
我尝试将
ReactModal.setAppElement
移动到componentDidMount
中,但似乎这不是解决方案。编辑于04.03.2020:
现在我更改了我的代码,让它从
App.js
的主组件进行测试,这很好用,但我想仅测试子组件SelectColumn
。我尝试使用shallow
和find
,但快照始终为exports[...] = null;
。import React from "react";
import SelectColumn from "../App.js";
import { test_snapshot_of } from "./utils.js";
import Enzyme, { shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
Enzyme.configure({ adapter: new Adapter() });
// mock uniqid to have consistent names
// https://stackoverflow.com/a/44538270/396730
jest.mock("uniqid", () => i => i + "uniqid");
const wrapper = shallow(<SelectColumn />);
const rootElement = wrapper.find("#root");
test_snapshot_of("Render SelectColumn", overview => rootElement);
react-modal: 未找到选择器#root的元素。
22 | 23 | componentDidMount() {` > 24 | ReactModal.setAppElement("#root"); | ^ 25 | } 26 | // -------------------------Rendering------------------------- 27 | renderRunSets = () => {
- MartinTypeError: parentInstance.children.indexOf is not a function
,位于第83行:const component = renderer.create(component_func(overview));
。 - Martin#root
的元素吗?如果是的话,也许你可以尝试将ReactModal.setAppElement
移动到componentDidMount
中,实际上最好是创建另一个问题并提供更多信息 :) - Carr