Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for victory-stack, victory-group, and victory-selection-container #2253

Merged
merged 3 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions test/jest/victory-group/victory-group.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { render, screen } from "@testing-library/react";
import React from "react";
import { VictoryBar } from "victory-bar";
import { VictoryGroup } from "victory-group";

describe("components/victory-group", () => {
it("has a static role", () => {
expect(VictoryGroup.role).toEqual("group");
});

describe("default component rendering", () => {
it("renders an svg with the correct width and height", () => {
const { container } = render(
<VictoryGroup>
<VictoryBar />
<VictoryBar />
</VictoryGroup>
);
const svg = container.querySelector("svg");
expect(svg.style.width).toEqual("100%");
expect(svg.style.height).toEqual("100%");
});

it("renders an svg with the correct viewBox", () => {
const { container } = render(
<VictoryGroup>
<VictoryBar />
<VictoryBar />
</VictoryGroup>
);
const svg = container.querySelector("svg");
const viewBoxValue = `0 0 ${450} ${300}`;
expect(svg.getAttribute("viewBox")).toEqual(viewBoxValue);
});

it("accepts user props", () => {
render(
<VictoryGroup data-testid="victory-group" aria-label="Group">
<VictoryBar />
<VictoryBar />
</VictoryGroup>
);

expect(screen.getByTestId("victory-group")).toBeDefined();
expect(screen.getByLabelText("Group")).toBeDefined();
});
});
});
77 changes: 77 additions & 0 deletions test/jest/victory-selection-container/selection-helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { assign } from "lodash";
import React from "react";
import { VictoryBar } from "victory-bar";
import { SelectionHelpers } from "victory-selection-container";
import * as d3Scale from "victory-vendor/d3-scale";

const scale = { x: d3Scale.scaleLinear(), y: d3Scale.scaleLinear() };

describe("helpers/selection", () => {
describe("getDatasets", () => {
it("returns data from props", () => {
const data = [
{ x: 1, y: 3 },
{ x: 2, y: 5 }
];
const props = { data };
const dataset = SelectionHelpers.getDatasets(props);
expect(dataset).toEqual([{ data }]);
});

it("returns data from children", () => {
const data = [
{ eventKey: 0, x: 1, y: 3 },
{ eventKey: 1, x: 2, y: 5 }
];
const expectedReturn = [
{ eventKey: 0, x: 1, _x: 1, y: 3, _y: 3 },
{ eventKey: 1, x: 2, _x: 2, y: 5, _y: 5 }
];
const name = "points";
const children = [React.createElement(VictoryBar, { name, data })];
const props = { children };
const dataset = SelectionHelpers.getDatasets(props);
expect(dataset).toEqual([{ childName: name, data: expectedReturn }]);
});
});

describe("filterDatasets", () => {
it("returns null when no datasets are within bounds", () => {
const datasets = [
{
childName: "a",
data: [
{ eventKey: 0, _x: 1, _y: 3 },
{ eventKey: 1, _x: 2, _y: 5 }
]
}
];
const props = { scale, x1: 0, y1: 0, x2: 0.5, y2: 0.5 };
const bounds = { x: [0, 1], y: [10, 15] };
const filteredData = SelectionHelpers.filterDatasets(
props,
datasets,
bounds
);
expect(filteredData).toBeNull();
});

it("returns data points within bounds", () => {
const data = [
{ eventKey: 0, _x: 0, _y: 0 },
{ eventKey: 1, _x: 2, _y: 5 }
];
const childName = "a";
const datasets = [{ childName, data }];
const bounds = { x: [0, 1], y: [0, 10] };
const props = { scale, x1: 0, y1: 0, x2: 0.5, y2: 0.5 };
const filteredData = SelectionHelpers.filterDatasets(
props,
datasets,
bounds
);
const expected = { eventKey: [0], data: [data[0]] };
expect(filteredData).toEqual([assign({ childName }, expected)]);
});
});
});
84 changes: 84 additions & 0 deletions test/jest/victory-stack/victory-stack.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* global console */
/* eslint-disable max-nested-callbacks */
/* eslint-disable no-console */

import { render } from "@testing-library/react";
import React from "react";
import { VictoryBar } from "victory-bar";
import { VictoryHistogram } from "victory-histogram";
import { VictoryStack } from "victory-stack";

describe("components/victory-stack", () => {
describe("default component rendering", () => {
it("renders an svg with the correct width and height", () => {
const { container } = render(
<VictoryStack>
<VictoryBar />
<VictoryBar />
</VictoryStack>
);
const svg = container.querySelector("svg");
expect(svg.style.width).toEqual("100%");
expect(svg.style.height).toEqual("100%");
});

it("renders an svg with the correct viewBox", () => {
const { container } = render(
<VictoryStack>
<VictoryBar />
<VictoryBar />
</VictoryStack>
);
const svg = container.querySelector("svg");
const viewBoxValue = `0 0 ${450} ${300}`;
expect(svg.getAttribute("viewBox")).toEqual(viewBoxValue);
});

it("accepts user props", () => {
const { container } = render(
<VictoryStack data-testid="victory-stack" aria-label="Stack">
<VictoryBar />
<VictoryBar />
</VictoryStack>
);

const svgNode = container.querySelector("svg");
expect(svgNode.getAttribute("data-testid")).toEqual("victory-stack");
expect(svgNode.getAttribute("aria-label")).toEqual("Stack");
});
});

describe("warnings", () => {
beforeEach(() => {
jest.spyOn(console, "warn").mockImplementation(() => {});
});

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

it("should warn when histogram children are mixed with non-histogram children", () => {
render(
<VictoryStack>
<VictoryHistogram />
<VictoryBar />
</VictoryStack>
);

expect(console.warn).toHaveBeenCalledWith(
"VictoryHistogram only supports being stacked with other VictoryHistogram components. Check to make sure that you are only passing VictoryHistogram components to VictoryStack"
);
});

it("should not warn when only histogram children are passed", () => {
render(
<VictoryStack>
<VictoryHistogram />
<VictoryHistogram />
</VictoryStack>
);

expect(console.warn).not.toHaveBeenCalled();
});
});
});