Skip to content

Commit 9218fcd

Browse files
author
Becca Bailey
authored
Merge pull request #2246 from FormidableLabs/chore/victory-tests
Convert victory and victory-chart tests to jest
2 parents 86ba846 + a4fbb65 commit 9218fcd

File tree

6 files changed

+410
-3
lines changed

6 files changed

+410
-3
lines changed

packages/victory-axis/src/victory-axis.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
LineSegment,
1111
TextSize,
1212
addEvents,
13-
Axis
13+
Axis,
14+
UserProps
1415
} from "victory-core";
1516
import { getBaseProps, getStyles } from "./helper-methods";
1617

@@ -268,6 +269,7 @@ class VictoryAxis extends React.Component {
268269
render() {
269270
const { animationWhitelist } = VictoryAxis;
270271
const props = Axis.modifyProps(this.props, fallbackProps);
272+
const userProps = UserProps.getSafeUserProps(this.props);
271273

272274
if (this.shouldAnimate()) {
273275
return this.animateComponent(props, animationWhitelist);
@@ -282,9 +284,10 @@ class VictoryAxis extends React.Component {
282284
this.renderLabel(props),
283285
...modifiedGridAndTicks
284286
];
287+
const container = React.cloneElement(props.containerComponent, userProps);
285288
return props.standalone
286-
? this.renderContainer(props.containerComponent, children)
287-
: React.cloneElement(props.groupComponent, {}, children);
289+
? this.renderContainer(container, children)
290+
: React.cloneElement(props.groupComponent, userProps, children);
288291
}
289292
}
290293

test/client/spec/victory-axis/victory-axis.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ import { _approximateTextSizeInternal } from "victory-core/es/victory-util/texts
1515

1616
describe("components/victory-axis", () => {
1717
describe("default component rendering", () => {
18+
it("accepts user props", () => {
19+
const wrapper = mount(
20+
<VictoryAxis data-testid="victory-axis" aria-label="Axis" />
21+
);
22+
const svgNode = wrapper.find("svg").at(0).getDOMNode();
23+
expect(svgNode.getAttribute("data-testid")).to.equal("victory-axis");
24+
expect(svgNode.getAttribute("aria-label")).to.equal("Axis");
25+
});
26+
1827
it("renders an svg with the correct width and height", () => {
1928
const wrapper = mount(<VictoryAxis />);
2029
const svg = wrapper.find("svg").at(0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { getChildComponents } from "victory-chart/lib/helper-methods";
2+
import React from "react";
3+
import { VictoryAxis } from "victory-axis";
4+
import { VictoryLine } from "victory-line";
5+
6+
describe("victory-chart/helpers-methods", () => {
7+
const getVictoryLine = (props) => React.createElement(VictoryLine, props);
8+
const getVictoryAxis = (props) => React.createElement(VictoryAxis, props);
9+
10+
describe("getChildComponents", () => {
11+
const defaultAxes = {
12+
independent: getVictoryAxis({}),
13+
dependent: getVictoryAxis({ dependentAxis: true })
14+
};
15+
16+
it("returns a pair of default axes when no children are given", () => {
17+
const children = [];
18+
const result = getChildComponents({ children }, defaultAxes);
19+
expect(result).toHaveLength(2);
20+
expect(result).toEqual([defaultAxes.independent, defaultAxes.dependent]);
21+
});
22+
23+
it("adds default axes when none of the children are axis components", () => {
24+
const line = getVictoryLine({});
25+
const children = [line];
26+
const result = getChildComponents({ children }, defaultAxes);
27+
expect(result).toHaveLength(3);
28+
expect(result).toContain(defaultAxes.independent);
29+
expect(result).toContain(defaultAxes.dependent);
30+
});
31+
32+
it("does not add default axes if axis any axis components exist in children", () => {
33+
const axis = getVictoryAxis({});
34+
const children = [axis];
35+
const result = getChildComponents({ children }, defaultAxes);
36+
expect(result).toHaveLength(1);
37+
expect(result[0].props).toEqual(axis.props);
38+
});
39+
});
40+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React from "react";
2+
import { VictoryChart } from "victory-chart";
3+
import { VictoryAxis } from "victory-axis";
4+
import { render, screen, fireEvent } from "@testing-library/react";
5+
6+
describe("components/victory-chart", () => {
7+
describe("default component rendering", () => {
8+
it("renders an svg with the correct width and height", () => {
9+
const { container } = render(<VictoryChart />);
10+
const svg = container.querySelector("svg");
11+
12+
expect(svg.getAttribute("style")).toContain("width: 100%; height: 100%");
13+
});
14+
15+
it("renders an svg with the correct viewBox", () => {
16+
const { container } = render(<VictoryChart />);
17+
const svg = container.querySelector("svg");
18+
const viewBoxValue = `0 0 ${450} ${300}`;
19+
20+
expect(svg.getAttribute("viewBox")).toEqual(viewBoxValue);
21+
});
22+
});
23+
24+
describe("axis rendering", () => {
25+
it("renders two axes by default", () => {
26+
const props = {
27+
defaultAxes: {
28+
independent: <VictoryAxis data-testid="axis" />,
29+
dependent: <VictoryAxis data-testid="axis" dependentAxis />
30+
}
31+
};
32+
render(<VictoryChart {...props} />);
33+
34+
const axes = screen.getAllByTestId("axis");
35+
36+
expect(axes).toHaveLength(2);
37+
});
38+
39+
it("renders one axis if one axis is given", () => {
40+
render(
41+
<VictoryChart>
42+
<VictoryAxis data-testid="axis" />
43+
</VictoryChart>
44+
);
45+
const axes = screen.getAllByTestId("axis");
46+
47+
expect(axes).toHaveLength(1);
48+
});
49+
50+
// TODO: Is this test useful? It's hard to test this with react testing library, which
51+
// may mean it should be removed.
52+
it("allows axis to control the crossAxis, and offset props", () => {
53+
render(
54+
<VictoryChart>
55+
<VictoryAxis
56+
data-testid="axis"
57+
crossAxis={false}
58+
offsetX={50}
59+
offsetY={50}
60+
data-cross-axis={(props) => props.crossAxis}
61+
data-offset-x={(props) => props.offsetX}
62+
data-offset-y={(props) => props.offsetY}
63+
/>
64+
</VictoryChart>
65+
);
66+
const axis = screen.getByTestId("axis");
67+
68+
expect(axis.getAttribute("data-cross-axis")).toEqual("false");
69+
expect(axis.getAttribute("data-offset-x")).toEqual("50");
70+
expect(axis.getAttribute("data-offset-y")).toEqual("50");
71+
});
72+
73+
it("accepts user props", () => {
74+
render(<VictoryChart data-testid="victory-chart" aria-label="Chart" />);
75+
76+
expect(screen.getByTestId("victory-chart")).toBeDefined();
77+
expect(screen.getByLabelText("Chart")).toBeDefined();
78+
});
79+
});
80+
81+
describe("event handling", () => {
82+
it("attaches an event to the parent svg", () => {
83+
const clickHandler = jest.fn();
84+
const { container } = render(
85+
<VictoryChart
86+
events={[
87+
{
88+
target: "parent",
89+
eventHandlers: { onClick: clickHandler }
90+
}
91+
]}
92+
/>
93+
);
94+
const svg = container.querySelector("svg");
95+
96+
fireEvent.click(svg);
97+
98+
expect(clickHandler).toHaveBeenCalled();
99+
});
100+
});
101+
});

0 commit comments

Comments
 (0)