【发布时间】:2020-02-28 16:22:17
【问题描述】:
我正在尝试创建类似于Summary.test.js 的SelectColumn.test.js
但不幸的是没有成功,我尝试了代码论坛的所有解决方案。但似乎我做错了什么。
结果:失败
预期结果应该是:通过
错误:
react-modal: 没有找到选择器 #root 的元素。
180 |
181 | render() {
> 182 | ReactModal.setAppElement("#root");
| ^
183 | return (
184 | <ReactModal
185 | className="overlay"
于 2020 年 3 月 1 日编辑:
我添加了<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,但这似乎不是解决方案。
于 2020 年 3 月 4 日编辑:
现在我更改了我的代码,因此它从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);
【问题讨论】:
标签: javascript reactjs testing integration-testing