使用第 3 部分 SVG 图表生成 Jest 快照 (Nivo)

发布于 2025-01-16 14:39:51 字数 1730 浏览 5 评论 0原文

我正在尝试为使用 Nivo 图表库生成图表(特别是 ResponsivePie 图表)的图表组件编写单元测试。当我尝试渲染组件的快照时,我得到的只是周围的 Div 元素,其中没有任何内容。

我尝试过,

  • getBBox
  • 添加一个引用,
  • 在组件周围添加一个具有高度和宽度的 div
  • 模拟引用

我的测试文件

import React from "react";
import "i18n_test";
import { render, cleanup } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import DonutChart from "./DonutChart";

afterEach(() => {
  jest.clearAllMocks();
  cleanup();
});


const data = [
  {
    id: "make",
    label: "make",
    value: 68,
  },
  {
    id: "elixir",
    label: "elixir",
    value: 580,
  },
  {
    id: "stylus",
    label: "stylus",
    value: 286,
  },
  {
    id: "python",
    label: "python",
    value: 26,
  },
  {
    id: "scala",
    label: "scala",
    value: 151,
  },
];
const seed = 0;
const total = 4;
const showGrey = false;

const onClick = jest.fn();

describe("DonutChart Component", () => {
  it("Snapshot render", async () => {
    const useMockRef = React.createRef(null);
    const { container } = render(
      <div style={{ width: "200px", height: "200px" }} ref={useMockRef}>
        <DonutChart
          data={data}
          seed={seed}
          total={total}
          showGrey={showGrey}
          onClick={onClick}
        />
      </div>
    );
    expect(container.firstChild).toMatchSnapshot();
  });
});

我生成的快照

exports[`DonutChart Component Snapshot render 1`] = `
<div
  style="width: 100%; height: 100%;"
>
  <div
    class="has-other"
    style="width: 100%; height: 100%;"
  >
    <div
      style="width: 100%; height: 100%;"
    />
  </div>
</div>
`;

I am trying to write unit tests for a chart component that uses the Nivo charting library to generate the chart, specifically the ResponsivePie chart. When I try to render the component for a snapshot all I get is the surrounding Div element with nothing in it.

I have tried,

  • getBBox
  • adding a ref
  • adding a div with height and width around the component
  • mocking up the ref

My test file

import React from "react";
import "i18n_test";
import { render, cleanup } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import DonutChart from "./DonutChart";

afterEach(() => {
  jest.clearAllMocks();
  cleanup();
});


const data = [
  {
    id: "make",
    label: "make",
    value: 68,
  },
  {
    id: "elixir",
    label: "elixir",
    value: 580,
  },
  {
    id: "stylus",
    label: "stylus",
    value: 286,
  },
  {
    id: "python",
    label: "python",
    value: 26,
  },
  {
    id: "scala",
    label: "scala",
    value: 151,
  },
];
const seed = 0;
const total = 4;
const showGrey = false;

const onClick = jest.fn();

describe("DonutChart Component", () => {
  it("Snapshot render", async () => {
    const useMockRef = React.createRef(null);
    const { container } = render(
      <div style={{ width: "200px", height: "200px" }} ref={useMockRef}>
        <DonutChart
          data={data}
          seed={seed}
          total={total}
          showGrey={showGrey}
          onClick={onClick}
        />
      </div>
    );
    expect(container.firstChild).toMatchSnapshot();
  });
});

My resulting snapshot

exports[`DonutChart Component Snapshot render 1`] = `
<div
  style="width: 100%; height: 100%;"
>
  <div
    class="has-other"
    style="width: 100%; height: 100%;"
  >
    <div
      style="width: 100%; height: 100%;"
    />
  </div>
</div>
`;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文