@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.
Files changed (82) hide show
  1. package/README.md +35 -14
  2. package/package.json +5 -5
  3. package/src/DashboardPage.tsx +34 -0
  4. package/src/Field.test.tsx +26 -1
  5. package/src/Field.tsx +15 -5
  6. package/src/HubPage.tsx +7 -2
  7. package/src/ModuleNav.test.tsx +29 -1
  8. package/src/ModuleNav.tsx +5 -1
  9. package/src/Page.test.tsx +192 -0
  10. package/src/Page.tsx +141 -55
  11. package/src/PageActions.test.tsx +64 -3
  12. package/src/PageActions.tsx +40 -15
  13. package/src/PageSequence.tsx +114 -0
  14. package/src/Stat.test.tsx +376 -0
  15. package/src/Stat.tsx +316 -0
  16. package/src/Timeline.test.tsx +74 -0
  17. package/src/Timeline.tsx +71 -0
  18. package/src/admin/AdminHub.tsx +233 -78
  19. package/src/admin/AuditLogAdmin.tsx +3 -1
  20. package/src/admin/GroupCreate.tsx +3 -1
  21. package/src/admin/GroupDetail.tsx +72 -67
  22. package/src/admin/GroupsAdmin.tsx +3 -1
  23. package/src/admin/ModuleAccessPanel.tsx +13 -12
  24. package/src/admin/RecordHistory.tsx +88 -0
  25. package/src/admin/UserCreate.tsx +3 -1
  26. package/src/admin/UserDetail.tsx +8 -1
  27. package/src/admin/UsersAdmin.tsx +3 -0
  28. package/src/admin/admin.test.tsx +389 -19
  29. package/src/bootstrap.test.tsx +31 -7
  30. package/src/bootstrap.tsx +30 -4
  31. package/src/charts/BarChart.tsx +74 -0
  32. package/src/charts/ProportionBar.tsx +118 -0
  33. package/src/charts/StatusHistory.tsx +75 -0
  34. package/src/charts/TrendChart.tsx +232 -0
  35. package/src/charts/charts.test.tsx +409 -0
  36. package/src/charts/scale.ts +103 -0
  37. package/src/currentInView.ts +28 -0
  38. package/src/dataview/DataView.test.tsx +303 -1
  39. package/src/dataview/DataView.tsx +53 -3
  40. package/src/dataview/DataViewCardList.tsx +21 -13
  41. package/src/dataview/DataViewTable.tsx +5 -6
  42. package/src/dataview/DataViewToolbar.tsx +4 -1
  43. package/src/dataview/README.md +18 -0
  44. package/src/dataview/index.ts +1 -0
  45. package/src/dataview/internal.tsx +112 -5
  46. package/src/dataview/types.ts +39 -10
  47. package/src/errorMessages.test.tsx +17 -0
  48. package/src/errorMessages.tsx +18 -0
  49. package/src/format.ts +15 -0
  50. package/src/index.ts +24 -1
  51. package/src/layout.manifest.json +2 -9
  52. package/src/layout.manifest.test.ts +4 -2
  53. package/src/layout.test.tsx +74 -6
  54. package/src/layout.tsx +85 -25
  55. package/src/layoutContract.test.tsx +239 -1
  56. package/src/layoutContract.ts +131 -5
  57. package/src/layoutDeclaration.test.ts +31 -10
  58. package/src/layoutDeclaration.ts +9 -4
  59. package/src/locale.tsx +28 -3
  60. package/src/markers.test.ts +70 -2
  61. package/src/router.test.tsx +15 -3
  62. package/src/router.tsx +11 -2
  63. package/src/ssr.test.tsx +18 -0
  64. package/src/styles.test.ts +288 -190
  65. package/src/styles.ts +1379 -361
  66. package/src/theme.test.tsx +44 -18
  67. package/src/theme.themes.test.ts +22 -2
  68. package/src/theme.tsx +22 -14
  69. package/src/themes.ts +39 -4
  70. package/src/tokens.guard.test.ts +13 -15
  71. package/src/typography.tsx +5 -1
  72. package/src/ui/Alert.test.tsx +24 -1
  73. package/src/ui/Alert.tsx +10 -1
  74. package/src/ui/Card.tsx +5 -4
  75. package/src/ui/Tabs.test.tsx +44 -0
  76. package/src/ui/Tabs.tsx +5 -2
  77. package/src/ui/Tooltip.test.tsx +171 -2
  78. package/src/ui/Tooltip.tsx +144 -8
  79. package/src/ui/controlMessages.tsx +20 -7
  80. package/src/uiText.test.tsx +16 -0
  81. package/src/uiText.tsx +105 -7
  82. package/src/viewerTimeZone.ts +15 -0
@@ -0,0 +1,376 @@
1
+ // @vitest-environment jsdom
2
+ import { cleanup, render, screen } 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 { HubCard, HubPage } from "./HubPage";
9
+ import { LOCALE_EN, LOCALE_NL, LocaleProvider } from "./locale";
10
+ import { Stat, StatGroup } from "./Stat";
11
+ import { TERP_STYLES_CSS } from "./styles";
12
+
13
+ afterEach(() => {
14
+ cleanup();
15
+ window.localStorage.clear();
16
+ });
17
+
18
+ /** Render under a fixed app locale, so a formatting assertion never depends on the runner's. */
19
+ function renderIn(locale: "nl" | "en", node: ReactNode) {
20
+ return render(
21
+ <LocaleProvider locales={{ nl: LOCALE_NL, en: LOCALE_EN }} defaultLocale={locale}>
22
+ {node}
23
+ </LocaleProvider>,
24
+ );
25
+ }
26
+
27
+ const part = (marker: string) => document.querySelector(`[data-terp="${marker}"]`);
28
+
29
+ describe("Stat's figure", () => {
30
+ it("prints its label and a numeric value in the app's locale", () => {
31
+ renderIn("nl", <Stat label="Open orders" value={1284.5} />);
32
+ expect(part("stat-label")!.textContent).toBe("Open orders");
33
+ expect(part("stat-value")!.textContent).toBe("1.284,5");
34
+ cleanup();
35
+ renderIn("en", <Stat label="Open orders" value={1284.5} />);
36
+ expect(part("stat-value")!.textContent).toBe("1,284.5");
37
+ });
38
+
39
+ it("formats with the options it is given", () => {
40
+ renderIn("en", <Stat label="Spend" value={842} format={{ style: "currency", currency: "EUR" }} />);
41
+ expect(part("stat-value")!.textContent).toBe("€842.00");
42
+ });
43
+
44
+ it("prints a string as written, and the dash for a figure not known yet", () => {
45
+ renderIn("en", <Stat label="Uptime" value="3 d 4 h" />);
46
+ expect(part("stat-value")!.textContent).toBe("3 d 4 h");
47
+ cleanup();
48
+ for (const value of [null, undefined, ""] as const) {
49
+ renderIn("en", <Stat label="Uptime" value={value} />);
50
+ expect(part("stat-value")!.textContent).toBe("—");
51
+ cleanup();
52
+ }
53
+ });
54
+
55
+ it("stamps the headline only when asked", () => {
56
+ // The page frame counts [data-terp="stat"][data-headline]; a stamp on every figure would
57
+ // make every page with two figures fail closed.
58
+ renderIn("en", <Stat label="Revenue" value={1} />);
59
+ expect(part("stat")).not.toHaveAttribute("data-headline");
60
+ cleanup();
61
+ renderIn("en", <Stat label="Revenue" value={1} headline />);
62
+ expect(part("stat")).toHaveAttribute("data-headline", "true");
63
+ });
64
+
65
+ it("prints a caption, and renders no empty line for an absent one", () => {
66
+ renderIn("en", <Stat label="Spend" value={842} caption="of 1,200 budgeted" />);
67
+ expect(part("stat-caption")!.textContent).toBe("of 1,200 budgeted");
68
+ cleanup();
69
+ for (const caption of [undefined, null, false, ""] as const) {
70
+ renderIn("en", <Stat label="Spend" value={842} caption={caption} />);
71
+ expect(part("stat-caption")).toBeNull();
72
+ cleanup();
73
+ }
74
+ });
75
+ });
76
+
77
+ describe("Stat's delta", () => {
78
+ it("prints the change with its sign, in the change's own format", () => {
79
+ renderIn("en", (
80
+ <Stat
81
+ label="Orders"
82
+ value={1284}
83
+ delta={{ value: 0.12, sentiment: "positive", format: { style: "percent" } }}
84
+ />
85
+ ));
86
+ // The sign is printed even when the caller's format says nothing about it.
87
+ expect(part("stat-change")!.textContent).toBe("+12% favourable");
88
+ cleanup();
89
+ renderIn("en", <Stat label="Orders" value={1284} delta={{ value: -3, sentiment: "negative" }} />);
90
+ expect(part("stat-change")!.textContent).toBe("-3 unfavourable");
91
+ });
92
+
93
+ it("colours by the declared sentiment, never by the sign", () => {
94
+ // More rejections is up and bad: the caller says so, and the pill follows the caller.
95
+ renderIn("en", (
96
+ <Stat label="Rejections" value={40} delta={{ value: 6, sentiment: "negative" }} />
97
+ ));
98
+ expect(part("stat-change")).toHaveAttribute("data-sentiment", "negative");
99
+ expect(part("stat-arrow")!.querySelector("path")!.getAttribute("d")).toBe("M6 2 L11 10 L1 10 Z");
100
+ });
101
+
102
+ it("points the arrow by the sign, and draws a bar for no change", () => {
103
+ const arrows: Record<string, string> = {};
104
+ for (const [name, value] of [["up", 2], ["down", -2], ["flat", 0]] as const) {
105
+ renderIn("en", <Stat label="Runs" value={9} delta={{ value, sentiment: "neutral" }} />);
106
+ arrows[name] = part("stat-arrow")!.querySelector("path")!.getAttribute("d")!;
107
+ cleanup();
108
+ }
109
+ expect(new Set(Object.values(arrows)).size).toBe(3);
110
+ expect(arrows.flat).toBe("M1 5 H11 V7 H1 Z");
111
+ });
112
+
113
+ it("says the sentiment as a word, and says nothing for a neutral change", () => {
114
+ // A tone is always also a word (ADR 0169 §7): the pill's colour is read out after the
115
+ // change. Mutation: drop the word, and a screen reader hears "+12%" with no judgement.
116
+ renderIn("nl", (
117
+ <Stat label="Orders" value={1284} delta={{ value: 12, sentiment: "positive" }} />
118
+ ));
119
+ expect(part("stat-sentiment")!.textContent).toBe(" gunstig");
120
+ cleanup();
121
+ renderIn("nl", <Stat label="Orders" value={1284} delta={{ value: 12, sentiment: "negative" }} />);
122
+ expect(part("stat-sentiment")!.textContent).toBe(" ongunstig");
123
+ cleanup();
124
+ renderIn("nl", <Stat label="Orders" value={1284} delta={{ value: 12, sentiment: "neutral" }} />);
125
+ expect(part("stat-sentiment")).toBeNull();
126
+ });
127
+
128
+ it("follows the change as it prints: a change that rounds to zero is no change", () => {
129
+ // 0.004 as a whole percentage prints "0%"; an up arrow, a green pill and "favourable" beside
130
+ // "0%" contradicted the number they decorate. Mutation: take the direction from the raw
131
+ // value again, and the arrow points up.
132
+ renderIn("en", (
133
+ <Stat
134
+ label="Orders"
135
+ value={1}
136
+ delta={{ value: 0.004, sentiment: "positive", format: { style: "percent" } }}
137
+ />
138
+ ));
139
+ expect(part("stat-change")!.textContent).toBe("0%");
140
+ expect(part("stat-change")).toHaveAttribute("data-sentiment", "neutral");
141
+ expect(part("stat-arrow")!.querySelector("path")!.getAttribute("d")).toBe("M1 5 H11 V7 H1 Z");
142
+ });
143
+
144
+ it("keeps the sign over a format that asks to hide it", () => {
145
+ // Mutation: spread the caller's format last, and "never" wins.
146
+ renderIn("en", (
147
+ <Stat label="Orders" value={1} delta={{ value: 5, sentiment: "positive", format: { signDisplay: "never" } }} />
148
+ ));
149
+ expect(part("stat-change")!.textContent).toBe("+5 favourable");
150
+ });
151
+
152
+ it("hides the arrow from assistive technology, which hears the sign instead", () => {
153
+ renderIn("en", <Stat label="Orders" value={1} delta={{ value: 1, sentiment: "positive" }} />);
154
+ expect(part("stat-arrow")).toHaveAttribute("aria-hidden", "true");
155
+ });
156
+
157
+ it("prints what the change is measured against", () => {
158
+ renderIn("en", (
159
+ <Stat
160
+ label="Orders"
161
+ value={1284}
162
+ delta={{ value: 12, sentiment: "positive", label: "vs last week" }}
163
+ />
164
+ ));
165
+ expect(part("stat-delta-label")!.textContent).toBe("vs last week");
166
+ });
167
+ });
168
+
169
+ describe("Stat's trend", () => {
170
+ const WEEK = [
171
+ { label: "Mon", value: 10 },
172
+ { label: "Tue", value: 30 },
173
+ { label: "Wed", value: 20 },
174
+ ];
175
+
176
+ it("draws the series as SVG geometry, highest at the top", () => {
177
+ renderIn("en", <Stat label="Runs" value={20} trend={WEEK} />);
178
+ const line = part("stat-trend-line")!;
179
+ // x spreads the points across 100, y maps 10..30 onto 22..2 (the canvas less its inset).
180
+ expect(line.getAttribute("points")).toBe("0,22 50,2 100,12");
181
+ expect(part("stat-trend-area")!.getAttribute("points")).toBe("0,24 0,22 50,2 100,12 100,24");
182
+ expect(part("stat-trend-chart")).toHaveAttribute("aria-hidden", "true");
183
+ // Geometry, not style: nothing here is an inline style (ADR 0169 §7).
184
+ expect(part("stat")!.querySelector("[style]")).toBeNull();
185
+ });
186
+
187
+ it("draws a flat series through the middle rather than along the floor", () => {
188
+ renderIn("en", <Stat label="Runs" value={5} trend={[{ label: "a", value: 5 }, { label: "b", value: 5 }]} />);
189
+ expect(part("stat-trend-line")!.getAttribute("points")).toBe("0,12 100,12");
190
+ });
191
+
192
+ it("reads every point out, label and value, in the app's locale", () => {
193
+ // ADR 0158's data alternative, as text because a figure is phrasing content throughout.
194
+ // Mutation: drop the text, and the sparkline is a picture with nothing behind it.
195
+ renderIn("nl", (
196
+ <Stat
197
+ label="Omzet"
198
+ value={1500}
199
+ trend={[{ label: "jan", value: 1200.5 }, { label: "feb", value: 1500 }]}
200
+ />
201
+ ));
202
+ expect(part("stat-trend-data")!.textContent).toBe("Verloop: jan: 1.200,5 en feb: 1.500");
203
+ });
204
+
205
+ it("reads a series out as a sentence in English too, each point's label set off from its value", () => {
206
+ // Intl's narrow unit list joins English with bare spaces, so "Mon 10 Tue 30 Wed 20" read as
207
+ // one run of words and numbers. Mutation: go back to the narrow unit list, and this fails.
208
+ renderIn("en", <Stat label="Runs" value={20} trend={WEEK} />);
209
+ expect(part("stat-trend-data")!.textContent).toBe("Over time: Mon: 10, Tue: 30, and Wed: 20");
210
+ });
211
+
212
+ it("reads out a single point without drawing a line, or a block, for it", () => {
213
+ // As the only child of the trend's own block, the hidden text left that block's margin
214
+ // behind as an empty line. Mutation: keep the block for one point, and it is found.
215
+ renderIn("en", <Stat label="Runs" value={5} trend={[{ label: "Mon", value: 5 }]} />);
216
+ expect(part("stat-trend-chart")).toBeNull();
217
+ expect(part("stat-trend")).toBeNull();
218
+ expect(part("stat-trend-data")!.textContent).toBe("Over time: Mon: 5");
219
+ });
220
+
221
+ it("draws only the points that are numbers, and prints the dash for one that is not", () => {
222
+ // A NaN in the points attribute is a console error and no line at all.
223
+ // Mutation: draw every value, and the points carry a NaN.
224
+ renderIn("en", (
225
+ <Stat
226
+ label="Runs"
227
+ value={3}
228
+ trend={[
229
+ { label: "a", value: 1 },
230
+ { label: "b", value: Number.NaN },
231
+ { label: "c", value: 3 },
232
+ ]}
233
+ />
234
+ ));
235
+ expect(part("stat-trend-line")!.getAttribute("points")).toBe("0,22 100,2");
236
+ expect(part("stat-trend-data")!.textContent).toBe("Over time: a: 1, b: —, and c: 3");
237
+ });
238
+
239
+ it("renders no trend at all for an empty series", () => {
240
+ renderIn("en", <Stat label="Runs" value={5} trend={[]} />);
241
+ expect(part("stat-trend")).toBeNull();
242
+ });
243
+ });
244
+
245
+ describe("Stat's target", () => {
246
+ it("draws the figure against its range as a Meter named by the figure's label", () => {
247
+ renderIn("en", <Stat label="Spend" value={600} target={{ max: 1200, high: 1000, optimum: 0 }} />);
248
+ expect(screen.getByRole("meter", { name: "Spend" })).toHaveAttribute("aria-valuetext", "50%");
249
+ });
250
+
251
+ it("draws no meter for a figure that is not a number", () => {
252
+ renderIn("en", <Stat label="Spend" value={null} target={{ max: 1200 }} />);
253
+ expect(screen.queryByRole("meter")).toBeNull();
254
+ });
255
+ });
256
+
257
+ describe("Stat as phrasing content", () => {
258
+ it("renders only phrasing elements, so it is valid inside a HubCard's link", () => {
259
+ // A HubCard is one link whose body is spans; a block element inside it is invalid HTML.
260
+ // Mutation: render the root as a div, and this fails.
261
+ renderIn("en", (
262
+ <Stat
263
+ label="Spend"
264
+ value={600}
265
+ delta={{ value: 1, sentiment: "positive", label: "vs last week" }}
266
+ trend={[{ label: "a", value: 1 }, { label: "b", value: 2 }]}
267
+ target={{ max: 1200 }}
268
+ caption="of 1,200"
269
+ />
270
+ ));
271
+ const PHRASING = new Set(["span", "svg", "path", "polyline", "polygon", "meter"]);
272
+ const tags = [part("stat")!, ...part("stat")!.querySelectorAll("*")].map((element) =>
273
+ element.tagName.toLowerCase(),
274
+ );
275
+ expect(tags.filter((tag) => !PHRASING.has(tag))).toEqual([]);
276
+ });
277
+
278
+ it("sits in a HubCard's stat row and names the card's link with the figure", () => {
279
+ renderIn("en", (
280
+ <HubPage title="Admin">
281
+ <HubCard to="/users" title="Users" stat={<Stat label="Total" value={1420} />} />
282
+ </HubPage>
283
+ ));
284
+ expect(screen.getByRole("link").textContent).toContain("Total1,420");
285
+ });
286
+ });
287
+
288
+ describe("StatGroup", () => {
289
+ it("renders its figures in one group", () => {
290
+ renderIn("en", (
291
+ <StatGroup>
292
+ <Stat label="A" value={1} />
293
+ <Stat label="B" value={2} />
294
+ </StatGroup>
295
+ ));
296
+ expect(part("stat-group")!.querySelectorAll(':scope > [data-terp="stat"]')).toHaveLength(2);
297
+ });
298
+ });
299
+
300
+ describe("Stat's colours are declared pairings", () => {
301
+ /** The declarations of the one rule whose selector is exactly *selector*. */
302
+ function rule(selector: string): string {
303
+ const escaped = selector.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
304
+ const match = new RegExp(`(?:^|\\n)${escaped} \\{([^}]*)\\}`).exec(TERP_STYLES_CSS);
305
+ expect(match, `no rule for ${selector}`).not.toBeNull();
306
+ return match![1]!;
307
+ }
308
+ const declared = (fg: string, bg: string) =>
309
+ [...tokenPairs.textPairs, ...tokenPairs.nonTextPairs].some(
310
+ (pair) => "fg" in pair && pair.fg === fg && pair.bg === bg,
311
+ );
312
+
313
+ it("draws each delta pill in a pairing the contrast gate holds", () => {
314
+ // The pill carries its own fill so its contrast travels with it onto a band, a card or the
315
+ // headline. Mutation: colour the positive pill's ink with the accent, and no pairing
316
+ // holds it.
317
+ for (const sentiment of ["positive", "negative", "neutral"]) {
318
+ const body = rule(`[data-terp="stat-change"][data-sentiment="${sentiment}"]`);
319
+ const fg = /(?<![-\w])color: var\((--[a-z0-9-]+)\)/.exec(body)![1]!;
320
+ const bg = /(?<![-\w])background: var\((--[a-z0-9-]+)\)/.exec(body)![1]!;
321
+ expect(declared(fg, bg), `${sentiment}: ${fg} on ${bg}`).toBe(true);
322
+ }
323
+ });
324
+
325
+ it("fills the headline with the brand and its contrast ink, the primary button's pairing", () => {
326
+ const body = rule('[data-terp="stat"][data-headline]');
327
+ const fg = /(?<![-\w])color: var\((--[a-z0-9-]+)\)/.exec(body)![1]!;
328
+ const bg = /(?<![-\w])background: var\((--[a-z0-9-]+)\)/.exec(body)![1]!;
329
+ expect(declared(fg, bg)).toBe(true);
330
+ });
331
+
332
+ it("writes the summary band's text in inks declared against its fill", () => {
333
+ const body = rule('[data-terp="page-summary"]');
334
+ const bg = /(?<![-\w])background: var\((--[a-z0-9-]+)\)/.exec(body)![1]!;
335
+ expect(bg).toBe("--color-bg-summary");
336
+ // The band's own ink, and the two a figure writes on it.
337
+ for (const fg of ["--color-fg-default", "--color-fg-muted", "--color-fg-accent"]) {
338
+ expect(declared(fg, bg), `${fg} on ${bg}`).toBe(true);
339
+ }
340
+ });
341
+ });
342
+
343
+ describe("a figure with no value", () => {
344
+ it("marks its dash as empty, and a zero as a value", () => {
345
+ const { container } = renderIn(
346
+ "en",
347
+ <>
348
+ <Stat label="Nothing" value={null} />
349
+ <Stat label="Blank" value="" />
350
+ <Stat label="Zero" value={0} />
351
+ </>,
352
+ );
353
+ const values = [...container.querySelectorAll('[data-terp="stat-value"]')].map((value) => [
354
+ value.textContent,
355
+ value.getAttribute("data-empty"),
356
+ ]);
357
+ // Mutations: no mark, so the dash keeps the value's ink; or a zero taken for nothing.
358
+ expect(values).toEqual([
359
+ ["—", "true"],
360
+ ["—", "true"],
361
+ ["0", null],
362
+ ]);
363
+ });
364
+
365
+ it("draws the dash in the quiet ink, and leaves a headline's ink alone", () => {
366
+ const rule =
367
+ /\[data-terp="stat"\]:not\(\[data-headline\]\) \[data-terp="stat-value"\]\[data-empty\] \{([^}]*)\}/.exec(
368
+ TERP_STYLES_CSS,
369
+ );
370
+ // Mutation: the rule without :not([data-headline]), which would put the subtle ink on the
371
+ // headline's brand fill, the one surface it was never measured against.
372
+ expect(rule, "an empty value has a rule of its own, outside a headline").not.toBeNull();
373
+ expect(rule![1]).toContain("color: var(--color-fg-subtle)");
374
+ expect(rule![1]).toContain("font-weight: var(--font-weight-normal)");
375
+ });
376
+ });