Skip to content

Commit daa24d2

Browse files
author
Becca Bailey
authored
Merge pull request #2249 from FormidableLabs/chore/victory-tests
Add victory core tests
2 parents 0537eb1 + 2421648 commit daa24d2

26 files changed

+3160
-1
lines changed

packages/victory-core/src/victory-label/victory-label.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Log from "../victory-util/log";
1212
import * as CustomPropTypes from "../victory-util/prop-types";
1313
import * as Style from "../victory-util/style";
1414
import * as TextSize from "../victory-util/textsize";
15+
import * as UserProps from "../victory-util/user-props";
1516

1617
const defaultStyles = {
1718
fill: "#252525",
@@ -487,6 +488,7 @@ const renderLabel = (calculatedProps, tspanValues) => {
487488
tspanComponent,
488489
textComponent
489490
} = calculatedProps;
491+
const userProps = UserProps.getSafeUserProps(calculatedProps);
490492

491493
const textProps = {
492494
"aria-label": ariaLabel,
@@ -501,7 +503,8 @@ const renderLabel = (calculatedProps, tspanValues) => {
501503
title,
502504
desc: Helpers.evaluateProp(desc, calculatedProps),
503505
tabIndex: Helpers.evaluateProp(tabIndex, calculatedProps),
504-
id
506+
id,
507+
...userProps
505508
};
506509

507510
const tspans = text.map((line, i) => {
@@ -528,6 +531,7 @@ const VictoryLabel = (props) => {
528531
return null;
529532
}
530533
const calculatedProps = getCalculatedProps(props);
534+
531535
const { text, style, capHeight, backgroundPadding, lineHeight } =
532536
calculatedProps;
533537

test/jest/rendering-utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { render } from "@testing-library/react";
2+
import * as React from "react";
3+
4+
export const renderInSvg = (component) => {
5+
return render(<svg>{component}</svg>);
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from "react";
2+
import { renderInSvg } from "../../rendering-utils";
3+
import { VictoryAccessibleGroup } from "victory-core";
4+
5+
describe("components/victory-accessible-group", () => {
6+
it("renders an g with an aria-label", () => {
7+
const { container } = renderInSvg(
8+
<VictoryAccessibleGroup aria-label="test-aria-label" />
9+
);
10+
expect(container.querySelector("g")).toMatchInlineSnapshot(`
11+
<g
12+
aria-label="test-aria-label"
13+
class="VictoryAccessibleGroup"
14+
/>
15+
`);
16+
});
17+
18+
it("renders an g with a tabIndex and className", () => {
19+
const { container } = renderInSvg(
20+
<VictoryAccessibleGroup tabIndex={5} className="accessibility" />
21+
);
22+
expect(container.querySelector("g")).toMatchInlineSnapshot(`
23+
<g
24+
class="accessibility"
25+
tabindex="5"
26+
/>
27+
`);
28+
});
29+
30+
it("renders an g with a desc node if given", () => {
31+
const { container } = renderInSvg(
32+
<VictoryAccessibleGroup
33+
aria-label="desc node tests"
34+
desc="test description"
35+
aria-describedby="describes group"
36+
/>
37+
);
38+
expect(container.querySelector("g")).toMatchInlineSnapshot(`
39+
<g
40+
aria-describedby="describes group"
41+
aria-label="desc node tests"
42+
class="VictoryAccessibleGroup"
43+
>
44+
<desc
45+
id="describes group"
46+
>
47+
test description
48+
</desc>
49+
</g>
50+
`);
51+
});
52+
53+
it("uses the desc getAttribute value for descId and aria-describedby if no aria-describedby getAttribute value", () => {
54+
const { container } = renderInSvg(
55+
<VictoryAccessibleGroup
56+
aria-label="desc node tests"
57+
desc="applies to both aria-describeby and descId"
58+
/>
59+
);
60+
expect(container.querySelector("g")).toMatchInlineSnapshot(`
61+
<g
62+
aria-describedby="applies-to-both-aria-describeby-and-descId"
63+
aria-label="desc node tests"
64+
class="VictoryAccessibleGroup"
65+
>
66+
<desc
67+
id="applies-to-both-aria-describeby-and-descId"
68+
>
69+
applies to both aria-describeby and descId
70+
</desc>
71+
</g>
72+
`);
73+
});
74+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* eslint-disable no-unused-expressions */
2+
import { victoryInterpolator } from "victory-core/lib/victory-animation/util";
3+
4+
describe("victoryInterpolator", () => {
5+
it("does not attempt to interpolate identical values", () => {
6+
// This case fails with the default interpolator, returning *almost* 3.
7+
expect(victoryInterpolator(3, 3)(0.25920000000000004)).toEqual(3);
8+
});
9+
10+
it("does not attempt to interpolate Boolean values", () => {
11+
// The default interpolator would return 0.5.
12+
expect(victoryInterpolator(false, true)(0.5)).toEqual(true);
13+
});
14+
15+
it("always returns the end value if starting from null", () => {
16+
const interpolator = victoryInterpolator(null, 5);
17+
expect(interpolator(0)).toEqual(5);
18+
expect(interpolator(0.49)).toEqual(5);
19+
expect(interpolator(0.5)).toEqual(5);
20+
expect(interpolator(1)).toEqual(5);
21+
});
22+
23+
it("always returns the end value if ending on null", () => {
24+
const interpolator = victoryInterpolator(5, null);
25+
expect(interpolator(0)).toBeNull();
26+
expect(interpolator(0.49)).toBeNull();
27+
expect(interpolator(0.5)).toBeNull();
28+
expect(interpolator(1)).toBeNull();
29+
});
30+
31+
it("always returns the end value if starting from undefined", () => {
32+
const interpolator = victoryInterpolator(undefined, 5);
33+
expect(interpolator(0)).toEqual(5);
34+
expect(interpolator(0.49)).toEqual(5);
35+
expect(interpolator(0.5)).toEqual(5);
36+
expect(interpolator(1)).toEqual(5);
37+
});
38+
39+
it("always returns the end value if ending on undefined", () => {
40+
const interpolator = victoryInterpolator(5, undefined);
41+
expect(interpolator(0)).toBeUndefined();
42+
expect(interpolator(0.49)).toBeUndefined();
43+
expect(interpolator(0.5)).toBeUndefined();
44+
expect(interpolator(1)).toBeUndefined();
45+
});
46+
47+
it("interpolates functions", () => {
48+
const fromFn = () => 5;
49+
const toFn = () => 10;
50+
const interpolator = victoryInterpolator(fromFn, toFn);
51+
const halfwayFn = interpolator(0.5);
52+
expect(halfwayFn).toBeInstanceOf(Function);
53+
expect(halfwayFn()).toEqual(7.5);
54+
});
55+
56+
it("interpolates string values", () => {
57+
// From https://github.com/d3/d3-interpolate/blob/main/test/value-test.js#L5-L7
58+
const interpolator = victoryInterpolator("foo", "bar");
59+
expect(interpolator(0.5)).toEqual("bar");
60+
});
61+
62+
it("interpolates color values", () => {
63+
// From https://github.com/d3/d3-interpolate/blob/main/test/value-test.js#L15
64+
const interpolator = victoryInterpolator("red", "blue");
65+
expect(interpolator(0.5)).toEqual("rgb(128, 0, 128)");
66+
});
67+
68+
it("interpolates object values", () => {
69+
// From https://github.com/d3/d3-interpolate/blob/main/test/value-test.js#L44
70+
const interpolator = victoryInterpolator(
71+
{ color: "red" },
72+
{ color: "blue" }
73+
);
74+
expect(interpolator(0.5)).toEqual({ color: "rgb(128, 0, 128)" });
75+
});
76+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import React from "react";
2+
import { VictoryContainer } from "victory-core";
3+
import { fireEvent, render } from "@testing-library/react";
4+
5+
describe("components/victory-container", () => {
6+
it("renders an svg with a role of img", () => {
7+
const { container } = render(<VictoryContainer />);
8+
const output = container.querySelector("svg");
9+
expect(output.getAttribute("role")).toContain("img");
10+
});
11+
12+
it("renders an svg with a custom role", () => {
13+
const { container } = render(<VictoryContainer role="presentation" />);
14+
expect(container.querySelector("svg").getAttribute("role")).toEqual(
15+
"presentation"
16+
);
17+
});
18+
19+
it("renders an svg with a title node", () => {
20+
const { container } = render(<VictoryContainer title="Victory Chart" />);
21+
expect(container.querySelector("title")).toMatchInlineSnapshot(`
22+
<title
23+
id="victory-container-3-title"
24+
>
25+
Victory Chart
26+
</title>
27+
`);
28+
});
29+
30+
it("renders an svg with a desc node", () => {
31+
const { container } = render(<VictoryContainer desc="description" />);
32+
expect(container.querySelector("desc")).toMatchInlineSnapshot(`
33+
<desc
34+
id="victory-container-4-desc"
35+
>
36+
description
37+
</desc>
38+
`);
39+
});
40+
41+
it("renders an svg with an aria-describedby attribute", () => {
42+
const { container } = render(
43+
<VictoryContainer aria-describedby="testid" desc="description" />
44+
);
45+
const describedElement = container.querySelector(
46+
`svg[aria-describedby~="testid"]`
47+
);
48+
expect(describedElement).toBeDefined();
49+
});
50+
51+
it("renders an svg with an aria-labelledby attribute", () => {
52+
const { container } = render(
53+
<VictoryContainer aria-labelledby="testid" title="title" />
54+
);
55+
const describedElement = container.querySelector(
56+
`svg[aria-labelledby~="testid"]`
57+
);
58+
expect(describedElement).toBeDefined();
59+
});
60+
61+
it("renders an svg with the correct viewbox", () => {
62+
const width = 300;
63+
const height = 300;
64+
const { container } = render(
65+
<VictoryContainer width={width} height={height} />
66+
);
67+
const svg = container.querySelector("svg");
68+
const viewBoxValue = `0 0 ${width} ${height}`;
69+
expect(svg.getAttribute("viewBox")).toEqual(viewBoxValue);
70+
});
71+
72+
it("attaches an event to the container", () => {
73+
const clickHandler = jest.fn();
74+
const { container } = render(
75+
<VictoryContainer events={{ onClick: clickHandler }} />
76+
);
77+
const svg = container.querySelector("svg");
78+
fireEvent.click(svg);
79+
expect(clickHandler).toBeCalled();
80+
});
81+
});

0 commit comments

Comments
 (0)