Skip to content

Commit 0537eb1

Browse files
Merge pull request #2247 from FormidableLabs/feature/jest-candlestick-tests
Feature/jest candlestick tests
2 parents 9218fcd + 3c1a9c1 commit 0537eb1

File tree

4 files changed

+391
-1
lines changed

4 files changed

+391
-1
lines changed

test/jest/victory-bar/victory-bar.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ describe("components/victory-bar", () => {
243243
/>
244244
);
245245

246-
Array.from(container.querySelectorAll("path")).forEach((bar, index) => {
246+
container.querySelectorAll("path").forEach((bar, index) => {
247247
expect(parseInt(bar.getAttribute("tabindex"))).toEqual(index + 1);
248248
expect(bar.getAttribute("aria-label")).toEqual(`x: ${data[index].x}`);
249249
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from "react";
2+
import { render } from "@testing-library/react";
3+
import { Candle } from "victory-candlestick";
4+
import { VictoryContainer } from "victory-core";
5+
import * as d3Scale from "victory-vendor/d3-scale";
6+
7+
describe("victory-primitives/candle", () => {
8+
const baseProps = {
9+
data: [
10+
{ x: 1, open: 10, close: 30, high: 50, low: 5, eventKey: 0 },
11+
{ x: 2, open: 40, close: 80, high: 100, low: 10, eventKey: 1 }
12+
],
13+
datum: { x: 1, open: 10, close: 30, high: 50, low: 5, eventKey: 0 },
14+
scale: {
15+
x: d3Scale.scaleLinear(),
16+
y: d3Scale.scaleLinear()
17+
},
18+
candleWidth: 2,
19+
x: 5,
20+
high: 50,
21+
low: 5,
22+
close: 30,
23+
open: 10
24+
};
25+
26+
const renderCandle = (props = {}) =>
27+
render(
28+
<VictoryContainer>
29+
<Candle {...baseProps} {...props} />
30+
</VictoryContainer>
31+
);
32+
33+
it("should render a wick line", () => {
34+
const { container } = renderCandle();
35+
const wicks = container.querySelectorAll("line");
36+
const values = [
37+
{
38+
x1: 5,
39+
x2: 5,
40+
y1: 50,
41+
y2: 10
42+
},
43+
{
44+
x1: 5,
45+
x2: 5,
46+
y1: 30,
47+
y2: 5
48+
}
49+
];
50+
51+
wicks.forEach((wick, i) => {
52+
const [x1, x2, y1, y2] = ["x1", "x2", "y1", "y2"].map((prop) =>
53+
parseInt(wick.getAttribute(prop))
54+
);
55+
expect(values[i]).toMatchObject({ x1, x2, y1, y2 });
56+
});
57+
});
58+
59+
it("should render a candle rectangle", () => {
60+
const { container } = renderCandle();
61+
const rect = container.querySelector("rect");
62+
const [width, height, x, y] = ["width", "height", "x", "y"].map((prop) =>
63+
rect.getAttribute(prop)
64+
);
65+
66+
// width = style.width || 0.5 * (width - 2 * padding) / data.length;
67+
68+
expect(width).toEqual("2");
69+
expect(height).toEqual("20");
70+
// x = x - width / 2
71+
expect(x).toEqual("4");
72+
expect(y).toEqual("10");
73+
});
74+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* eslint max-nested-callbacks: 0 */
2+
import { range } from "lodash";
3+
import { fromJS } from "immutable";
4+
import { getData, getDomain } from "victory-candlestick/lib/helper-methods";
5+
6+
const immutableGetDataTest = {
7+
createData: (x) => fromJS(x),
8+
testLabel: "with immutable data"
9+
};
10+
const getDataTest = {
11+
createData: (x) => x,
12+
testLabel: "with js data"
13+
};
14+
15+
[getDataTest, immutableGetDataTest].forEach(({ createData, testLabel }) => {
16+
describe(`victory-candlestick/helper-methods ${testLabel}`, () => {
17+
describe("getData", () => {
18+
it("sorts data by sortKey", () => {
19+
const data = createData(
20+
range(5)
21+
.map((i) => ({ x: i, open: i, close: i, high: i, low: i }))
22+
.reverse()
23+
);
24+
25+
const dataResult = getData({
26+
data,
27+
x: "x",
28+
open: "open",
29+
close: "close",
30+
high: "high",
31+
low: "low",
32+
sortKey: "x"
33+
});
34+
35+
expect(dataResult.map((datum) => datum.x)).toEqual([0, 1, 2, 3, 4]);
36+
});
37+
});
38+
39+
describe("getDomain", () => {
40+
const dataSet = createData([
41+
{ x: 5, open: 10, close: 20, high: 25, low: 5 },
42+
{ x: 10, open: 15, close: 25, high: 30, low: 10 }
43+
]);
44+
45+
it("returns a domain array for the x axis", () => {
46+
const domainXResult = getDomain(
47+
{
48+
data: dataSet,
49+
x: "x",
50+
open: "open",
51+
close: "close",
52+
high: "high",
53+
low: "low"
54+
},
55+
"x"
56+
);
57+
expect(domainXResult).toEqual([5, 10]);
58+
});
59+
60+
it("returns a domain array for the y axis", () => {
61+
const domainYResult = getDomain(
62+
{
63+
data: dataSet,
64+
open: "open",
65+
close: "close",
66+
high: "high",
67+
low: "low"
68+
},
69+
"y"
70+
);
71+
expect(domainYResult).toEqual([5, 30]);
72+
});
73+
});
74+
});
75+
});

0 commit comments

Comments
 (0)