From 918093517b3397cf52ab015f16d01cb0298bdadb Mon Sep 17 00:00:00 2001 From: Becca Bailey Date: Mon, 23 May 2022 14:57:30 -0700 Subject: [PATCH 1/3] Add victory-group tests --- test/jest/victory-group/victory-group.test.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/jest/victory-group/victory-group.test.js diff --git a/test/jest/victory-group/victory-group.test.js b/test/jest/victory-group/victory-group.test.js new file mode 100644 index 000000000..3c8828577 --- /dev/null +++ b/test/jest/victory-group/victory-group.test.js @@ -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( + + + + + ); + 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( + + + + + ); + const svg = container.querySelector("svg"); + const viewBoxValue = `0 0 ${450} ${300}`; + expect(svg.getAttribute("viewBox")).toEqual(viewBoxValue); + }); + + it("accepts user props", () => { + render( + + + + + ); + + expect(screen.getByTestId("victory-group")).toBeDefined(); + expect(screen.getByLabelText("Group")).toBeDefined(); + }); + }); +}); From 3ba55a1bb13b5ee9b0b3a05c0d211526e941f529 Mon Sep 17 00:00:00 2001 From: Becca Bailey Date: Mon, 23 May 2022 15:10:24 -0700 Subject: [PATCH 2/3] Add VictoryStack tests --- test/jest/victory-stack/victory-stack.test.js | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 test/jest/victory-stack/victory-stack.test.js diff --git a/test/jest/victory-stack/victory-stack.test.js b/test/jest/victory-stack/victory-stack.test.js new file mode 100644 index 000000000..9007d7010 --- /dev/null +++ b/test/jest/victory-stack/victory-stack.test.js @@ -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( + + + + + ); + 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( + + + + + ); + const svg = container.querySelector("svg"); + const viewBoxValue = `0 0 ${450} ${300}`; + expect(svg.getAttribute("viewBox")).toEqual(viewBoxValue); + }); + + it("accepts user props", () => { + const { container } = render( + + + + + ); + + 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( + + + + + ); + + 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( + + + + + ); + + expect(console.warn).not.toHaveBeenCalled(); + }); + }); +}); From bc36e8860aa3fd84f6b3c4063ede1b551e134637 Mon Sep 17 00:00:00 2001 From: Becca Bailey Date: Mon, 23 May 2022 15:14:59 -0700 Subject: [PATCH 3/3] Add selection container tests --- .../selection-helpers.test.js | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test/jest/victory-selection-container/selection-helpers.test.js diff --git a/test/jest/victory-selection-container/selection-helpers.test.js b/test/jest/victory-selection-container/selection-helpers.test.js new file mode 100644 index 000000000..a0c947de4 --- /dev/null +++ b/test/jest/victory-selection-container/selection-helpers.test.js @@ -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)]); + }); + }); +});