Skip to content

Commit e58d1cc

Browse files
authored
chore: Adds test page and unit test for dynamically hiding chart axes (#64)
1 parent a764511 commit e58d1cc

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { useState } from "react";
5+
import Highcharts from "highcharts";
6+
import { omit } from "lodash";
7+
8+
import SpaceBetween from "@cloudscape-design/components/space-between";
9+
import Toggle from "@cloudscape-design/components/toggle";
10+
11+
import CoreChart, { CoreChartProps } from "../../lib/components/internal-do-not-use/core-chart";
12+
import { dateFormatter } from "../common/formatters";
13+
import { useChartSettings } from "../common/page-settings";
14+
import { Page } from "../common/templates";
15+
import pseudoRandom from "../utils/pseudo-random";
16+
17+
function randomInt(min: number, max: number) {
18+
return min + Math.floor(pseudoRandom() * (max - min));
19+
}
20+
21+
const baseline = [
22+
{ x: 1600984800000, y: 58020 },
23+
{ x: 1600985700000, y: 102402 },
24+
{ x: 1600986600000, y: 104920 },
25+
{ x: 1600987500000, y: 94031 },
26+
{ x: 1600988400000, y: 125021 },
27+
{ x: 1600989300000, y: 159219 },
28+
{ x: 1600990200000, y: 193082 },
29+
{ x: 1600991100000, y: 162592 },
30+
{ x: 1600992000000, y: 274021 },
31+
{ x: 1600992900000, y: 264286 },
32+
{ x: 1600993800000, y: 289210 },
33+
{ x: 1600994700000, y: 256362 },
34+
{ x: 1600995600000, y: 257306 },
35+
{ x: 1600996500000, y: 186776 },
36+
{ x: 1600997400000, y: 294020 },
37+
];
38+
39+
const dataA = baseline.map(({ x, y }) => ({ x, y }));
40+
const dataB = baseline.map(({ x, y }) => ({ x, y: y + randomInt(-100000, 100000) }));
41+
const dataC = baseline.map(({ x, y }) => ({ x, y: y + randomInt(-150000, 50000) }));
42+
43+
const lineSeries: Highcharts.SeriesOptionsType[] = [
44+
{ name: "A", type: "line", data: dataA },
45+
{ name: "B", type: "line", data: dataB },
46+
{ name: "C", type: "line", data: dataC },
47+
];
48+
49+
const pieSeries: Highcharts.SeriesOptionsType[] = [
50+
{
51+
name: "Average",
52+
type: "pie",
53+
data: [
54+
{ name: "A", y: Math.round(dataA.reduce((acc, { y }) => acc + y, 0) / dataA.length) },
55+
{ name: "B", y: Math.round(dataB.reduce((acc, { y }) => acc + y, 0) / dataA.length) },
56+
{ name: "C", y: Math.round(dataC.reduce((acc, { y }) => acc + y, 0) / dataA.length) },
57+
],
58+
},
59+
];
60+
61+
export default function () {
62+
const { chartProps } = useChartSettings();
63+
const [chartType, setChartType] = useState<"line" | "pie">("line");
64+
const options: CoreChartProps.ChartOptions =
65+
chartType === "line"
66+
? {
67+
series: lineSeries,
68+
xAxis: [
69+
{
70+
type: "datetime",
71+
title: { text: "Time (UTC)" },
72+
valueFormatter: dateFormatter,
73+
},
74+
],
75+
yAxis: [{ title: { text: "Events" } }],
76+
}
77+
: { series: pieSeries, xAxis: [], yAxis: [] };
78+
return (
79+
<Page title="Dynamic chart type">
80+
<SpaceBetween size="m">
81+
<Toggle checked={chartType === "pie"} onChange={({ detail }) => setChartType(detail.checked ? "pie" : "line")}>
82+
Show average
83+
</Toggle>
84+
85+
<CoreChart
86+
{...omit(chartProps.cartesian, "ref")}
87+
highcharts={Highcharts}
88+
ariaLabel="Events chart"
89+
options={options}
90+
/>
91+
</SpaceBetween>
92+
</Page>
93+
);
94+
}

src/core/__tests__/chart-core-rendering.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,32 @@ describe("CoreChart: rendering", () => {
4545
clearChartHighlight: expect.any(Function),
4646
});
4747
});
48+
49+
// To hide the axes we pass them as empty arrays instead of undefined.
50+
// See: https://github.com/highcharts/highcharts/issues/23337.
51+
test("hides cartesian axes after re-rendering", () => {
52+
const { wrapper, rerender } = renderChart({
53+
highcharts,
54+
options: {
55+
title: { text: "Chart title" },
56+
series: [{ type: "line", name: "Site 1", data: [1, 2, 3] }],
57+
xAxis: [{ title: { text: "X" } }],
58+
yAxis: [{ title: { text: "Y" } }],
59+
},
60+
});
61+
expect(wrapper.findXAxisTitle()!.getElement()).toHaveTextContent("X");
62+
expect(wrapper.findYAxisTitle()!.getElement()).toHaveTextContent("Y");
63+
64+
rerender({
65+
highcharts,
66+
options: {
67+
title: { text: "Chart title" },
68+
series: [{ type: "line", name: "Site 1", data: [1, 2, 3] }],
69+
xAxis: [],
70+
yAxis: [],
71+
},
72+
});
73+
expect(wrapper.findXAxisTitle()).toBe(null);
74+
expect(wrapper.findYAxisTitle()).toBe(null);
75+
});
4876
});

0 commit comments

Comments
 (0)