@terpjs/react-core 0.30.0 → 0.32.0
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 +35 -14
- package/package.json +5 -5
- package/src/DashboardPage.tsx +34 -0
- package/src/Field.test.tsx +26 -1
- package/src/Field.tsx +15 -5
- package/src/HubPage.tsx +7 -2
- package/src/ModuleNav.test.tsx +29 -1
- package/src/ModuleNav.tsx +5 -1
- package/src/Page.test.tsx +192 -0
- package/src/Page.tsx +141 -55
- package/src/PageActions.test.tsx +64 -3
- package/src/PageActions.tsx +40 -15
- package/src/PageSequence.tsx +114 -0
- package/src/Stat.test.tsx +376 -0
- package/src/Stat.tsx +316 -0
- package/src/Timeline.test.tsx +74 -0
- package/src/Timeline.tsx +71 -0
- package/src/admin/AdminHub.tsx +233 -78
- package/src/admin/AuditLogAdmin.tsx +3 -1
- package/src/admin/GroupCreate.tsx +3 -1
- package/src/admin/GroupDetail.tsx +72 -67
- package/src/admin/GroupsAdmin.tsx +3 -1
- package/src/admin/ModuleAccessPanel.tsx +13 -12
- package/src/admin/RecordHistory.tsx +88 -0
- package/src/admin/UserCreate.tsx +3 -1
- package/src/admin/UserDetail.tsx +8 -1
- package/src/admin/UsersAdmin.tsx +3 -0
- package/src/admin/admin.test.tsx +389 -19
- package/src/bootstrap.test.tsx +31 -7
- package/src/bootstrap.tsx +30 -4
- package/src/charts/BarChart.tsx +74 -0
- package/src/charts/ProportionBar.tsx +118 -0
- package/src/charts/StatusHistory.tsx +75 -0
- package/src/charts/TrendChart.tsx +232 -0
- package/src/charts/charts.test.tsx +409 -0
- package/src/charts/scale.ts +103 -0
- package/src/currentInView.ts +28 -0
- package/src/dataview/DataView.test.tsx +303 -1
- package/src/dataview/DataView.tsx +53 -3
- package/src/dataview/DataViewCardList.tsx +21 -13
- package/src/dataview/DataViewTable.tsx +5 -6
- package/src/dataview/DataViewToolbar.tsx +4 -1
- package/src/dataview/README.md +18 -0
- package/src/dataview/index.ts +1 -0
- package/src/dataview/internal.tsx +112 -5
- package/src/dataview/types.ts +39 -10
- package/src/errorMessages.test.tsx +17 -0
- package/src/errorMessages.tsx +18 -0
- package/src/format.ts +15 -0
- package/src/index.ts +24 -1
- package/src/layout.manifest.json +2 -9
- package/src/layout.manifest.test.ts +4 -2
- package/src/layout.test.tsx +74 -6
- package/src/layout.tsx +85 -25
- package/src/layoutContract.test.tsx +239 -1
- package/src/layoutContract.ts +131 -5
- package/src/layoutDeclaration.test.ts +31 -10
- package/src/layoutDeclaration.ts +9 -4
- package/src/locale.tsx +28 -3
- package/src/markers.test.ts +70 -2
- package/src/router.test.tsx +15 -3
- package/src/router.tsx +11 -2
- package/src/ssr.test.tsx +18 -0
- package/src/styles.test.ts +288 -190
- package/src/styles.ts +1379 -361
- package/src/theme.test.tsx +44 -18
- package/src/theme.themes.test.ts +22 -2
- package/src/theme.tsx +22 -14
- package/src/themes.ts +39 -4
- package/src/tokens.guard.test.ts +13 -15
- package/src/typography.tsx +5 -1
- package/src/ui/Alert.test.tsx +24 -1
- package/src/ui/Alert.tsx +10 -1
- package/src/ui/Card.tsx +5 -4
- package/src/ui/Tabs.test.tsx +44 -0
- package/src/ui/Tabs.tsx +5 -2
- package/src/ui/Tooltip.test.tsx +171 -2
- package/src/ui/Tooltip.tsx +144 -8
- package/src/ui/controlMessages.tsx +20 -7
- package/src/uiText.test.tsx +16 -0
- package/src/uiText.tsx +105 -7
- package/src/viewerTimeZone.ts +15 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, render, screen, within } from "@testing-library/react";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
+
|
|
6
|
+
// The pairings the contrast gate measures, read from the contract package that owns them.
|
|
7
|
+
import tokenPairs from "../../../contract/token-pairs.json";
|
|
8
|
+
import { LOCALE_EN, LOCALE_NL, LocaleProvider } from "../locale";
|
|
9
|
+
import { TERP_STYLES_CSS } from "../styles";
|
|
10
|
+
import { BarChart } from "./BarChart";
|
|
11
|
+
import { ProportionBar } from "./ProportionBar";
|
|
12
|
+
import { bandCentre, columnOf, niceStep, valueAxis, xOf, yOf } from "./scale";
|
|
13
|
+
import { StatusHistory } from "./StatusHistory";
|
|
14
|
+
import { TrendChart } from "./TrendChart";
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
cleanup();
|
|
18
|
+
window.localStorage.clear();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
function renderIn(locale: "nl" | "en", node: ReactNode) {
|
|
22
|
+
return render(
|
|
23
|
+
<LocaleProvider locales={{ nl: LOCALE_NL, en: LOCALE_EN }} defaultLocale={locale}>
|
|
24
|
+
{node}
|
|
25
|
+
</LocaleProvider>,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const part = (marker: string) => document.querySelector(`[data-terp="${marker}"]`);
|
|
30
|
+
const parts = (marker: string) => [...document.querySelectorAll(`[data-terp="${marker}"]`)];
|
|
31
|
+
|
|
32
|
+
describe("the value axis", () => {
|
|
33
|
+
it("rounds a step up to 1, 2, 2.5 or 5 times a power of ten", () => {
|
|
34
|
+
expect([0.7, 1, 1.3, 2.2, 3, 7, 12, 260].map((raw) => niceStep(raw))).toEqual([1, 1, 2, 2.5, 5, 10, 20, 500]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("spans the values in two equal round steps, zero kept where a mark's length is its value", () => {
|
|
38
|
+
// Mutation: drop zero from an area's axis, and the axis starts at the lowest value.
|
|
39
|
+
expect(valueAxis([118, 172], true)).toEqual({ low: 0, high: 200, ticks: [200, 100, 0] });
|
|
40
|
+
const line = valueAxis([118, 172], false);
|
|
41
|
+
expect(line.low).toBeGreaterThan(0);
|
|
42
|
+
expect(line.high).toBeGreaterThanOrEqual(172);
|
|
43
|
+
expect(line.ticks[0] - line.ticks[1]).toBe(line.ticks[1] - line.ticks[2]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("gives a flat or empty series an axis with height", () => {
|
|
47
|
+
const flat = valueAxis([5, 5, 5], true);
|
|
48
|
+
expect(flat.high).toBeGreaterThan(flat.low);
|
|
49
|
+
const empty = valueAxis([], false);
|
|
50
|
+
expect(empty.high).toBeGreaterThan(empty.low);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("steps an axis of counts in whole numbers", () => {
|
|
54
|
+
// Half a run is no label: a count axis takes 1, 2, 3 or 5 times a power of ten. Mutation:
|
|
55
|
+
// let 2.5 back in, and a maximum of five puts "2.5" -- or "3", rounded -- on the middle line.
|
|
56
|
+
expect([0.4, 1, 1.3, 2.2, 2.6, 4, 7, 26].map((raw) => niceStep(raw, true))).toEqual([1, 1, 2, 3, 3, 5, 10, 30]);
|
|
57
|
+
expect(valueAxis([0, 5], true, true).ticks).toEqual([6, 3, 0]);
|
|
58
|
+
expect(valueAxis([0, 5], true).ticks).toEqual([5, 2.5, 0]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("spans negative values without losing zero", () => {
|
|
62
|
+
const axis = valueAxis([-30, 40], true);
|
|
63
|
+
expect(axis.low).toBeLessThanOrEqual(-30);
|
|
64
|
+
expect(axis.high).toBeGreaterThanOrEqual(40);
|
|
65
|
+
expect(axis.ticks.length).toBe(3);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("places points edge to edge, and columns centred in bands of their own", () => {
|
|
69
|
+
expect([0, 1, 2].map((index) => xOf(index, 3))).toEqual([0, 50, 100]);
|
|
70
|
+
expect(columnOf(0, 4)).toEqual({ x: 5, width: 15 });
|
|
71
|
+
expect([0, 3].map((index) => bandCentre(index, 4))).toEqual([12.5, 87.5]);
|
|
72
|
+
expect(yOf(100, { low: 0, high: 200, ticks: [200, 100, 0] })).toBe(20);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("TrendChart", () => {
|
|
77
|
+
const WEEKS = [
|
|
78
|
+
{ label: "W1", value: 120 },
|
|
79
|
+
{ label: "W2", value: 140 },
|
|
80
|
+
{ label: "W3", value: 180 },
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
it("is a captioned figure whose picture is hidden and whose data is a table", () => {
|
|
84
|
+
renderIn("en", <TrendChart label="Rows synced" series={{ label: "This month", points: WEEKS }} />);
|
|
85
|
+
const figure = screen.getByRole("figure", { name: "Rows synced" });
|
|
86
|
+
expect(part("chart-body")).toHaveAttribute("aria-hidden", "true");
|
|
87
|
+
// The table is the chart for assistive technology: a row header per period, a value each.
|
|
88
|
+
// Mutation: drop the table, and the figure has nothing behind its caption.
|
|
89
|
+
const table = within(figure).getByRole("table", { name: "Rows synced" });
|
|
90
|
+
expect(within(table).getAllByRole("rowheader").map((cell) => cell.textContent)).toEqual([
|
|
91
|
+
"W1",
|
|
92
|
+
"W2",
|
|
93
|
+
"W3",
|
|
94
|
+
]);
|
|
95
|
+
expect(within(table).getAllByRole("cell").map((cell) => cell.textContent)).toEqual([
|
|
96
|
+
"",
|
|
97
|
+
"120",
|
|
98
|
+
"140",
|
|
99
|
+
"180",
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("draws a line by default, and an area and columns from zero", () => {
|
|
104
|
+
renderIn("en", <TrendChart label="Rows" series={{ label: "Rows", points: WEEKS }} />);
|
|
105
|
+
expect(part("trend-chart-line")).not.toBeNull();
|
|
106
|
+
expect(part("trend-chart-area")).toBeNull();
|
|
107
|
+
cleanup();
|
|
108
|
+
renderIn("en", <TrendChart label="Rows" mark="columns" series={{ label: "Rows", points: WEEKS }} />);
|
|
109
|
+
const columns = parts("trend-chart-column");
|
|
110
|
+
expect(columns).toHaveLength(3);
|
|
111
|
+
// The axis is 0..200, so 180 stands 36 of the plot's 40 units tall.
|
|
112
|
+
expect(columns[2]!.getAttribute("height")).toBe("36");
|
|
113
|
+
expect(columns[2]!.getAttribute("y")).toBe("4");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("prints the axis in the app's locale, top to bottom", () => {
|
|
117
|
+
renderIn("nl", (
|
|
118
|
+
<TrendChart
|
|
119
|
+
label="Omzet"
|
|
120
|
+
mark="area"
|
|
121
|
+
format={{ style: "currency", currency: "EUR", maximumFractionDigits: 0 }}
|
|
122
|
+
series={{ label: "Omzet", points: [{ label: "jan", value: 1500 }, { label: "feb", value: 3800 }] }}
|
|
123
|
+
/>
|
|
124
|
+
));
|
|
125
|
+
expect([...part("chart-axis")!.children].map((tick) => tick.textContent)).toEqual([
|
|
126
|
+
"€ 4.000",
|
|
127
|
+
"€ 2.000",
|
|
128
|
+
"€ 0",
|
|
129
|
+
]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("names the comparison in a legend and in the table, and draws it dashed behind the series", () => {
|
|
133
|
+
renderIn("en", (
|
|
134
|
+
<TrendChart
|
|
135
|
+
label="Rows"
|
|
136
|
+
series={{ label: "This month", points: WEEKS }}
|
|
137
|
+
comparison={{ label: "Last month", points: [{ label: "W1", value: 100 }, { label: "W2", value: 90 }, { label: "W3", value: 130 }] }}
|
|
138
|
+
/>
|
|
139
|
+
));
|
|
140
|
+
expect(parts("chart-legend-item").map((item) => item.textContent)).toEqual(["This month", "Last month"]);
|
|
141
|
+
expect(part("trend-chart-comparison")).not.toBeNull();
|
|
142
|
+
expect(
|
|
143
|
+
within(screen.getByRole("table")).getAllByRole("columnheader").map((cell) => cell.textContent),
|
|
144
|
+
).toEqual(["This month", "Last month"]);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("leaves a gap where a value is not a number, and prints the dash for it", () => {
|
|
148
|
+
renderIn("en", (
|
|
149
|
+
<TrendChart
|
|
150
|
+
label="Rows"
|
|
151
|
+
series={{ label: "Rows", points: [WEEKS[0]!, { label: "W2", value: Number.NaN }, WEEKS[2]!] }}
|
|
152
|
+
/>
|
|
153
|
+
));
|
|
154
|
+
// The line breaks at W2 rather than inventing it, leaving a point on each side with no
|
|
155
|
+
// neighbour, on a line's axis of counts from 120 to 180. A one-point polyline is never
|
|
156
|
+
// stroked, so each is a dot: a zero-length stroke the sheet caps round. Mutation: draw them
|
|
157
|
+
// as lines, and the plot is blank.
|
|
158
|
+
expect(parts("trend-chart-line")).toHaveLength(0);
|
|
159
|
+
expect(parts("trend-chart-dot").map((dot) => dot.getAttribute("points"))).toEqual([
|
|
160
|
+
"0,40 0,40",
|
|
161
|
+
"100,0 100,0",
|
|
162
|
+
]);
|
|
163
|
+
expect(within(screen.getByRole("table")).getAllByRole("cell")[2]!.textContent).toBe("—");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("draws a series of one point as a dot in the middle, its label under it", () => {
|
|
167
|
+
renderIn("en", <TrendChart label="Rows" series={{ label: "Rows", points: [{ label: "Today", value: 40 }] }} />);
|
|
168
|
+
expect(part("trend-chart-dot")!.getAttribute("points")).toMatch(/^50,[\d.]+ 50,[\d.]+$/);
|
|
169
|
+
expect([...part("chart-labels")!.children].map((label) => label.textContent)).toEqual(["Today"]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("prints a line's middle label only where a point stands under it", () => {
|
|
173
|
+
// The labels stand at the ends and the centre of the plot; with an even number of points no
|
|
174
|
+
// point is at the centre. Mutation: print the middle anyway, and six weeks label week 3 at
|
|
175
|
+
// the place between weeks 3 and 4.
|
|
176
|
+
const weeks = (count: number) =>
|
|
177
|
+
Array.from({ length: count }, (_, index) => ({ label: `P${index + 1}`, value: index }));
|
|
178
|
+
const printed = () => [...part("chart-labels")!.children].map((label) => label.textContent);
|
|
179
|
+
renderIn("en", <TrendChart label="Rows" series={{ label: "Rows", points: weeks(6) }} />);
|
|
180
|
+
expect(printed()).toEqual(["P1", "P6"]);
|
|
181
|
+
cleanup();
|
|
182
|
+
renderIn("en", <TrendChart label="Rows" series={{ label: "Rows", points: weeks(7) }} />);
|
|
183
|
+
expect(printed()).toEqual(["P1", "P4", "P7"]);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("places a comparison on the series' own scale, point for point, and lists every point", () => {
|
|
187
|
+
// A month so far against a whole month: point i of both stands at the same x, so the
|
|
188
|
+
// current one stops short. Mutation: scale each series to itself, and both reach the edge.
|
|
189
|
+
renderIn("en", (
|
|
190
|
+
<TrendChart
|
|
191
|
+
label="Rows"
|
|
192
|
+
series={{ label: "This month", points: [{ label: "A", value: 10 }, { label: "B", value: 20 }] }}
|
|
193
|
+
comparison={{
|
|
194
|
+
label: "Last month",
|
|
195
|
+
points: ["a", "b", "c", "d"].map((label, index) => ({ label, value: 5 * (index + 1) })),
|
|
196
|
+
}}
|
|
197
|
+
/>
|
|
198
|
+
));
|
|
199
|
+
const xs = (marker: string) =>
|
|
200
|
+
part(marker)!.getAttribute("points")!.split(" ").map((point) => Number(point.split(",")[0]));
|
|
201
|
+
expect(xs("trend-chart-line")).toEqual([0, 33.33]);
|
|
202
|
+
expect(xs("trend-chart-comparison")).toEqual([0, 33.33, 66.67, 100]);
|
|
203
|
+
// The table pairs the two by index and lists the longer one whole, the dash where the
|
|
204
|
+
// series has no point yet.
|
|
205
|
+
const table = screen.getByRole("table", { name: "Rows" });
|
|
206
|
+
expect(within(table).getAllByRole("rowheader").map((cell) => cell.textContent)).toEqual(["A", "B", "c", "d"]);
|
|
207
|
+
expect(within(table).getAllByRole("cell").slice(1).map((cell) => cell.textContent)).toEqual([
|
|
208
|
+
"10", "5", "20", "10", "—", "15", "—", "20",
|
|
209
|
+
]);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("bends a comparison over columns at their centres, drawn over them", () => {
|
|
213
|
+
renderIn("en", (
|
|
214
|
+
<TrendChart
|
|
215
|
+
label="Runs"
|
|
216
|
+
mark="columns"
|
|
217
|
+
series={{ label: "This week", points: [1, 2, 3, 4].map((value) => ({ label: `D${value}`, value })) }}
|
|
218
|
+
comparison={{ label: "Last week", points: [2, 2, 2, 2].map((value, index) => ({ label: `D${index + 1}`, value })) }}
|
|
219
|
+
/>
|
|
220
|
+
));
|
|
221
|
+
const comparison = part("trend-chart-comparison")!;
|
|
222
|
+
expect(comparison.getAttribute("points")!.split(" ").map((point) => Number(point.split(",")[0]))).toEqual([
|
|
223
|
+
12.5, 37.5, 62.5, 87.5,
|
|
224
|
+
]);
|
|
225
|
+
// After the columns in the drawing, so the opaque columns do not hide it.
|
|
226
|
+
const plot = [...part("chart-plot")!.children].map((child) => child.getAttribute("data-terp"));
|
|
227
|
+
expect(plot.lastIndexOf("trend-chart-column")).toBeLessThan(plot.indexOf("trend-chart-comparison"));
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("steps the axis of a series of counts in whole numbers", () => {
|
|
231
|
+
renderIn("en", (
|
|
232
|
+
<TrendChart
|
|
233
|
+
label="Runs"
|
|
234
|
+
mark="columns"
|
|
235
|
+
series={{ label: "Runs", points: [1, 5, 3].map((value, index) => ({ label: `D${index}`, value })) }}
|
|
236
|
+
/>
|
|
237
|
+
));
|
|
238
|
+
expect([...part("chart-axis")!.children].map((tick) => tick.textContent)).toEqual(["6", "3", "0"]);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("prints a column chart's labels under each column, the ones not printed holding their place", () => {
|
|
242
|
+
const many = Array.from({ length: 7 }, (_, index) => ({ label: `D${index + 1}`, value: index }));
|
|
243
|
+
renderIn("en", <TrendChart label="Runs" mark="columns" series={{ label: "Runs", points: many }} />);
|
|
244
|
+
const labels = [...part("chart-labels")!.children];
|
|
245
|
+
expect(labels).toHaveLength(7);
|
|
246
|
+
expect(labels.filter((label) => !label.hasAttribute("data-quiet")).map((label) => label.textContent)).toEqual([
|
|
247
|
+
"D1",
|
|
248
|
+
"D4",
|
|
249
|
+
"D7",
|
|
250
|
+
]);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
describe("BarChart", () => {
|
|
255
|
+
it("is a table of its categories, each bar scaled to the largest", () => {
|
|
256
|
+
// The chart is its own table: a row header per category, the bar hidden, the value printed.
|
|
257
|
+
renderIn("en", (
|
|
258
|
+
<BarChart
|
|
259
|
+
label="Rows by source"
|
|
260
|
+
bars={[
|
|
261
|
+
{ label: "Customers", value: 1200 },
|
|
262
|
+
{ label: "Orders", value: 600 },
|
|
263
|
+
{ label: "Stock", value: 0 },
|
|
264
|
+
]}
|
|
265
|
+
/>
|
|
266
|
+
));
|
|
267
|
+
const figure = screen.getByRole("figure", { name: "Rows by source" });
|
|
268
|
+
expect(within(figure).getAllByRole("rowheader").map((cell) => cell.textContent)).toEqual([
|
|
269
|
+
"Customers",
|
|
270
|
+
"Orders",
|
|
271
|
+
"Stock",
|
|
272
|
+
]);
|
|
273
|
+
expect(parts("bar-chart-value").map((cell) => cell.textContent)).toEqual(["1,200", "600", "0"]);
|
|
274
|
+
// Mutation: scale each bar to itself, and every bar is full.
|
|
275
|
+
expect(parts("bar-chart-mark").map((mark) => mark.getAttribute("width"))).toEqual(["100", "50", "0"]);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("names its table by the caption, and hides the bar's cell rather than reading it as blank", () => {
|
|
279
|
+
renderIn("en", <BarChart label="Rows by source" bars={[{ label: "Customers", value: 1200 }]} />);
|
|
280
|
+
// Mutation: drop the table's name, or read the bar cell, and a row reads "Customers, blank, 1,200".
|
|
281
|
+
expect(screen.getByRole("table", { name: "Rows by source" })).toBeInTheDocument();
|
|
282
|
+
expect(part("bar-chart-bar")).toHaveAttribute("aria-hidden", "true");
|
|
283
|
+
// The name sits in a box of its own, which is what the sheet caps at a third of the chart.
|
|
284
|
+
expect(part("bar-chart-name")!.textContent).toBe("Customers");
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("scales to a declared max, and draws no bar for a value that is not a number", () => {
|
|
288
|
+
renderIn("en", (
|
|
289
|
+
<BarChart label="Load" max={2000} bars={[{ label: "A", value: 500 }, { label: "B", value: Number.NaN }]} />
|
|
290
|
+
));
|
|
291
|
+
expect(parts("bar-chart-mark").map((mark) => mark.getAttribute("width"))).toEqual(["25", "0"]);
|
|
292
|
+
expect(parts("bar-chart-value")[1]!.textContent).toBe("—");
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
describe("ProportionBar", () => {
|
|
297
|
+
const RUN = [
|
|
298
|
+
{ label: "Created", value: 30, tone: "success" as const },
|
|
299
|
+
{ label: "Updated", value: 60, tone: "info" as const },
|
|
300
|
+
{ label: "Failed", value: 10, tone: "danger" as const },
|
|
301
|
+
];
|
|
302
|
+
|
|
303
|
+
it("lays the parts along one bar by their shares, parted by hairlines", () => {
|
|
304
|
+
renderIn("en", <ProportionBar label="Last run" parts={RUN} />);
|
|
305
|
+
expect(parts("proportion-bar-part").map((rect) => [rect.getAttribute("x"), rect.getAttribute("width")])).toEqual([
|
|
306
|
+
["0", "30"],
|
|
307
|
+
["30", "60"],
|
|
308
|
+
["90", "10"],
|
|
309
|
+
]);
|
|
310
|
+
expect(parts("proportion-bar-gap")).toHaveLength(2);
|
|
311
|
+
expect(part("proportion-bar-track")).toHaveAttribute("aria-hidden", "true");
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("says every part in words: its name, its count and its share", () => {
|
|
315
|
+
// A tone is always also a word: the legend is the chart's data. Mutation: drop the share,
|
|
316
|
+
// and "10%" is never printed.
|
|
317
|
+
renderIn("nl", <ProportionBar label="Laatste run" parts={RUN} />);
|
|
318
|
+
// A space between the name and the count, or a screen reader says "Created30".
|
|
319
|
+
expect(parts("chart-legend-item").map((item) => item.textContent)).toEqual([
|
|
320
|
+
"Created 30 · 30%",
|
|
321
|
+
"Updated 60 · 60%",
|
|
322
|
+
"Failed 10 · 10%",
|
|
323
|
+
]);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("takes the chart colours in order for parts without a tone, and no share from a part that is not positive", () => {
|
|
327
|
+
renderIn("en", (
|
|
328
|
+
<ProportionBar
|
|
329
|
+
label="Sources"
|
|
330
|
+
parts={[
|
|
331
|
+
{ label: "A", value: 3 },
|
|
332
|
+
{ label: "B", value: 1 },
|
|
333
|
+
{ label: "C", value: -2 },
|
|
334
|
+
]}
|
|
335
|
+
/>
|
|
336
|
+
));
|
|
337
|
+
expect(parts("proportion-bar-part").map((rect) => rect.getAttribute("data-series"))).toEqual(["1", "2"]);
|
|
338
|
+
expect(parts("chart-legend-value").map((value) => value.textContent)).toEqual(["3 · 75%", "1 · 25%", "-2 · 0%"]);
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
describe("StatusHistory", () => {
|
|
343
|
+
const SUCCEEDED = { label: "Succeeded", tone: "success" as const };
|
|
344
|
+
const FAILED = { label: "Failed", tone: "danger" as const };
|
|
345
|
+
const RUNS = [
|
|
346
|
+
{ label: "Mon", outcome: SUCCEEDED },
|
|
347
|
+
{ label: "Tue", outcome: FAILED },
|
|
348
|
+
{ label: "Wed", outcome: SUCCEEDED },
|
|
349
|
+
];
|
|
350
|
+
|
|
351
|
+
it("draws a cell per run in its tone, and prints the latest ending", () => {
|
|
352
|
+
renderIn("en", <StatusHistory label="Runs" runs={RUNS} />);
|
|
353
|
+
expect(parts("status-history-cell").map((cell) => cell.getAttribute("data-tone"))).toEqual([
|
|
354
|
+
"success",
|
|
355
|
+
"danger",
|
|
356
|
+
"success",
|
|
357
|
+
]);
|
|
358
|
+
expect(part("status-history-latest")!.textContent).toBe("Succeeded");
|
|
359
|
+
expect(part("status-history-cells")).toHaveAttribute("aria-hidden", "true");
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("reads every run out, its label and its ending, oldest first", () => {
|
|
363
|
+
// Mutation: drop the text, and the cells' colours say it to nobody who cannot see them.
|
|
364
|
+
renderIn("nl", <StatusHistory label="Runs" runs={RUNS} />);
|
|
365
|
+
expect(part("status-history-data")!.textContent).toBe(
|
|
366
|
+
"Runs, oudste eerst: Mon: Succeeded, Tue: Failed en Wed: Succeeded",
|
|
367
|
+
);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it("renders nothing for no runs, rather than a sentence that stops at its colon", () => {
|
|
371
|
+
const { container } = renderIn("en", <StatusHistory label="Runs" runs={[]} />);
|
|
372
|
+
expect(container.textContent).toBe("");
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it("is phrasing content, so it can sit in a link or a cell", () => {
|
|
376
|
+
renderIn("en", <StatusHistory label="Runs" runs={RUNS} />);
|
|
377
|
+
const tags = [part("status-history")!, ...part("status-history")!.querySelectorAll("*")].map(
|
|
378
|
+
(element) => element.tagName.toLowerCase(),
|
|
379
|
+
);
|
|
380
|
+
expect(tags.every((tag) => tag === "span")).toBe(true);
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
describe("the charts' colours are declared pairings", () => {
|
|
385
|
+
const declared = (fg: string, bg: string) =>
|
|
386
|
+
[...tokenPairs.textPairs, ...tokenPairs.nonTextPairs].some(
|
|
387
|
+
(pair) => "fg" in pair && pair.fg === fg && pair.bg === bg,
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
it("holds every chart colour the sheet paints to 3:1 against the surface", () => {
|
|
391
|
+
// The chart ramp only: every chart colour the sheet paints has its pairing on the surface.
|
|
392
|
+
// Mutation: drop one of those pairings, and this names it.
|
|
393
|
+
const painted = new Set(
|
|
394
|
+
[...TERP_STYLES_CSS.matchAll(/(?:fill|background|stroke): var\((--color-chart-[1-5])\)/g)].map(
|
|
395
|
+
(match) => match[1]!,
|
|
396
|
+
),
|
|
397
|
+
);
|
|
398
|
+
expect([...painted].sort()).toEqual([
|
|
399
|
+
"--color-chart-1",
|
|
400
|
+
"--color-chart-2",
|
|
401
|
+
"--color-chart-3",
|
|
402
|
+
"--color-chart-4",
|
|
403
|
+
"--color-chart-5",
|
|
404
|
+
]);
|
|
405
|
+
for (const token of painted) {
|
|
406
|
+
expect(declared(token, "--color-bg-surface"), `${token} on the surface`).toBe(true);
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic every chart kind shares: a value axis with round bounds, and positions on a
|
|
3
|
+
* plot whose coordinates are proportions rather than pixels.
|
|
4
|
+
*
|
|
5
|
+
* Kept free of React and of the DOM, so the geometry is tested as numbers — the screenshot lane
|
|
6
|
+
* shows a picture, and a picture cannot say that a column is a pixel too short.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** The plot's coordinate space. The SVG stretches to its box, so these are proportions. */
|
|
10
|
+
export const PLOT_WIDTH = 100;
|
|
11
|
+
export const PLOT_HEIGHT = 40;
|
|
12
|
+
|
|
13
|
+
/** A value axis: its bottom, its top and the tick between them. */
|
|
14
|
+
export interface ValueAxis {
|
|
15
|
+
low: number;
|
|
16
|
+
high: number;
|
|
17
|
+
/** The three ticks, top first, the order the labels are read down the axis. */
|
|
18
|
+
ticks: readonly [number, number, number];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The smallest of 1, 2, 2.5 and 5 times a power of ten that is at least `raw`. An axis of
|
|
23
|
+
* `counts` steps in whole numbers instead -- 1, 2, 3 and 5 times a power of ten, and never
|
|
24
|
+
* below 1 -- since half a run is a label nobody can read, and a format that rounds it prints a
|
|
25
|
+
* tick the gridline is not at.
|
|
26
|
+
*/
|
|
27
|
+
export function niceStep(raw: number, counts = false): number {
|
|
28
|
+
if (!(raw > 0) || !Number.isFinite(raw)) {
|
|
29
|
+
return 1;
|
|
30
|
+
}
|
|
31
|
+
if (counts && raw <= 1) {
|
|
32
|
+
return 1;
|
|
33
|
+
}
|
|
34
|
+
const power = 10 ** Math.floor(Math.log10(raw));
|
|
35
|
+
for (const factor of counts ? [1, 2, 3, 5, 10] : [1, 2, 2.5, 5, 10]) {
|
|
36
|
+
if (factor * power >= raw - power * 1e-9) {
|
|
37
|
+
return factor * power;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return 10 * power;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A value axis over `values` in two equal steps of a round size, so its three tick labels can
|
|
45
|
+
* sit at the top, the middle and the bottom of the plot with no position of their own.
|
|
46
|
+
*
|
|
47
|
+
* `fromZero` keeps zero on the axis — required where a mark's length is its value (an area, a
|
|
48
|
+
* column), and left off for a line, where an axis from zero flattens the change the line is
|
|
49
|
+
* there to show. An empty or a flat series still gets an axis with height. `counts` steps it in
|
|
50
|
+
* whole numbers, for values that are counts.
|
|
51
|
+
*/
|
|
52
|
+
export function valueAxis(values: readonly number[], fromZero: boolean, counts = false): ValueAxis {
|
|
53
|
+
const finite = values.filter((value) => Number.isFinite(value));
|
|
54
|
+
let min = finite.length === 0 ? 0 : Math.min(...finite);
|
|
55
|
+
let max = finite.length === 0 ? 0 : Math.max(...finite);
|
|
56
|
+
if (fromZero) {
|
|
57
|
+
min = Math.min(min, 0);
|
|
58
|
+
max = Math.max(max, 0);
|
|
59
|
+
}
|
|
60
|
+
if (min === max) {
|
|
61
|
+
// A flat series: give it a step either side of itself, or one step above zero.
|
|
62
|
+
const pad = niceStep(Math.abs(max) || 1, counts);
|
|
63
|
+
min = fromZero && min >= 0 ? 0 : min - pad;
|
|
64
|
+
max = max + pad;
|
|
65
|
+
}
|
|
66
|
+
let step = niceStep((max - min) / 2, counts);
|
|
67
|
+
for (;;) {
|
|
68
|
+
const low = Math.floor(min / step) * step;
|
|
69
|
+
const high = low + 2 * step;
|
|
70
|
+
if (high >= max - step * 1e-9) {
|
|
71
|
+
return { low: clean(low), high: clean(high), ticks: [clean(high), clean(low + step), clean(low)] };
|
|
72
|
+
}
|
|
73
|
+
step = niceStep(step * 1.01, counts);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Float residue off a computed tick (0.30000000000000004 is 0.3). */
|
|
78
|
+
const clean = (value: number) => Number.parseFloat(value.toPrecision(12));
|
|
79
|
+
|
|
80
|
+
/** The y coordinate of `value` on an axis, top of the plot highest. */
|
|
81
|
+
export function yOf(value: number, axis: ValueAxis): number {
|
|
82
|
+
const share = (value - axis.low) / (axis.high - axis.low);
|
|
83
|
+
return round(PLOT_HEIGHT - share * PLOT_HEIGHT);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The x coordinate of point `index` of `count`, the first at the left edge and the last at the right. */
|
|
87
|
+
export function xOf(index: number, count: number): number {
|
|
88
|
+
return count <= 1 ? PLOT_WIDTH / 2 : round((index / (count - 1)) * PLOT_WIDTH);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The middle of band `index` of `count`: where a column stands, and a line drawn over columns bends. */
|
|
92
|
+
export function bandCentre(index: number, count: number): number {
|
|
93
|
+
return round(((index + 0.5) * PLOT_WIDTH) / count);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A column's left edge and width, in a band of its own: each of `count` bands holds one column. */
|
|
97
|
+
export function columnOf(index: number, count: number): { x: number; width: number } {
|
|
98
|
+
const band = PLOT_WIDTH / count;
|
|
99
|
+
const width = band * 0.6;
|
|
100
|
+
return { x: round(index * band + (band - width) / 2), width: round(width) };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const round = (value: number) => Math.round(value * 100) / 100;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import type { RefObject } from "react";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Keeps a scrolling strip's current item in the strip's view: the strip scrolls, the page never
|
|
6
|
+
* does. Tab strips are one line that scrolls sideways where they do not fit (the sheet's Tabs and
|
|
7
|
+
* Module navigation notes), so a current tab can sit past the strip's edge, as the third of three
|
|
8
|
+
* on a phone does; this brings it back whenever the current item changes.
|
|
9
|
+
*
|
|
10
|
+
* Measured rather than delegated to `scrollIntoView`, which scrolls every scrollable ancestor up
|
|
11
|
+
* to the page. Where nothing is laid out (jsdom, a strip that fits) the boxes agree and it does
|
|
12
|
+
* nothing.
|
|
13
|
+
*/
|
|
14
|
+
export function useCurrentInView(
|
|
15
|
+
strip: RefObject<HTMLElement | null>,
|
|
16
|
+
current: string | undefined,
|
|
17
|
+
selector: string,
|
|
18
|
+
): void {
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
const list = strip.current;
|
|
21
|
+
const item = list?.querySelector<HTMLElement>(selector);
|
|
22
|
+
if (!list || !item) return;
|
|
23
|
+
const bounds = list.getBoundingClientRect();
|
|
24
|
+
const box = item.getBoundingClientRect();
|
|
25
|
+
if (box.left < bounds.left) list.scrollLeft -= bounds.left - box.left;
|
|
26
|
+
else if (box.right > bounds.right) list.scrollLeft += box.right - bounds.right;
|
|
27
|
+
}, [strip, current, selector]);
|
|
28
|
+
}
|