@terpjs/react-core 0.29.0 → 0.31.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/Page.test.tsx +215 -1
- package/src/Page.tsx +145 -54
- package/src/PageActions.test.tsx +64 -3
- package/src/PageActions.tsx +40 -15
- package/src/PageSequence.tsx +114 -0
- package/src/Stat.test.tsx +341 -0
- package/src/Stat.tsx +314 -0
- package/src/Timeline.test.tsx +74 -0
- package/src/Timeline.tsx +71 -0
- package/src/admin/AdminHub.tsx +11 -3
- 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 +83 -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 +208 -5
- 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/dataview/DataView.test.tsx +255 -0
- package/src/dataview/DataView.tsx +35 -0
- package/src/dataview/DataViewCardList.tsx +21 -13
- package/src/dataview/DataViewTable.tsx +5 -6
- 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 +20 -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 +252 -176
- package/src/styles.ts +1290 -318
- 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 +16 -18
- 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/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 +76 -7
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { useId } from "react";
|
|
2
|
+
|
|
3
|
+
import { useFormatNumber } from "../format";
|
|
4
|
+
import { injectTerpStyles } from "../styles";
|
|
5
|
+
import { useUiText } from "../uiText";
|
|
6
|
+
import type { UiText } from "../uiText";
|
|
7
|
+
import { PLOT_HEIGHT, PLOT_WIDTH, bandCentre, columnOf, valueAxis, xOf, yOf } from "./scale";
|
|
8
|
+
import type { ValueAxis } from "./scale";
|
|
9
|
+
|
|
10
|
+
injectTerpStyles();
|
|
11
|
+
|
|
12
|
+
/** One point of a series, or one bar: what it is, and how much. */
|
|
13
|
+
export interface ChartPoint {
|
|
14
|
+
/** What the point is — a period's name or a date in a trend, a category in a bar chart. */
|
|
15
|
+
label: UiText;
|
|
16
|
+
/** How much, in the chart's format. A value that is not a number is printed as the dash and drawn as a gap. */
|
|
17
|
+
value: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A named run of points, oldest first. */
|
|
21
|
+
export interface ChartSeries {
|
|
22
|
+
/** What the series is ("This month"): its legend entry and its column in the table. */
|
|
23
|
+
label: UiText;
|
|
24
|
+
points: readonly ChartPoint[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** How the series is drawn: a line, a line with the area under it filled, or a column per point. */
|
|
28
|
+
export type TrendMark = "line" | "area" | "columns";
|
|
29
|
+
|
|
30
|
+
export interface TrendChartProps {
|
|
31
|
+
/** What the chart shows: printed as its caption, and its accessible name. */
|
|
32
|
+
label: UiText;
|
|
33
|
+
/** The values over time. */
|
|
34
|
+
series: ChartSeries;
|
|
35
|
+
/**
|
|
36
|
+
* The same measure for an earlier period, drawn as a dashed line — behind a line or an area,
|
|
37
|
+
* over columns, where behind them it would be hidden — and listed beside the series in the
|
|
38
|
+
* table: the comparison a figure's delta summarises. Point `i` of
|
|
39
|
+
* either series stands at the same place, so a period still running stops short of the one it
|
|
40
|
+
* is compared with, as "this month so far" against last month should. Where the two differ in
|
|
41
|
+
* length, label points by their place in the period ("Day 3"): the axis and the table read a
|
|
42
|
+
* point's label from whichever series has that point.
|
|
43
|
+
*/
|
|
44
|
+
comparison?: ChartSeries;
|
|
45
|
+
/**
|
|
46
|
+
* `"line"` (the default) for a level that moves, `"area"` for an amount that accumulates,
|
|
47
|
+
* `"columns"` for a count per period — runs per day, deliveries per hour. An area and columns
|
|
48
|
+
* keep zero on the axis, because their length is the value; a line does not, because an axis
|
|
49
|
+
* from zero flattens the change a line is drawn to show.
|
|
50
|
+
*/
|
|
51
|
+
mark?: TrendMark;
|
|
52
|
+
/** How values print, as `Intl.NumberFormatOptions` (default: a plain number). */
|
|
53
|
+
format?: Intl.NumberFormatOptions;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Points as runs of coordinates, broken where a value is not a number. `x` places point `i`;
|
|
58
|
+
* a run of one point is kept, and drawn as a dot, since a line needs two.
|
|
59
|
+
*/
|
|
60
|
+
function runsOf(points: readonly ChartPoint[], axis: ValueAxis, x: (index: number) => number): string[][] {
|
|
61
|
+
const runs: string[][] = [];
|
|
62
|
+
let current: string[] = [];
|
|
63
|
+
points.forEach((point, index) => {
|
|
64
|
+
if (!Number.isFinite(point.value)) {
|
|
65
|
+
if (current.length > 0) runs.push(current);
|
|
66
|
+
current = [];
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
current.push(`${x(index)},${yOf(point.value, axis)}`);
|
|
70
|
+
});
|
|
71
|
+
if (current.length > 0) runs.push(current);
|
|
72
|
+
return runs;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Values over time (ADR 0158, ADR 0169 §6): a line, an area or columns, with an optional
|
|
77
|
+
* comparison period, on a value axis of round numbers.
|
|
78
|
+
*
|
|
79
|
+
* Under ADR 0158's contract: SVG geometry in attributes and colour from the sheet, so nothing
|
|
80
|
+
* is styled inline; numbers through the `format` helpers in the app's locale; no dependency;
|
|
81
|
+
* and the data as a table — visually hidden here, read by assistive technology in place of the
|
|
82
|
+
* picture, which is hidden from it. The axis takes three ticks in two equal steps, so its labels
|
|
83
|
+
* stand at the top, the middle and the foot of the plot without a position of their own; an
|
|
84
|
+
* axis of counts steps in whole numbers.
|
|
85
|
+
*
|
|
86
|
+
* A tile on the surface, the rung where data is read (ADR 0169 §3), like a figure.
|
|
87
|
+
*/
|
|
88
|
+
export function TrendChart({ label, series, comparison, mark = "line", format }: TrendChartProps) {
|
|
89
|
+
const resolve = useUiText();
|
|
90
|
+
const formatNumber = useFormatNumber();
|
|
91
|
+
const captionId = useId();
|
|
92
|
+
const points = series.points;
|
|
93
|
+
const earlier = comparison?.points ?? [];
|
|
94
|
+
// One index scale for both series, as long as the longer of them.
|
|
95
|
+
const count = Math.max(points.length, earlier.length);
|
|
96
|
+
const values = [...points, ...earlier].map((point) => point.value);
|
|
97
|
+
const counts = values.filter((value) => Number.isFinite(value)).every((value) => Number.isInteger(value));
|
|
98
|
+
const axis = valueAxis(values, mark !== "line", counts);
|
|
99
|
+
const base = yOf(Math.min(Math.max(0, axis.low), axis.high), axis);
|
|
100
|
+
// A line's point stands at its index across the plot; a column's in the middle of its band,
|
|
101
|
+
// where a comparison drawn over columns puts its vertices too.
|
|
102
|
+
const x = mark === "columns" ? (index: number) => bandCentre(index, count) : (index: number) => xOf(index, count);
|
|
103
|
+
const lines = runsOf(points, axis, x);
|
|
104
|
+
const before = runsOf(earlier, axis, x);
|
|
105
|
+
const caption = resolve(label);
|
|
106
|
+
const labelAt = (index: number) => resolve(points[index]?.label ?? earlier[index]?.label ?? "");
|
|
107
|
+
// Which labels the x axis prints: the ends, and the middle where there are enough points for
|
|
108
|
+
// one. A column chart prints under its columns, so its middle band always has one; a line's
|
|
109
|
+
// middle label sits at the plot's centre, where only an odd number of points puts a point.
|
|
110
|
+
const middle = (count - 1) / 2;
|
|
111
|
+
const shown = (index: number) =>
|
|
112
|
+
index === 0 ||
|
|
113
|
+
index === count - 1 ||
|
|
114
|
+
(count >= 5 && (mark === "columns" ? index === Math.floor(middle) : index === middle));
|
|
115
|
+
const indices = Array.from({ length: count }, (_, index) => index);
|
|
116
|
+
const comparisonMarks = before.map((run, index) =>
|
|
117
|
+
run.length === 1 ? (
|
|
118
|
+
// A zero-length stroke with a round cap: a dot the plot's stretch cannot flatten.
|
|
119
|
+
<polyline key={`c${index}`} data-terp="trend-chart-dot" data-series="comparison" points={`${run[0]} ${run[0]}`} />
|
|
120
|
+
) : (
|
|
121
|
+
<polyline key={`c${index}`} data-terp="trend-chart-comparison" points={run.join(" ")} />
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
return (
|
|
125
|
+
// Named by its caption explicitly: the figure-from-figcaption name is the HTML mapping
|
|
126
|
+
// browsers implement and not every accessibility tree computes.
|
|
127
|
+
<figure data-terp="trend-chart" data-mark={mark} aria-labelledby={captionId}>
|
|
128
|
+
<figcaption id={captionId} data-terp="chart-caption">
|
|
129
|
+
{caption}
|
|
130
|
+
</figcaption>
|
|
131
|
+
{comparison !== undefined && (
|
|
132
|
+
<ul data-terp="chart-legend">
|
|
133
|
+
<li data-terp="chart-legend-item">
|
|
134
|
+
<span data-terp="chart-swatch" data-series="main" aria-hidden="true" />
|
|
135
|
+
{resolve(series.label)}
|
|
136
|
+
</li>
|
|
137
|
+
<li data-terp="chart-legend-item">
|
|
138
|
+
<span data-terp="chart-swatch" data-series="comparison" aria-hidden="true" />
|
|
139
|
+
{resolve(comparison.label)}
|
|
140
|
+
</li>
|
|
141
|
+
</ul>
|
|
142
|
+
)}
|
|
143
|
+
<div data-terp="chart-body" aria-hidden="true">
|
|
144
|
+
<span data-terp="chart-axis">
|
|
145
|
+
{axis.ticks.map((tick, index) => (
|
|
146
|
+
<span key={index}>{formatNumber(tick, format)}</span>
|
|
147
|
+
))}
|
|
148
|
+
</span>
|
|
149
|
+
<svg
|
|
150
|
+
data-terp="chart-plot"
|
|
151
|
+
viewBox={`0 0 ${PLOT_WIDTH} ${PLOT_HEIGHT}`}
|
|
152
|
+
preserveAspectRatio="none"
|
|
153
|
+
focusable="false"
|
|
154
|
+
>
|
|
155
|
+
{[0, PLOT_HEIGHT / 2, PLOT_HEIGHT].map((y) => (
|
|
156
|
+
<line key={y} data-terp="chart-gridline" x1={0} x2={PLOT_WIDTH} y1={y} y2={y} />
|
|
157
|
+
))}
|
|
158
|
+
{mark !== "columns" && comparisonMarks}
|
|
159
|
+
{mark === "area" &&
|
|
160
|
+
lines
|
|
161
|
+
.filter((run) => run.length > 1)
|
|
162
|
+
.map((run, index) => {
|
|
163
|
+
const first = run[0]!.split(",")[0];
|
|
164
|
+
const last = run.at(-1)!.split(",")[0];
|
|
165
|
+
return (
|
|
166
|
+
<polygon
|
|
167
|
+
key={index}
|
|
168
|
+
data-terp="trend-chart-area"
|
|
169
|
+
points={`${first},${base} ${run.join(" ")} ${last},${base}`}
|
|
170
|
+
/>
|
|
171
|
+
);
|
|
172
|
+
})}
|
|
173
|
+
{mark !== "columns" &&
|
|
174
|
+
lines.map((run, index) =>
|
|
175
|
+
run.length === 1 ? (
|
|
176
|
+
<polyline key={index} data-terp="trend-chart-dot" points={`${run[0]} ${run[0]}`} />
|
|
177
|
+
) : (
|
|
178
|
+
<polyline key={index} data-terp="trend-chart-line" points={run.join(" ")} />
|
|
179
|
+
),
|
|
180
|
+
)}
|
|
181
|
+
{mark === "columns" &&
|
|
182
|
+
points.map((point, index) => {
|
|
183
|
+
if (!Number.isFinite(point.value)) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
const column = columnOf(index, count);
|
|
187
|
+
const y = yOf(point.value, axis);
|
|
188
|
+
return (
|
|
189
|
+
<rect
|
|
190
|
+
key={index}
|
|
191
|
+
data-terp="trend-chart-column"
|
|
192
|
+
x={column.x}
|
|
193
|
+
y={Math.min(y, base)}
|
|
194
|
+
width={column.width}
|
|
195
|
+
height={Math.abs(base - y)}
|
|
196
|
+
/>
|
|
197
|
+
);
|
|
198
|
+
})}
|
|
199
|
+
{mark === "columns" && comparisonMarks}
|
|
200
|
+
</svg>
|
|
201
|
+
<span data-terp="chart-labels" data-mark={mark}>
|
|
202
|
+
{indices.map((index) =>
|
|
203
|
+
mark === "columns" || shown(index) ? (
|
|
204
|
+
<span key={index} data-quiet={mark === "columns" && !shown(index) ? "true" : undefined}>
|
|
205
|
+
{labelAt(index)}
|
|
206
|
+
</span>
|
|
207
|
+
) : null,
|
|
208
|
+
)}
|
|
209
|
+
</span>
|
|
210
|
+
</div>
|
|
211
|
+
{/* Named by the same caption rather than a <caption> of its own, so it is not read twice. */}
|
|
212
|
+
<table data-terp="chart-table" aria-labelledby={captionId}>
|
|
213
|
+
<thead>
|
|
214
|
+
<tr>
|
|
215
|
+
<td />
|
|
216
|
+
<th scope="col">{resolve(series.label)}</th>
|
|
217
|
+
{comparison !== undefined && <th scope="col">{resolve(comparison.label)}</th>}
|
|
218
|
+
</tr>
|
|
219
|
+
</thead>
|
|
220
|
+
<tbody>
|
|
221
|
+
{indices.map((index) => (
|
|
222
|
+
<tr key={index}>
|
|
223
|
+
<th scope="row">{labelAt(index)}</th>
|
|
224
|
+
<td>{formatNumber(points[index]?.value, format)}</td>
|
|
225
|
+
{comparison !== undefined && <td>{formatNumber(earlier[index]?.value, format)}</td>}
|
|
226
|
+
</tr>
|
|
227
|
+
))}
|
|
228
|
+
</tbody>
|
|
229
|
+
</table>
|
|
230
|
+
</figure>
|
|
231
|
+
);
|
|
232
|
+
}
|
|
@@ -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
|
+
});
|