@terreno/ui 57.8.0 → 57.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/AreaChart.d.ts +1 -1
- package/dist/AreaChart.d.ts.map +1 -1
- package/dist/AreaChart.js +70 -22
- package/dist/AreaChart.js.map +1 -1
- package/dist/BarChart.d.ts.map +1 -1
- package/dist/BarChart.js +55 -13
- package/dist/BarChart.js.map +1 -1
- package/dist/Button.d.ts.map +1 -1
- package/dist/Button.js +19 -4
- package/dist/Button.js.map +1 -1
- package/dist/ChartCard.d.ts +5 -0
- package/dist/ChartCard.d.ts.map +1 -0
- package/dist/ChartCard.js +18 -0
- package/dist/ChartCard.js.map +1 -0
- package/dist/Common.d.ts +147 -6
- package/dist/Common.d.ts.map +1 -1
- package/dist/Common.js.map +1 -1
- package/dist/DashboardGrid.d.ts +6 -2
- package/dist/DashboardGrid.d.ts.map +1 -1
- package/dist/DashboardGrid.js +43 -10
- package/dist/DashboardGrid.js.map +1 -1
- package/dist/DateTimeField.d.ts.map +1 -1
- package/dist/DateTimeField.js +90 -17
- package/dist/DateTimeField.js.map +1 -1
- package/dist/DonutChart.d.ts.map +1 -1
- package/dist/DonutChart.js +20 -10
- package/dist/DonutChart.js.map +1 -1
- package/dist/DropdownMenuItem.d.ts +16 -0
- package/dist/DropdownMenuItem.d.ts.map +1 -0
- package/dist/DropdownMenuItem.js +11 -0
- package/dist/DropdownMenuItem.js.map +1 -0
- package/dist/FilePickerButton.d.ts +15 -1
- package/dist/FilePickerButton.d.ts.map +1 -1
- package/dist/FilePickerButton.js +90 -13
- package/dist/FilePickerButton.js.map +1 -1
- package/dist/GPTChat.d.ts +57 -1
- package/dist/GPTChat.d.ts.map +1 -1
- package/dist/GPTChat.js +301 -20
- package/dist/GPTChat.js.map +1 -1
- package/dist/HtmlFrame.d.ts +11 -0
- package/dist/HtmlFrame.d.ts.map +1 -0
- package/dist/HtmlFrame.js +29 -0
- package/dist/HtmlFrame.js.map +1 -0
- package/dist/LineChart.d.ts +1 -1
- package/dist/LineChart.d.ts.map +1 -1
- package/dist/LineChart.js +62 -21
- package/dist/LineChart.js.map +1 -1
- package/dist/Scorecard.d.ts +5 -0
- package/dist/Scorecard.d.ts.map +1 -0
- package/dist/Scorecard.js +11 -0
- package/dist/Scorecard.js.map +1 -0
- package/dist/SparklineChart.d.ts +5 -0
- package/dist/SparklineChart.d.ts.map +1 -0
- package/dist/SparklineChart.js +35 -0
- package/dist/SparklineChart.js.map +1 -0
- package/dist/Text.d.ts +1 -1
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +2 -2
- package/dist/Text.js.map +1 -1
- package/dist/TextField.d.ts.map +1 -1
- package/dist/TextField.js +12 -4
- package/dist/TextField.js.map +1 -1
- package/dist/ToastNotifications.d.ts.map +1 -1
- package/dist/ToastNotifications.js +29 -6
- package/dist/ToastNotifications.js.map +1 -1
- package/dist/WebDropdownMenu.d.ts +2 -1
- package/dist/WebDropdownMenu.d.ts.map +1 -1
- package/dist/WebDropdownMenu.js +2 -6
- package/dist/WebDropdownMenu.js.map +1 -1
- package/dist/asks/AskCard.d.ts +34 -0
- package/dist/asks/AskCard.d.ts.map +1 -0
- package/dist/asks/AskCard.js +91 -0
- package/dist/asks/AskCard.js.map +1 -0
- package/dist/asks/AskChoice.d.ts +11 -0
- package/dist/asks/AskChoice.d.ts.map +1 -0
- package/dist/asks/AskChoice.js +146 -0
- package/dist/asks/AskChoice.js.map +1 -0
- package/dist/asks/AskConfirm.d.ts +15 -0
- package/dist/asks/AskConfirm.d.ts.map +1 -0
- package/dist/asks/AskConfirm.js +17 -0
- package/dist/asks/AskConfirm.js.map +1 -0
- package/dist/asks/AskFiles.d.ts +18 -0
- package/dist/asks/AskFiles.d.ts.map +1 -0
- package/dist/asks/AskFiles.js +149 -0
- package/dist/asks/AskFiles.js.map +1 -0
- package/dist/asks/AskForm.d.ts +27 -0
- package/dist/asks/AskForm.d.ts.map +1 -0
- package/dist/asks/AskForm.js +146 -0
- package/dist/asks/AskForm.js.map +1 -0
- package/dist/asks/AskMarkdown.d.ts +25 -0
- package/dist/asks/AskMarkdown.d.ts.map +1 -0
- package/dist/asks/AskMarkdown.js +73 -0
- package/dist/asks/AskMarkdown.js.map +1 -0
- package/dist/asks/SimpleAskCard.d.ts +23 -0
- package/dist/asks/SimpleAskCard.d.ts.map +1 -0
- package/dist/asks/SimpleAskCard.js +32 -0
- package/dist/asks/SimpleAskCard.js.map +1 -0
- package/dist/asks/askControls.d.ts +33 -0
- package/dist/asks/askControls.d.ts.map +1 -0
- package/dist/asks/askControls.js +30 -0
- package/dist/asks/askControls.js.map +1 -0
- package/dist/asks/askFileRefs.d.ts +26 -0
- package/dist/asks/askFileRefs.d.ts.map +1 -0
- package/dist/asks/askFileRefs.js +75 -0
- package/dist/asks/askFileRefs.js.map +1 -0
- package/dist/asks/askFormDraft.d.ts +60 -0
- package/dist/asks/askFormDraft.d.ts.map +1 -0
- package/dist/asks/askFormDraft.js +224 -0
- package/dist/asks/askFormDraft.js.map +1 -0
- package/dist/asks/askSummary.d.ts +10 -0
- package/dist/asks/askSummary.d.ts.map +1 -0
- package/dist/asks/askSummary.js +138 -0
- package/dist/asks/askSummary.js.map +1 -0
- package/dist/asks/askTypes.d.ts +21 -0
- package/dist/asks/askTypes.d.ts.map +1 -0
- package/dist/asks/askTypes.js +2 -0
- package/dist/asks/askTypes.js.map +1 -0
- package/dist/asks/simpleCardButtonVariants.d.ts +5 -0
- package/dist/asks/simpleCardButtonVariants.d.ts.map +1 -0
- package/dist/asks/simpleCardButtonVariants.js +8 -0
- package/dist/asks/simpleCardButtonVariants.js.map +1 -0
- package/dist/blocks/BlocksError.d.ts +8 -0
- package/dist/blocks/BlocksError.d.ts.map +1 -0
- package/dist/blocks/BlocksError.js +14 -0
- package/dist/blocks/BlocksError.js.map +1 -0
- package/dist/blocks/BlocksView.d.ts +10 -0
- package/dist/blocks/BlocksView.d.ts.map +1 -0
- package/dist/blocks/BlocksView.js +90 -0
- package/dist/blocks/BlocksView.js.map +1 -0
- package/dist/blocks/blockRenderers.d.ts +25 -0
- package/dist/blocks/blockRenderers.d.ts.map +1 -0
- package/dist/blocks/blockRenderers.js +136 -0
- package/dist/blocks/blockRenderers.js.map +1 -0
- package/dist/blocks/datasetToPoints.d.ts +22 -0
- package/dist/blocks/datasetToPoints.d.ts.map +1 -0
- package/dist/blocks/datasetToPoints.js +28 -0
- package/dist/blocks/datasetToPoints.js.map +1 -0
- package/dist/blocks/useBlockSelections.d.ts +6 -0
- package/dist/blocks/useBlockSelections.d.ts.map +1 -0
- package/dist/blocks/useBlockSelections.js +10 -0
- package/dist/blocks/useBlockSelections.js.map +1 -0
- package/dist/blocks/useResolvedDatasets.d.ts +18 -0
- package/dist/blocks/useResolvedDatasets.d.ts.map +1 -0
- package/dist/blocks/useResolvedDatasets.js +59 -0
- package/dist/blocks/useResolvedDatasets.js.map +1 -0
- package/dist/blocks/useResolvedImages.d.ts +15 -0
- package/dist/blocks/useResolvedImages.d.ts.map +1 -0
- package/dist/blocks/useResolvedImages.js +66 -0
- package/dist/blocks/useResolvedImages.js.map +1 -0
- package/dist/charts/ChartFacadeContainer.d.ts +10 -0
- package/dist/charts/ChartFacadeContainer.d.ts.map +1 -0
- package/dist/charts/ChartFacadeContainer.js +10 -0
- package/dist/charts/ChartFacadeContainer.js.map +1 -0
- package/dist/charts/ChartFrame.d.ts +4 -0
- package/dist/charts/ChartFrame.d.ts.map +1 -1
- package/dist/charts/ChartFrame.js +4 -2
- package/dist/charts/ChartFrame.js.map +1 -1
- package/dist/charts/layout.d.ts +9 -2
- package/dist/charts/layout.d.ts.map +1 -1
- package/dist/charts/layout.js +28 -6
- package/dist/charts/layout.js.map +1 -1
- package/dist/charts/scales.d.ts +3 -1
- package/dist/charts/scales.d.ts.map +1 -1
- package/dist/charts/scales.js +7 -7
- package/dist/charts/scales.js.map +1 -1
- package/dist/charts/series.d.ts +15 -0
- package/dist/charts/series.d.ts.map +1 -0
- package/dist/charts/series.js +19 -0
- package/dist/charts/series.js.map +1 -0
- package/dist/charts/types/chartTypes.d.ts +7 -0
- package/dist/charts/types/chartTypes.d.ts.map +1 -1
- package/dist/dashboardGridLayout.d.ts +6 -0
- package/dist/dashboardGridLayout.d.ts.map +1 -1
- package/dist/dashboardGridLayout.js +19 -3
- package/dist/dashboardGridLayout.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts +15 -0
- package/dist/lazyBoundaries/heavyOptionalExports.d.ts.map +1 -1
- package/dist/lazyBoundaries/heavyOptionalExports.js +10 -0
- package/dist/lazyBoundaries/heavyOptionalExports.js.map +1 -1
- package/dist/resolveDocumentBodyPortalTarget.d.ts +7 -0
- package/dist/resolveDocumentBodyPortalTarget.d.ts.map +1 -0
- package/dist/resolveDocumentBodyPortalTarget.js +12 -0
- package/dist/resolveDocumentBodyPortalTarget.js.map +1 -0
- package/package.json +2 -1
- package/src/AIRequestExplorer.test.tsx +2 -2
- package/src/AreaChart.test.tsx +81 -0
- package/src/AreaChart.tsx +171 -62
- package/src/BarChart.test.tsx +119 -0
- package/src/BarChart.tsx +94 -20
- package/src/Button.test.tsx +72 -0
- package/src/Button.tsx +34 -4
- package/src/ChartCard.test.tsx +79 -0
- package/src/ChartCard.tsx +80 -0
- package/src/Common.ts +136 -2
- package/src/DashboardGrid.test.tsx +98 -1
- package/src/DashboardGrid.tsx +71 -20
- package/src/DateTimeField.test.tsx +185 -2
- package/src/DateTimeField.tsx +106 -16
- package/src/DonutChart.test.tsx +78 -10
- package/src/DonutChart.tsx +87 -14
- package/src/DropdownMenuItem.tsx +48 -0
- package/src/FilePickerButton.test.tsx +116 -9
- package/src/FilePickerButton.tsx +177 -23
- package/src/GPTChat.test.tsx +891 -8
- package/src/GPTChat.tsx +713 -45
- package/src/HtmlFrame.tsx +60 -0
- package/src/LineChart.test.tsx +109 -10
- package/src/LineChart.tsx +154 -58
- package/src/Scorecard.test.tsx +65 -0
- package/src/Scorecard.tsx +45 -0
- package/src/SparklineChart.test.tsx +69 -0
- package/src/SparklineChart.tsx +85 -0
- package/src/Text.test.tsx +22 -0
- package/src/Text.tsx +10 -1
- package/src/TextField.test.tsx +16 -0
- package/src/TextField.tsx +13 -3
- package/src/ToastNotifications.tsx +33 -6
- package/src/ToastNotifications.webModal.test.tsx +285 -0
- package/src/WebDropdownMenu.tsx +3 -7
- package/src/asks/AskCard.test.tsx +1428 -0
- package/src/asks/AskCard.tsx +240 -0
- package/src/asks/AskChoice.tsx +368 -0
- package/src/asks/AskConfirm.tsx +42 -0
- package/src/asks/AskFiles.test.tsx +347 -0
- package/src/asks/AskFiles.tsx +246 -0
- package/src/asks/AskForm.tsx +334 -0
- package/src/asks/AskMarkdown.tsx +176 -0
- package/src/asks/SimpleAskCard.test.tsx +253 -0
- package/src/asks/SimpleAskCard.tsx +82 -0
- package/src/asks/askControls.tsx +98 -0
- package/src/asks/askFileRefs.test.ts +155 -0
- package/src/asks/askFileRefs.ts +98 -0
- package/src/asks/askFormDraft.test.ts +347 -0
- package/src/asks/askFormDraft.ts +309 -0
- package/src/asks/askSummary.test.ts +255 -0
- package/src/asks/askSummary.ts +160 -0
- package/src/asks/askTypes.ts +25 -0
- package/src/asks/asksUseTerrenoComponents.test.ts +55 -0
- package/src/asks/simpleCardButtonVariants.ts +14 -0
- package/src/blocks/BlocksError.tsx +28 -0
- package/src/blocks/BlocksView.test.tsx +607 -0
- package/src/blocks/BlocksView.tsx +125 -0
- package/src/blocks/blockRenderers.tsx +288 -0
- package/src/blocks/datasetToPoints.test.ts +31 -0
- package/src/blocks/datasetToPoints.ts +48 -0
- package/src/blocks/useBlockSelections.ts +13 -0
- package/src/blocks/useResolvedDatasets.ts +78 -0
- package/src/blocks/useResolvedImages.ts +81 -0
- package/src/bunSetup.ts +1 -0
- package/src/charts/ChartFacadeContainer.tsx +35 -0
- package/src/charts/ChartFrame.tsx +39 -1
- package/src/charts/layout.test.ts +22 -1
- package/src/charts/layout.ts +37 -4
- package/src/charts/scales.test.ts +33 -0
- package/src/charts/scales.ts +10 -6
- package/src/charts/series.ts +43 -0
- package/src/charts/types/chartTypes.ts +8 -0
- package/src/dashboardGridLayout.test.ts +59 -1
- package/src/dashboardGridLayout.ts +30 -3
- package/src/index.tsx +13 -0
- package/src/isolated/HtmlFrame.isolated.tsx +54 -0
- package/src/lazyBoundaries/heavyOptionalExports.test.tsx +2 -0
- package/src/lazyBoundaries/heavyOptionalExports.tsx +35 -0
- package/src/resolveDocumentBodyPortalTarget.ts +11 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import React, {useRef} from "react";
|
|
2
|
+
import {Platform} from "react-native";
|
|
3
|
+
import WebView from "react-native-webview";
|
|
4
|
+
|
|
5
|
+
import {Box} from "./Box";
|
|
6
|
+
import type {HtmlFrameProps} from "./Common";
|
|
7
|
+
|
|
8
|
+
export type {HtmlFrameProps} from "./Common";
|
|
9
|
+
|
|
10
|
+
const HTML_FRAME_HEIGHTS = {lg: 640, md: 400, sm: 240} as const;
|
|
11
|
+
|
|
12
|
+
const CSP =
|
|
13
|
+
"default-src 'none'; img-src data:; style-src 'unsafe-inline'; font-src data:; form-action 'none'; base-uri 'none'";
|
|
14
|
+
|
|
15
|
+
/** The document a frame loads. The CSP meta tag is first so it applies to the preview. */
|
|
16
|
+
export const htmlFrameSrcDoc = (html: string): string =>
|
|
17
|
+
`<meta http-equiv="Content-Security-Policy" content="${CSP}">${html}`;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sandboxed preview of agent HTML. Web uses an iframe with an empty sandbox.
|
|
21
|
+
* Native uses a WebView with JavaScript off and navigation blocked after the first load.
|
|
22
|
+
*/
|
|
23
|
+
export const HtmlFrame: React.FC<HtmlFrameProps> = ({height = "md", html, title}) => {
|
|
24
|
+
const srcDoc = htmlFrameSrcDoc(html);
|
|
25
|
+
const pixels = HTML_FRAME_HEIGHTS[height];
|
|
26
|
+
const didLoad = useRef(false);
|
|
27
|
+
|
|
28
|
+
if (Platform.OS === "web") {
|
|
29
|
+
return (
|
|
30
|
+
<Box height={pixels} testID="html-frame-web" width="100%">
|
|
31
|
+
<iframe
|
|
32
|
+
referrerPolicy="no-referrer"
|
|
33
|
+
sandbox=""
|
|
34
|
+
srcDoc={srcDoc}
|
|
35
|
+
style={{border: 0, height: "100%", width: "100%"}}
|
|
36
|
+
title={title ?? "Agent-generated preview"}
|
|
37
|
+
/>
|
|
38
|
+
</Box>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<Box height={pixels} testID="html-frame-native" width="100%">
|
|
44
|
+
<WebView
|
|
45
|
+
dataDetectorTypes="none"
|
|
46
|
+
incognito
|
|
47
|
+
javaScriptEnabled={false}
|
|
48
|
+
onShouldStartLoadWithRequest={() => {
|
|
49
|
+
if (didLoad.current) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
didLoad.current = true;
|
|
53
|
+
return true;
|
|
54
|
+
}}
|
|
55
|
+
source={{html: srcDoc}}
|
|
56
|
+
style={{flex: 1}}
|
|
57
|
+
/>
|
|
58
|
+
</Box>
|
|
59
|
+
);
|
|
60
|
+
};
|
package/src/LineChart.test.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {describe, expect, it
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
2
|
import {act, fireEvent} from "@testing-library/react-native";
|
|
3
|
-
import {
|
|
3
|
+
import {assert} from "chai";
|
|
4
4
|
|
|
5
5
|
import {LineChart} from "./LineChart";
|
|
6
6
|
import {renderWithTheme} from "./test-utils";
|
|
@@ -11,7 +11,110 @@ const POINTS = [
|
|
|
11
11
|
{label: "C", value: 100},
|
|
12
12
|
];
|
|
13
13
|
|
|
14
|
+
const SERIES = [
|
|
15
|
+
{data: POINTS, id: "rank", label: "Search lost IS (rank)"},
|
|
16
|
+
{
|
|
17
|
+
data: POINTS.map((point) => ({...point, value: point.value / 2})),
|
|
18
|
+
id: "share",
|
|
19
|
+
label: "Search impr. share",
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
data: POINTS.map((point) => ({...point, value: point.value / 4})),
|
|
23
|
+
id: "budget",
|
|
24
|
+
label: "Search lost IS (budget)",
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
|
|
14
28
|
describe("LineChart", () => {
|
|
29
|
+
it("applies explicit tick rotation and chart-card header shortcuts", async (): Promise<void> => {
|
|
30
|
+
let pressCount = 0;
|
|
31
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
32
|
+
<LineChart
|
|
33
|
+
data={POINTS}
|
|
34
|
+
onPeriodPress={(): void => {
|
|
35
|
+
pressCount += 1;
|
|
36
|
+
}}
|
|
37
|
+
periodLabel="Last 30 days"
|
|
38
|
+
testID="chart"
|
|
39
|
+
title="Search share"
|
|
40
|
+
xTickPolicy="rotate"
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
const tickStyle = getByTestId("chart.xtick.0").props.style;
|
|
44
|
+
const tickStyles = Array.isArray(tickStyle) ? tickStyle : [tickStyle];
|
|
45
|
+
|
|
46
|
+
assert.isTrue(tickStyles.some((style) => Array.isArray(style?.transform)));
|
|
47
|
+
assert.exists(getByText("Search share"));
|
|
48
|
+
assert.exists(getByText("Last 30 days"));
|
|
49
|
+
fireEvent.press(getByTestId("chart.card.period-clickable"));
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
51
|
+
assert.equal(pressCount, 1);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("renders one path and legend item per named series", () => {
|
|
55
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
56
|
+
<LineChart data={[]} series={SERIES} testID="chart" />
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
for (let index = 0; index < SERIES.length; index += 1) {
|
|
60
|
+
assert.exists(getByTestId(`chart.series.${index}.path`));
|
|
61
|
+
assert.exists(getByTestId(`chart.series.${index}.marker.0`));
|
|
62
|
+
assert.exists(getByTestId(`chart.legend.${index}`));
|
|
63
|
+
assert.exists(getByTestId(`chart.legend.${index}.swatch`));
|
|
64
|
+
const legendStyle = getByTestId(`chart.legend.${index}.item`).props.style;
|
|
65
|
+
const legendStyles = Array.isArray(legendStyle) ? legendStyle : [legendStyle];
|
|
66
|
+
assert.isTrue(
|
|
67
|
+
legendStyles.some(
|
|
68
|
+
(style: {flexGrow?: number; minWidth?: number} | undefined) =>
|
|
69
|
+
style?.flexGrow === 1 && style.minWidth === 0
|
|
70
|
+
)
|
|
71
|
+
);
|
|
72
|
+
assert.exists(getByText(SERIES[index]?.label ?? ""));
|
|
73
|
+
}
|
|
74
|
+
assert.isBelow(
|
|
75
|
+
getByTestId("chart.series.0.marker.2").props.cy,
|
|
76
|
+
getByTestId("chart.series.2.marker.2").props.cy
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("identifies the active series in a multi-series tooltip", async (): Promise<void> => {
|
|
81
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
82
|
+
<LineChart data={[]} series={SERIES} testID="chart" />
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
await act(async () => {
|
|
86
|
+
fireEvent.press(getByTestId("chart.series.1.point.1-clickable"));
|
|
87
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
assert.exists(getByText("Search impr. share — B: 25"));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("renders a dotted comparison path on the shared scale", () => {
|
|
94
|
+
const {getByTestId} = renderWithTheme(
|
|
95
|
+
<LineChart comparisonData={POINTS} data={POINTS} testID="chart" />
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
assert.isString(getByTestId("chart.comparison").props.strokeDasharray);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("renders comparison-only data instead of the empty state", () => {
|
|
102
|
+
const {getByTestId, queryByText} = renderWithTheme(
|
|
103
|
+
<LineChart comparisonData={POINTS} data={[]} testID="chart" />
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
assert.exists(getByTestId("chart.comparison"));
|
|
107
|
+
assert.notExists(queryByText("No data"));
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("shows the empty state when every named series is empty", () => {
|
|
111
|
+
const {getByText} = renderWithTheme(
|
|
112
|
+
<LineChart data={POINTS} series={[{data: [], id: "empty", label: "Empty"}]} />
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
assert.exists(getByText("No data"));
|
|
116
|
+
});
|
|
117
|
+
|
|
15
118
|
it("renders one mark testID per point", () => {
|
|
16
119
|
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
17
120
|
<LineChart data={POINTS} testID="chart" />
|
|
@@ -150,17 +253,13 @@ describe("LineChart", () => {
|
|
|
150
253
|
);
|
|
151
254
|
});
|
|
152
255
|
|
|
153
|
-
it("does not open URLs from x-axis labels",
|
|
154
|
-
const openURLSpy = spyOn(Linking, "openURL").mockImplementation(() => Promise.resolve(true));
|
|
256
|
+
it("does not open URLs from x-axis labels", () => {
|
|
155
257
|
const {getByText} = renderWithTheme(
|
|
156
258
|
<LineChart data={[{label: "https://evil.example", value: 10}]} />
|
|
157
259
|
);
|
|
158
260
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
expect(openURLSpy).not.toHaveBeenCalled();
|
|
164
|
-
openURLSpy.mockRestore();
|
|
261
|
+
// The suite shares one Linking mock, so a spy on openURL also records calls
|
|
262
|
+
// from other files. A label is inert when it has no press handler.
|
|
263
|
+
expect(getByText("https://evil.example").props.onPress).toBeUndefined();
|
|
165
264
|
});
|
|
166
265
|
});
|
package/src/LineChart.tsx
CHANGED
|
@@ -4,17 +4,21 @@ import {Circle, Path, Svg, Line as SvgLine} from "react-native-svg";
|
|
|
4
4
|
|
|
5
5
|
import {Box} from "./Box";
|
|
6
6
|
import type {LayoutChangeEvent, LineChartProps} from "./Common";
|
|
7
|
+
import {ChartFacadeContainer} from "./charts/ChartFacadeContainer";
|
|
7
8
|
import {ChartFrame} from "./charts/ChartFrame";
|
|
8
9
|
import {
|
|
10
|
+
CHART_ROTATED_X_AXIS_HEIGHT,
|
|
9
11
|
CHART_X_AXIS_HEIGHT,
|
|
10
12
|
getChartAxisWidth,
|
|
11
13
|
getChartPlot,
|
|
12
14
|
getPlotHeight,
|
|
13
15
|
getXTickStyle,
|
|
14
16
|
getYTickStyle,
|
|
17
|
+
shouldRotateChartXTicks,
|
|
15
18
|
} from "./charts/layout";
|
|
16
19
|
import {getLinePath} from "./charts/paths";
|
|
17
20
|
import {createCartesianScales, getYTickValues} from "./charts/scales";
|
|
21
|
+
import {getAllSeriesPoints, getAxisPoints, resolveChartSeries} from "./charts/series";
|
|
18
22
|
import {getChartPaint} from "./charts/theme";
|
|
19
23
|
import type {ChartPoint} from "./charts/types/chartTypes";
|
|
20
24
|
import {Text} from "./Text";
|
|
@@ -28,27 +32,41 @@ const MARK_HIT_SIZE = 24;
|
|
|
28
32
|
const formatChartTooltip = ({
|
|
29
33
|
formatValue,
|
|
30
34
|
point,
|
|
35
|
+
seriesLabel,
|
|
31
36
|
}: {
|
|
32
37
|
formatValue: (value: number) => string;
|
|
33
38
|
point: ChartPoint;
|
|
39
|
+
seriesLabel?: string;
|
|
34
40
|
}): string => {
|
|
35
|
-
|
|
41
|
+
const prefix = seriesLabel ? `${seriesLabel} — ` : "";
|
|
42
|
+
return `${prefix}${point.label}: ${formatValue(point.value)}`;
|
|
36
43
|
};
|
|
37
44
|
|
|
45
|
+
interface ActivePoint {
|
|
46
|
+
pointIndex: number;
|
|
47
|
+
seriesIndex: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
38
50
|
export const LineChart: FC<LineChartProps> = ({
|
|
39
51
|
accessibilityLabel,
|
|
52
|
+
comparisonData = [],
|
|
40
53
|
data,
|
|
41
54
|
emptyText = "No data",
|
|
42
55
|
formatValue = String,
|
|
43
56
|
height = DEFAULT_HEIGHT,
|
|
44
57
|
legendLabel,
|
|
45
58
|
loading = false,
|
|
59
|
+
onPeriodPress,
|
|
60
|
+
periodLabel,
|
|
61
|
+
series,
|
|
46
62
|
testID,
|
|
63
|
+
title,
|
|
64
|
+
xTickPolicy = "auto",
|
|
47
65
|
}) => {
|
|
48
66
|
const {theme} = useTheme();
|
|
49
67
|
const paint = getChartPaint(theme);
|
|
50
68
|
const [chartWidth, setChartWidth] = useState(DEFAULT_WIDTH);
|
|
51
|
-
const [
|
|
69
|
+
const [activePoint, setActivePoint] = useState<ActivePoint | undefined>(undefined);
|
|
52
70
|
|
|
53
71
|
const handleLayout = useCallback((event: LayoutChangeEvent): void => {
|
|
54
72
|
const nextWidth = event.nativeEvent.layout.width;
|
|
@@ -57,29 +75,59 @@ export const LineChart: FC<LineChartProps> = ({
|
|
|
57
75
|
}
|
|
58
76
|
}, []);
|
|
59
77
|
|
|
60
|
-
const handleMarkPress = useCallback((pointIndex: number): void => {
|
|
61
|
-
|
|
78
|
+
const handleMarkPress = useCallback((seriesIndex: number, pointIndex: number): void => {
|
|
79
|
+
setActivePoint({pointIndex, seriesIndex});
|
|
62
80
|
}, []);
|
|
63
81
|
|
|
82
|
+
const resolvedSeries = resolveChartSeries({data, legendLabel, series});
|
|
83
|
+
const allPoints = getAllSeriesPoints({comparisonData, series: resolvedSeries});
|
|
84
|
+
const axisPoints = getAxisPoints({comparisonData, series: resolvedSeries});
|
|
85
|
+
const coloredSeries = resolvedSeries.map((entry, index) => ({
|
|
86
|
+
...entry,
|
|
87
|
+
color: entry.color ?? paint.slices[index % paint.slices.length] ?? paint.series,
|
|
88
|
+
}));
|
|
64
89
|
const axisWidth = getChartAxisWidth(chartWidth);
|
|
65
|
-
const
|
|
90
|
+
const legendItems =
|
|
91
|
+
series && series.length > 0
|
|
92
|
+
? coloredSeries.map((entry) => ({color: entry.color, label: entry.label}))
|
|
93
|
+
: undefined;
|
|
94
|
+
const isXTickRotated = shouldRotateChartXTicks({
|
|
95
|
+
labelCount: axisPoints.length,
|
|
96
|
+
policy: xTickPolicy,
|
|
97
|
+
});
|
|
98
|
+
const xAxisHeight = isXTickRotated ? CHART_ROTATED_X_AXIS_HEIGHT : CHART_X_AXIS_HEIGHT;
|
|
99
|
+
const plotHeight = getPlotHeight({
|
|
100
|
+
hasLegend: Boolean(legendLabel) || Boolean(legendItems?.length),
|
|
101
|
+
height,
|
|
102
|
+
xAxisHeight,
|
|
103
|
+
});
|
|
66
104
|
const plot = getChartPlot({chartWidth, height: plotHeight});
|
|
67
105
|
const plotWidth = chartWidth - axisWidth;
|
|
68
|
-
const scales = createCartesianScales({
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
106
|
+
const scales = createCartesianScales({
|
|
107
|
+
plot,
|
|
108
|
+
points: allPoints,
|
|
109
|
+
xLabels: axisPoints.map((point) => point.label),
|
|
110
|
+
});
|
|
111
|
+
const comparisonPath = getLinePath({points: comparisonData, scales});
|
|
112
|
+
const yTicks = getYTickValues(allPoints);
|
|
113
|
+
const activeSeries = activePoint ? coloredSeries[activePoint.seriesIndex] : undefined;
|
|
114
|
+
const selectedPoint = activePoint ? activeSeries?.data[activePoint.pointIndex] : undefined;
|
|
115
|
+
const tooltipText = selectedPoint
|
|
116
|
+
? formatChartTooltip({
|
|
117
|
+
formatValue,
|
|
118
|
+
point: selectedPoint,
|
|
119
|
+
seriesLabel: series && series.length > 0 ? activeSeries?.label : undefined,
|
|
120
|
+
})
|
|
74
121
|
: undefined;
|
|
75
122
|
const summaryLabel =
|
|
76
123
|
accessibilityLabel ?? (legendLabel ? `${legendLabel} line chart` : "Line chart");
|
|
77
124
|
|
|
78
|
-
|
|
125
|
+
const chart = (
|
|
79
126
|
<ChartFrame
|
|
80
127
|
accessibilityLabel={summaryLabel}
|
|
81
128
|
emptyText={emptyText}
|
|
82
|
-
isEmpty={
|
|
129
|
+
isEmpty={allPoints.length === 0}
|
|
130
|
+
legendItems={legendItems}
|
|
83
131
|
legendLabel={legendLabel}
|
|
84
132
|
loading={loading}
|
|
85
133
|
testID={testID}
|
|
@@ -115,66 +163,103 @@ export const LineChart: FC<LineChartProps> = ({
|
|
|
115
163
|
/>
|
|
116
164
|
);
|
|
117
165
|
})}
|
|
118
|
-
{
|
|
119
|
-
<Path
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
r={4}
|
|
166
|
+
{comparisonPath ? (
|
|
167
|
+
<Path
|
|
168
|
+
d={comparisonPath}
|
|
169
|
+
fill="none"
|
|
170
|
+
opacity={0.6}
|
|
171
|
+
stroke={paint.series}
|
|
172
|
+
strokeDasharray="4 4"
|
|
173
|
+
strokeWidth={2}
|
|
174
|
+
testID={resolveTestID(testID, "comparison")}
|
|
128
175
|
/>
|
|
129
|
-
)
|
|
176
|
+
) : null}
|
|
177
|
+
{coloredSeries.map((entry, seriesIndex) => {
|
|
178
|
+
const linePath = getLinePath({points: entry.data, scales});
|
|
179
|
+
return linePath ? (
|
|
180
|
+
<Path
|
|
181
|
+
d={linePath}
|
|
182
|
+
fill="none"
|
|
183
|
+
key={`line-${entry.id}`}
|
|
184
|
+
stroke={entry.color}
|
|
185
|
+
strokeDasharray={entry.stroke === "dotted" ? "4 4" : undefined}
|
|
186
|
+
strokeWidth={2}
|
|
187
|
+
testID={resolveTestID(testID, `series.${seriesIndex}.path`)}
|
|
188
|
+
/>
|
|
189
|
+
) : null;
|
|
190
|
+
})}
|
|
191
|
+
{coloredSeries.flatMap((entry, seriesIndex) =>
|
|
192
|
+
entry.data.map((point, pointIndex) => (
|
|
193
|
+
<Circle
|
|
194
|
+
cx={scales.xCenter(point.label)}
|
|
195
|
+
cy={scales.y(point.value)}
|
|
196
|
+
fill={entry.color}
|
|
197
|
+
key={`dot-${entry.id}-${point.label}`}
|
|
198
|
+
r={4}
|
|
199
|
+
testID={resolveTestID(testID, `series.${seriesIndex}.marker.${pointIndex}`)}
|
|
200
|
+
/>
|
|
201
|
+
))
|
|
202
|
+
)}
|
|
130
203
|
</Svg>
|
|
131
|
-
{
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
204
|
+
{coloredSeries.flatMap((entry, seriesIndex) =>
|
|
205
|
+
entry.data.map((point, pointIndex) => {
|
|
206
|
+
const onPress = (): void => {
|
|
207
|
+
handleMarkPress(seriesIndex, pointIndex);
|
|
208
|
+
};
|
|
209
|
+
const markTestID =
|
|
210
|
+
series && series.length > 0
|
|
211
|
+
? `series.${seriesIndex}.point.${pointIndex}`
|
|
212
|
+
: `point.${pointIndex}`;
|
|
213
|
+
return (
|
|
214
|
+
<Box
|
|
215
|
+
accessibilityHint={`Show value for ${point.label}`}
|
|
216
|
+
accessibilityLabel={
|
|
217
|
+
series && series.length > 0
|
|
218
|
+
? `${entry.label} — ${point.label}: ${formatValue(point.value)}`
|
|
219
|
+
: `${point.label}: ${formatValue(point.value)}`
|
|
220
|
+
}
|
|
221
|
+
dangerouslySetInlineStyle={{
|
|
222
|
+
__style: {
|
|
223
|
+
height: MARK_HIT_SIZE,
|
|
224
|
+
left: scales.xCenter(point.label) - MARK_HIT_SIZE / 2,
|
|
225
|
+
position: "absolute",
|
|
226
|
+
top: scales.y(point.value) - MARK_HIT_SIZE / 2,
|
|
227
|
+
width: MARK_HIT_SIZE,
|
|
228
|
+
},
|
|
229
|
+
}}
|
|
230
|
+
key={`mark-${entry.id}-${point.label}`}
|
|
231
|
+
onClick={onPress}
|
|
232
|
+
onHoverStart={onPress}
|
|
233
|
+
testID={resolveTestID(testID, markTestID)}
|
|
234
|
+
/>
|
|
235
|
+
);
|
|
236
|
+
})
|
|
237
|
+
)}
|
|
155
238
|
</Box>
|
|
156
239
|
</Box>
|
|
157
240
|
<Box direction="row">
|
|
158
241
|
<Box width={axisWidth} />
|
|
159
|
-
<Box
|
|
160
|
-
|
|
161
|
-
height={CHART_X_AXIS_HEIGHT}
|
|
162
|
-
minWidth={0}
|
|
163
|
-
overflow="hidden"
|
|
164
|
-
position="relative"
|
|
165
|
-
>
|
|
166
|
-
{data.map((point, index) => (
|
|
242
|
+
<Box flex="grow" height={xAxisHeight} minWidth={0} overflow="hidden" position="relative">
|
|
243
|
+
{axisPoints.map((point, index) => (
|
|
167
244
|
<Box
|
|
168
245
|
dangerouslySetInlineStyle={{
|
|
169
246
|
__style: getXTickStyle({
|
|
170
247
|
bandwidth: scales.bandwidth,
|
|
248
|
+
isRotated: isXTickRotated,
|
|
171
249
|
xCenter: scales.xCenter(point.label),
|
|
172
250
|
}),
|
|
173
251
|
}}
|
|
174
252
|
key={`xtick-${point.label}`}
|
|
175
253
|
testID={resolveTestID(testID, `xtick.${index}`)}
|
|
176
254
|
>
|
|
177
|
-
<Text
|
|
255
|
+
<Text
|
|
256
|
+
align="center"
|
|
257
|
+
color="secondaryDark"
|
|
258
|
+
numberOfLines={1}
|
|
259
|
+
size="sm"
|
|
260
|
+
skipLinking
|
|
261
|
+
truncate={!isXTickRotated}
|
|
262
|
+
>
|
|
178
263
|
{point.label}
|
|
179
264
|
</Text>
|
|
180
265
|
</Box>
|
|
@@ -184,7 +269,18 @@ export const LineChart: FC<LineChartProps> = ({
|
|
|
184
269
|
</Box>
|
|
185
270
|
</ChartFrame>
|
|
186
271
|
);
|
|
272
|
+
|
|
273
|
+
return (
|
|
274
|
+
<ChartFacadeContainer
|
|
275
|
+
onPeriodPress={onPeriodPress}
|
|
276
|
+
periodLabel={periodLabel}
|
|
277
|
+
testID={testID}
|
|
278
|
+
title={title}
|
|
279
|
+
>
|
|
280
|
+
{chart}
|
|
281
|
+
</ChartFacadeContainer>
|
|
282
|
+
);
|
|
187
283
|
};
|
|
188
284
|
|
|
189
285
|
export type {LineChartProps} from "./Common";
|
|
190
|
-
export type {ChartPoint} from "./charts/types/chartTypes";
|
|
286
|
+
export type {ChartPoint, ChartSeries} from "./charts/types/chartTypes";
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
import {Scorecard as RootScorecard} from "./index";
|
|
4
|
+
import {Scorecard} from "./Scorecard";
|
|
5
|
+
import {renderWithTheme} from "./test-utils";
|
|
6
|
+
|
|
7
|
+
const CURRENT = [
|
|
8
|
+
{label: "Mon", value: 12},
|
|
9
|
+
{label: "Tue", value: 18},
|
|
10
|
+
{label: "Wed", value: 9},
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const COMPARISON = [
|
|
14
|
+
{label: "Mon", value: 10},
|
|
15
|
+
{label: "Tue", value: 11},
|
|
16
|
+
{label: "Wed", value: 15},
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
describe("Scorecard", () => {
|
|
20
|
+
it("renders a formatted metric with current and comparison sparkline paths", () => {
|
|
21
|
+
const {getByTestId, getByText} = renderWithTheme(
|
|
22
|
+
<Scorecard
|
|
23
|
+
comparisonData={COMPARISON}
|
|
24
|
+
formatValue={(value): string => `$${value}`}
|
|
25
|
+
sparklineData={CURRENT}
|
|
26
|
+
testID="cost-scorecard"
|
|
27
|
+
title="Cost"
|
|
28
|
+
value={569}
|
|
29
|
+
/>
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
assert.exists(getByText("Cost"));
|
|
33
|
+
assert.exists(getByText("$569"));
|
|
34
|
+
assert.exists(getByTestId("cost-scorecard.sparkline.current"));
|
|
35
|
+
assert.exists(getByTestId("cost-scorecard.sparkline.comparison"));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("renders a string metric without requiring sparkline data", () => {
|
|
39
|
+
const {getByText, queryByTestId} = renderWithTheme(
|
|
40
|
+
<Scorecard sparklineData={[]} testID="status-scorecard" title="Status" value="Healthy" />
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
assert.exists(getByText("Healthy"));
|
|
44
|
+
assert.notExists(queryByTestId("status-scorecard.sparkline"));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("uses default numeric formatting and omits comparison when it is not provided", () => {
|
|
48
|
+
const {getByText, getByTestId, queryByTestId} = renderWithTheme(
|
|
49
|
+
<Scorecard
|
|
50
|
+
sparklineData={CURRENT}
|
|
51
|
+
testID="conversion-scorecard"
|
|
52
|
+
title="Conversions"
|
|
53
|
+
value={7}
|
|
54
|
+
/>
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
assert.exists(getByText("7"));
|
|
58
|
+
assert.exists(getByTestId("conversion-scorecard.sparkline.current"));
|
|
59
|
+
assert.notExists(queryByTestId("conversion-scorecard.sparkline.comparison"));
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("is available from the package root lazy boundary", () => {
|
|
63
|
+
assert.isFunction(RootScorecard);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type {FC} from "react";
|
|
2
|
+
|
|
3
|
+
import {Box} from "./Box";
|
|
4
|
+
import {ChartCard} from "./ChartCard";
|
|
5
|
+
import type {ScorecardProps} from "./Common";
|
|
6
|
+
import {Heading} from "./Heading";
|
|
7
|
+
import {SparklineChart} from "./SparklineChart";
|
|
8
|
+
import {resolveTestID} from "./testing/resolveTestId";
|
|
9
|
+
|
|
10
|
+
export const Scorecard: FC<ScorecardProps> = ({
|
|
11
|
+
comparisonData,
|
|
12
|
+
formatValue = String,
|
|
13
|
+
onPeriodPress,
|
|
14
|
+
periodLabel,
|
|
15
|
+
sparklineData,
|
|
16
|
+
testID,
|
|
17
|
+
title,
|
|
18
|
+
value,
|
|
19
|
+
}) => {
|
|
20
|
+
const displayValue = typeof value === "number" ? formatValue(value) : value;
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<ChartCard
|
|
24
|
+
onPeriodPress={onPeriodPress}
|
|
25
|
+
periodLabel={periodLabel}
|
|
26
|
+
testID={testID}
|
|
27
|
+
title={title}
|
|
28
|
+
>
|
|
29
|
+
<Box gap={2} minWidth={0} width="100%">
|
|
30
|
+
<Heading size="xl" testID={resolveTestID(testID, "value")}>
|
|
31
|
+
{displayValue}
|
|
32
|
+
</Heading>
|
|
33
|
+
{sparklineData && sparklineData.length > 0 ? (
|
|
34
|
+
<SparklineChart
|
|
35
|
+
comparisonData={comparisonData}
|
|
36
|
+
data={sparklineData}
|
|
37
|
+
testID={resolveTestID(testID, "sparkline")}
|
|
38
|
+
/>
|
|
39
|
+
) : null}
|
|
40
|
+
</Box>
|
|
41
|
+
</ChartCard>
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type {ScorecardProps} from "./Common";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import {fireEvent} from "@testing-library/react-native";
|
|
3
|
+
import {assert} from "chai";
|
|
4
|
+
|
|
5
|
+
import {SparklineChart as RootSparklineChart} from "./index";
|
|
6
|
+
import {SparklineChart} from "./SparklineChart";
|
|
7
|
+
import {Text} from "./Text";
|
|
8
|
+
import {renderWithTheme} from "./test-utils";
|
|
9
|
+
|
|
10
|
+
const CURRENT = [
|
|
11
|
+
{label: "Mon", value: 12},
|
|
12
|
+
{label: "Tue", value: 18},
|
|
13
|
+
{label: "Wed", value: 9},
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
const COMPARISON = [
|
|
17
|
+
{label: "Mon", value: 10},
|
|
18
|
+
{label: "Tue", value: 11},
|
|
19
|
+
{label: "Wed", value: 15},
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const getPathXCoordinates = (path: string): number[] => {
|
|
23
|
+
return Array.from(path.matchAll(/[ML]([0-9.]+),/g), (match) => Number(match[1]));
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe("SparklineChart", () => {
|
|
27
|
+
it("renders current and dotted comparison paths without chart chrome", () => {
|
|
28
|
+
const {getByTestId, queryByTestId, UNSAFE_queryAllByType} = renderWithTheme(
|
|
29
|
+
<SparklineChart comparisonData={COMPARISON} data={CURRENT} testID="revenue-sparkline" />
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const current = getByTestId("revenue-sparkline.current");
|
|
33
|
+
const comparison = getByTestId("revenue-sparkline.comparison");
|
|
34
|
+
|
|
35
|
+
assert.isUndefined(current.props.strokeDasharray);
|
|
36
|
+
assert.isString(comparison.props.strokeDasharray);
|
|
37
|
+
assert.deepEqual(getPathXCoordinates(current.props.d), getPathXCoordinates(comparison.props.d));
|
|
38
|
+
assert.lengthOf(getPathXCoordinates(current.props.d), CURRENT.length);
|
|
39
|
+
assert.lengthOf(UNSAFE_queryAllByType(Text), 0);
|
|
40
|
+
assert.notExists(queryByTestId("revenue-sparkline.tooltip"));
|
|
41
|
+
assert.notExists(queryByTestId("revenue-sparkline.legend"));
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("renders an empty frame without throwing", () => {
|
|
45
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
46
|
+
<SparklineChart data={[]} testID="empty-sparkline" />
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
assert.exists(getByTestId("empty-sparkline"));
|
|
50
|
+
assert.notExists(queryByTestId("empty-sparkline.current"));
|
|
51
|
+
assert.notExists(queryByTestId("empty-sparkline.comparison"));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("is available from the package root lazy boundary", () => {
|
|
55
|
+
assert.isFunction(RootSparklineChart);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("redraws from the measured container width", () => {
|
|
59
|
+
const {getByTestId} = renderWithTheme(
|
|
60
|
+
<SparklineChart comparisonData={COMPARISON} data={CURRENT} testID="measured-sparkline" />
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
fireEvent(getByTestId("measured-sparkline"), "layout", {
|
|
64
|
+
nativeEvent: {layout: {height: 48, width: 80}},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
assert.exists(getByTestId("measured-sparkline.current"));
|
|
68
|
+
});
|
|
69
|
+
});
|