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

Convert tests for victory error bar #2266

Merged
merged 2 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions packages/victory-errorbar/src/error-bar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-statements */
import React from "react";
import PropTypes from "prop-types";
import { Helpers, CommonProps, Line } from "victory-core";
import { Helpers, CommonProps, Line, UserProps } from "victory-core";
import { assign } from "lodash";

const renderBorder = (props, error, type) => {
Expand All @@ -17,7 +17,8 @@ const renderBorder = (props, error, type) => {
x1: vertical ? error[type] : props.x - props.borderWidth,
x2: vertical ? error[type] : props.x + props.borderWidth,
y1: vertical ? props.y - props.borderWidth : error[type],
y2: vertical ? props.y + props.borderWidth : error[type]
y2: vertical ? props.y + props.borderWidth : error[type],
"data-type": `border-${type}`
becca-bailey marked this conversation as resolved.
Show resolved Hide resolved
});
};

Expand All @@ -34,7 +35,8 @@ const renderCross = (props, error, type) => {
x1: props.x,
x2: vertical ? props.x : error[type],
y1: props.y,
y2: vertical ? error[type] : props.y
y2: vertical ? error[type] : props.y,
"data-type": `cross-${type}`
});
};

Expand Down Expand Up @@ -80,7 +82,8 @@ const evaluateProps = (props) => {

const ErrorBar = (props) => {
props = evaluateProps(props);
const { ariaLabel, tabIndex } = props;
const userProps = UserProps.getSafeUserProps(props);
const { tabIndex, ariaLabel } = props;
const error = calculateError(props);
const children = [
error.right ? renderBorder(props, error, "right") : null,
Expand All @@ -94,7 +97,7 @@ const ErrorBar = (props) => {
].filter(Boolean);
return React.cloneElement(
props.groupComponent,
{ "aria-label": ariaLabel, tabIndex },
{ tabIndex, "aria-label": ariaLabel, ...userProps },
children
);
};
Expand Down
5 changes: 0 additions & 5 deletions test/jest/rendering-utils.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { render } from "@testing-library/react";
import React from "react";
import { renderInSvg } from "../../rendering-utils";
import { VictoryAccessibleGroup } from "victory-core";

describe("components/victory-accessible-group", () => {
it("renders an g with an aria-label", () => {
const { container } = renderInSvg(
<VictoryAccessibleGroup aria-label="test-aria-label" />
const { container } = render(
<VictoryAccessibleGroup aria-label="test-aria-label" />,
{ wrapper: "svg" }
);
expect(container.querySelector("g")).toMatchInlineSnapshot(`
<g
Expand All @@ -16,8 +17,9 @@ describe("components/victory-accessible-group", () => {
});

it("renders an g with a tabIndex and className", () => {
const { container } = renderInSvg(
<VictoryAccessibleGroup tabIndex={5} className="accessibility" />
const { container } = render(
<VictoryAccessibleGroup tabIndex={5} className="accessibility" />,
{ wrapper: "svg" }
);
expect(container.querySelector("g")).toMatchInlineSnapshot(`
<g
Expand All @@ -28,12 +30,13 @@ describe("components/victory-accessible-group", () => {
});

it("renders an g with a desc node if given", () => {
const { container } = renderInSvg(
const { container } = render(
<VictoryAccessibleGroup
aria-label="desc node tests"
desc="test description"
aria-describedby="describes group"
/>
/>,
{ wrapper: "svg" }
);
expect(container.querySelector("g")).toMatchInlineSnapshot(`
<g
Expand All @@ -51,11 +54,12 @@ describe("components/victory-accessible-group", () => {
});

it("uses the desc getAttribute value for descId and aria-describedby if no aria-describedby getAttribute value", () => {
const { container } = renderInSvg(
const { container } = render(
<VictoryAccessibleGroup
aria-label="desc node tests"
desc="applies to both aria-describeby and descId"
/>
/>,
{ wrapper: "svg" }
);
expect(container.querySelector("g")).toMatchInlineSnapshot(`
<g
Expand Down
108 changes: 64 additions & 44 deletions test/jest/victory-core/victory-label/victory-label.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import React from "react";
import { VictoryLabel, Log } from "victory-core";
import { screen, fireEvent } from "@testing-library/react";
import { renderInSvg } from "../../rendering-utils";
import { screen, fireEvent, render } from "@testing-library/react";

describe("components/victory-label", () => {
it("accepts user props", () => {
renderInSvg(
render(
<VictoryLabel
data-testid="victory-label"
aria-label="test-aria-label"
text="label"
/>
/>,
{ wrapper: "svg" }
);

expect(screen.getByTestId("victory-label")).toBeDefined();
expect(screen.getByLabelText("test-aria-label")).toBeDefined();
});

it("has expected content with render", () => {
const { container } = renderInSvg(<VictoryLabel text="such text, wow" />);
const { container } = render(<VictoryLabel text="such text, wow" />, {
wrapper: "svg"
});
expect(container.querySelector("tspan").innerHTML).toMatchInlineSnapshot(
`"such text, wow"`
);
});

it("sets dx and dy for text element", () => {
const { container } = renderInSvg(
<VictoryLabel dx={30} dy={30} text="such text, wow" />
const { container } = render(
<VictoryLabel dx={30} dy={30} text="such text, wow" />,
{ wrapper: "svg" }
);
const output = container.querySelector("text");
expect(output.getAttribute("dx")).toEqual("30");
Expand All @@ -35,37 +38,41 @@ describe("components/victory-label", () => {
});

it("sets x and y for text element", () => {
const { container } = renderInSvg(
<VictoryLabel x="100%" y={30} text="such text, wow" />
const { container } = render(
<VictoryLabel x="100%" y={30} text="such text, wow" />,
{ wrapper: "svg" }
);
const output = container.querySelector("text");
expect(output.getAttribute("x")).toEqual("100%");
expect(parseFloat(output.getAttribute("y"))).toEqual(34.97);
});

it("has a transform property that rotates the text to match the labelAngle getAttribute", () => {
const { container } = renderInSvg(
<VictoryLabel angle={46} text="such text, wow" />
const { container } = render(
<VictoryLabel angle={46} text="such text, wow" />,
{ wrapper: "svg" }
);
const output = container.querySelector("text");
expect(output.getAttribute("transform")).toContain("rotate(46");
});

it("accepts the angle getAttribute as a function", () => {
const { container } = renderInSvg(
<VictoryLabel angle={() => 46} text="such text, wow" />
const { container } = render(
<VictoryLabel angle={() => 46} text="such text, wow" />,
{ wrapper: "svg" }
);
const output = container.querySelector("text");
expect(output.getAttribute("transform")).toContain("rotate(46");
});

it("strips px from fontSize", () => {
const { container } = renderInSvg(
const { container } = render(
<VictoryLabel
style={{ fontSize: "10px" }}
text="such text, wow"
data-font-size={(props) => props.style.fontSize}
/>
/>,
{ wrapper: "svg" }
);
const output = container.querySelector("text");
expect(output.getAttribute("data-font-size")).toEqual("10");
Expand All @@ -75,36 +82,40 @@ describe("components/victory-label", () => {
// This suppresses the console warning for invalid fontSize prop
jest.spyOn(Log, "warn").mockImplementation(() => {});

const { container } = renderInSvg(
<VictoryLabel style={{ fontSize: "foo" }} text="such text, wow" />
const { container } = render(
<VictoryLabel style={{ fontSize: "foo" }} text="such text, wow" />,
{ wrapper: "svg" }
);
const output = container.querySelector("tspan");
expect(output.getAttribute("style")).toContain("font-size: 14px");
});

it("renders an array of text as seperate tspans", () => {
const { container } = renderInSvg(
<VictoryLabel text={["one", "two", "three"]} />
const { container } = render(
<VictoryLabel text={["one", "two", "three"]} />,
{ wrapper: "svg" }
);
const output = container.querySelectorAll("tspan");
expect(output.length).toEqual(3);
});

it("renders splits newlines into tspans", () => {
const { container } = renderInSvg(
<VictoryLabel text={"one\ntwo\nthree"} />
);
const { container } = render(<VictoryLabel text={"one\ntwo\nthree"} />, {
wrapper: "svg"
});
const output = container.querySelectorAll("tspan");
expect(output.length).toEqual(3);
});

it("renders title and desc if provided ", () => {
const { container } = renderInSvg(
<VictoryLabel text="title and desc" title="title" desc="desc" />
const { container } = render(
<VictoryLabel text="title and desc" title="title" desc="desc" />,
{ wrapper: "svg" }
);

const { container: container2 } = renderInSvg(
<VictoryLabel text="title and desc" />
const { container: container2 } = render(
<VictoryLabel text="title and desc" />,
{ wrapper: "svg" }
);

const title = container.querySelectorAll("title");
Expand All @@ -122,11 +133,12 @@ describe("components/victory-label", () => {

it("renders tspan styles independently when `style` is an array", () => {
const fill = ["red", "green", "blue"];
const { container } = renderInSvg(
const { container } = render(
<VictoryLabel
text={"one\ntwo\nthree"}
style={[{ fill: fill[0] }, { fill: fill[1] }, { fill: fill[2] }]}
/>
/>,
{ wrapper: "svg" }
);
const output = container.querySelectorAll("tspan");
output.forEach((tspan, index) => {
Expand All @@ -137,17 +149,19 @@ describe("components/victory-label", () => {
describe("event handling", () => {
it("attaches an to the parent object", () => {
const clickHandler = jest.fn();
const { container } = renderInSvg(
<VictoryLabel text="hi" events={{ onClick: clickHandler }} />
const { container } = render(
<VictoryLabel text="hi" events={{ onClick: clickHandler }} />,
{ wrapper: "svg" }
);
fireEvent.click(container.querySelector("text"));
expect(clickHandler).toHaveBeenCalled();
});
});

it("renders 'tspan' elements inline when `inline` getAttribute is passed", () => {
const { container } = renderInSvg(
<VictoryLabel text={["Inline", "label", "testing"]} inline dx={5} />
const { container } = render(
<VictoryLabel text={["Inline", "label", "testing"]} inline dx={5} />,
{ wrapper: "svg" }
);

const output = container.querySelectorAll("tspan");
Expand All @@ -164,11 +178,12 @@ describe("components/victory-label", () => {
it("passes lineHeight as an array if provided", () => {
const lineHeight = [1, 2, 3];
const expectedDy = [0, 21, 35];
const { container } = renderInSvg(
const { container } = render(
<VictoryLabel
text={["lineHeight", "array", "testing"]}
lineHeight={lineHeight}
/>
/>,
{ wrapper: "svg" }
);

const output = container.querySelectorAll("tspan");
Expand All @@ -183,11 +198,12 @@ describe("components/victory-label", () => {

it("defaults lineHeight to 1 if an empty array is provided for lineHeight", () => {
const expectedDy = [0, 14, 14, 14];
const { container } = renderInSvg(
const { container } = render(
<VictoryLabel
text={["lineHeight", "empty", "array", "testing"]}
lineHeight={[]}
/>
/>,
{ wrapper: "svg" }
);

const output = container.querySelectorAll("tspan");
Expand All @@ -197,8 +213,9 @@ describe("components/victory-label", () => {
});

it("defaults style to `defaultStyles` if an empty array is provided for `style`", () => {
const { container } = renderInSvg(
<VictoryLabel text={["style", "empty", "array", "testing"]} style={[]} />
const { container } = render(
<VictoryLabel text={["style", "empty", "array", "testing"]} style={[]} />,
{ wrapper: "svg" }
);

expect(
Expand All @@ -209,8 +226,9 @@ describe("components/victory-label", () => {
});

it("passes id if provided as a string", () => {
const { container } = renderInSvg(
<VictoryLabel text="Some VictoryLabel" id="my-custom-id" />
const { container } = render(
<VictoryLabel text="Some VictoryLabel" id="my-custom-id" />,
{ wrapper: "svg" }
);

const output = container.querySelectorAll("text");
Expand All @@ -220,8 +238,9 @@ describe("components/victory-label", () => {
});

it("passes id if provided as a number", () => {
const { container } = renderInSvg(
<VictoryLabel text="Some VictoryLabel" id={12345} />
const { container } = render(
<VictoryLabel text="Some VictoryLabel" id={12345} />,
{ wrapper: "svg" }
);

const output = container.querySelectorAll("text");
Expand All @@ -231,11 +250,12 @@ describe("components/victory-label", () => {
});

it("runs function if id provided as a function", () => {
const { container } = renderInSvg(
const { container } = render(
<VictoryLabel
text="Some VictoryLabel"
id={() => `created-in-function-${Math.random()}`}
/>
/>,
{ wrapper: "svg" }
);

const output = container.querySelectorAll("text");
Expand Down
Loading