问题:当我运行我的代码时,SVG 附加到组件的外部 div 并在页面上完美呈现。但是,在测试期间,SVG 不会附加到外部 div。
堆栈:我正在尝试使用 Jest/Enzyme 测试包装在 React 组件中的 d3js 元素。
到目前为止我所做的研究:查找了一堆关于使用 google 和 stackoverflow 测试 d3js 的指南/帖子。使用了关于单元测试 d3js 和 React with Jasmine 的指南。我还读到,使用 Enzyme 测试 3rd 方库可能很困难,因此这可能是测试框架的限制。
我已经包含了一个基本的代码示例来测试。
成分:
import React from "react";
import * as d3 from "d3";
export default class Circle extends React.Component {
componentDidMount = () => {
return this.renderCircle();
}
renderCircle = () => {
d3.select("#outerDiv")
.append("svg")
.attr("width", 600)
.attr("height", 300)
.append("circle")
.attr("id", "d3-el")
.attr("cx", 300)
.attr("cy", 150)
.attr("r", 30) …Run Code Online (Sandbox Code Playgroud)