@terpjs/react-core 0.27.0 → 0.29.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 +31 -7
- package/package.json +4 -3
- package/src/Field.test.tsx +95 -1
- package/src/Field.tsx +70 -1
- package/src/LoginView.test.tsx +33 -0
- package/src/LoginView.tsx +36 -23
- package/src/Meter.test.tsx +304 -0
- package/src/Meter.tsx +159 -0
- package/src/QuietActions.test.tsx +118 -0
- package/src/QuietActions.tsx +75 -0
- package/src/UserMenu.test.tsx +23 -0
- package/src/UserMenu.tsx +9 -1
- package/src/admin/AccessAdmin.tsx +6 -2
- package/src/admin/AuditLogAdmin.tsx +1 -1
- package/src/admin/access.test.tsx +2 -1
- package/src/admin/admin.test.tsx +57 -5
- package/src/admin/module.tsx +9 -6
- package/src/bootstrap.test.tsx +1 -1
- package/src/bootstrap.tsx +2 -1
- package/src/dataview/DataView.test.tsx +99 -0
- package/src/dataview/DataViewPagination.tsx +2 -2
- package/src/dataview/DataViewToolbar.tsx +2 -2
- package/src/dataview/README.md +5 -3
- package/src/dataview/index.ts +1 -1
- package/src/dataview/internal.tsx +102 -21
- package/src/dataview/types.ts +11 -37
- package/src/errorMessages.test.tsx +21 -0
- package/src/errorMessages.tsx +33 -19
- package/src/index.ts +9 -3
- package/src/locale.test.tsx +164 -4
- package/src/locale.tsx +105 -10
- package/src/markers.test.ts +33 -11
- package/src/ssr.test.tsx +18 -0
- package/src/styles.test.ts +265 -14
- package/src/styles.ts +424 -49
- package/src/testing.test.tsx +162 -0
- package/src/testing.ts +84 -0
- package/src/tokens.guard.test.ts +11 -6
- package/src/ui/controlMessages.tsx +27 -14
- package/src/uiText.literals.test.ts +19 -6
- package/src/uiText.test.tsx +143 -2
- package/src/uiText.tsx +287 -15
- package/vitest.setup.ts +7 -20
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import type { ReactNode } from "react";
|
|
5
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
|
|
7
|
+
import { ConfirmDialog } from "./ConfirmDialog";
|
|
8
|
+
import { installDialogPolyfill } from "./testing";
|
|
9
|
+
import { Combobox } from "./ui/Combobox";
|
|
10
|
+
|
|
11
|
+
afterEach(cleanup);
|
|
12
|
+
|
|
13
|
+
// The setup file has already installed the polyfill here — the same call a generated app's
|
|
14
|
+
// setup file makes — so these tests drive it the way an app's test would: with key events.
|
|
15
|
+
// ConfirmDialog's own tests (feedback.test.tsx) dispatch `cancel` by hand to reach its
|
|
16
|
+
// handler; whether an Escape ever produces that event is the step this file exists to hold.
|
|
17
|
+
|
|
18
|
+
function OwnedDialog(props: {
|
|
19
|
+
onOpenChange: (open: boolean) => void;
|
|
20
|
+
isPending?: boolean;
|
|
21
|
+
description?: ReactNode;
|
|
22
|
+
}) {
|
|
23
|
+
const [open, setOpen] = useState(true);
|
|
24
|
+
return (
|
|
25
|
+
<ConfirmDialog
|
|
26
|
+
open={open}
|
|
27
|
+
onOpenChange={(next) => {
|
|
28
|
+
props.onOpenChange(next);
|
|
29
|
+
setOpen(next);
|
|
30
|
+
}}
|
|
31
|
+
onConfirm={() => {}}
|
|
32
|
+
title="Delete this record?"
|
|
33
|
+
description={props.description}
|
|
34
|
+
isPending={props.isPending}
|
|
35
|
+
/>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A bare dialog in the document, removed again by the caller. */
|
|
40
|
+
function bareDialog(): HTMLDialogElement {
|
|
41
|
+
return document.body.appendChild(document.createElement("dialog"));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe("installDialogPolyfill", () => {
|
|
45
|
+
it("dismisses a ConfirmDialog on Escape, through the cancel event a browser fires", () => {
|
|
46
|
+
const onOpenChange = vi.fn();
|
|
47
|
+
render(<OwnedDialog onOpenChange={onOpenChange} />);
|
|
48
|
+
|
|
49
|
+
fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" });
|
|
50
|
+
|
|
51
|
+
expect(onOpenChange).toHaveBeenCalledWith(false);
|
|
52
|
+
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("keeps a pending ConfirmDialog open on Escape, because it cancels that event", () => {
|
|
56
|
+
const onOpenChange = vi.fn();
|
|
57
|
+
render(<OwnedDialog onOpenChange={onOpenChange} isPending />);
|
|
58
|
+
|
|
59
|
+
fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" });
|
|
60
|
+
|
|
61
|
+
// Closing regardless of the cancel would hide the dialog while its owner still holds it
|
|
62
|
+
// open — a test of the pending guard that could not fail.
|
|
63
|
+
expect(onOpenChange).not.toHaveBeenCalled();
|
|
64
|
+
expect(screen.getByRole("dialog")).toBeInTheDocument();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("leaves the dialog open when a control inside it takes the Escape", () => {
|
|
68
|
+
// A Combobox cancels its own Escape to close its option list. That is not a close request
|
|
69
|
+
// in a browser (measured in Chromium), so the first Escape closes the list and only the
|
|
70
|
+
// second reaches the dialog.
|
|
71
|
+
const onOpenChange = vi.fn();
|
|
72
|
+
render(
|
|
73
|
+
<OwnedDialog
|
|
74
|
+
onOpenChange={onOpenChange}
|
|
75
|
+
description={
|
|
76
|
+
<Combobox
|
|
77
|
+
aria-label="Group"
|
|
78
|
+
options={[
|
|
79
|
+
{ value: "a", label: "Alpha" },
|
|
80
|
+
{ value: "b", label: "Beta" },
|
|
81
|
+
]}
|
|
82
|
+
/>
|
|
83
|
+
}
|
|
84
|
+
/>,
|
|
85
|
+
);
|
|
86
|
+
const input = screen.getByRole("combobox", { name: /Group/ });
|
|
87
|
+
fireEvent.focus(input);
|
|
88
|
+
expect(input).toHaveAttribute("aria-expanded", "true");
|
|
89
|
+
|
|
90
|
+
fireEvent.keyDown(input, { key: "Escape" });
|
|
91
|
+
expect(input).toHaveAttribute("aria-expanded", "false");
|
|
92
|
+
expect(onOpenChange).not.toHaveBeenCalled();
|
|
93
|
+
expect(screen.getByRole("dialog")).toBeInTheDocument();
|
|
94
|
+
|
|
95
|
+
fireEvent.keyDown(input, { key: "Escape" });
|
|
96
|
+
expect(onOpenChange).toHaveBeenCalledWith(false);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("closes a modal nobody handles on Escape, and on no other key", () => {
|
|
100
|
+
const dialog = bareDialog();
|
|
101
|
+
try {
|
|
102
|
+
const closed = vi.fn();
|
|
103
|
+
dialog.addEventListener("close", closed);
|
|
104
|
+
dialog.showModal();
|
|
105
|
+
|
|
106
|
+
fireEvent.keyDown(dialog, { key: "Enter" });
|
|
107
|
+
expect(dialog.open).toBe(true);
|
|
108
|
+
|
|
109
|
+
fireEvent.keyDown(dialog, { key: "Escape" });
|
|
110
|
+
expect(dialog.open).toBe(false);
|
|
111
|
+
expect(closed).toHaveBeenCalledTimes(1);
|
|
112
|
+
} finally {
|
|
113
|
+
dialog.remove();
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("asks only the topmost modal, and skips one that has left the document", () => {
|
|
118
|
+
const lower = bareDialog();
|
|
119
|
+
const upper = bareDialog();
|
|
120
|
+
try {
|
|
121
|
+
const asked: string[] = [];
|
|
122
|
+
lower.addEventListener("cancel", () => asked.push("lower"));
|
|
123
|
+
upper.addEventListener("cancel", () => asked.push("upper"));
|
|
124
|
+
lower.showModal();
|
|
125
|
+
upper.showModal();
|
|
126
|
+
|
|
127
|
+
fireEvent.keyDown(document.body, { key: "Escape" });
|
|
128
|
+
expect(asked).toEqual(["upper"]);
|
|
129
|
+
expect(lower.open).toBe(true);
|
|
130
|
+
|
|
131
|
+
// Opened again and then removed while open. A browser takes it out of the top layer with
|
|
132
|
+
// the removal (measured in Chromium), so the next Escape belongs to the one beneath.
|
|
133
|
+
upper.showModal();
|
|
134
|
+
upper.remove();
|
|
135
|
+
fireEvent.keyDown(document.body, { key: "Escape" });
|
|
136
|
+
expect(asked).toEqual(["upper", "lower"]);
|
|
137
|
+
expect(lower.open).toBe(false);
|
|
138
|
+
} finally {
|
|
139
|
+
lower.remove();
|
|
140
|
+
upper.remove();
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("stands aside where the environment already implements showModal", () => {
|
|
145
|
+
const proto = HTMLDialogElement.prototype;
|
|
146
|
+
const installed = { showModal: proto.showModal, close: proto.close };
|
|
147
|
+
const native = {
|
|
148
|
+
showModal: function showModal() {},
|
|
149
|
+
close: function close() {},
|
|
150
|
+
};
|
|
151
|
+
proto.showModal = native.showModal;
|
|
152
|
+
proto.close = native.close;
|
|
153
|
+
try {
|
|
154
|
+
installDialogPolyfill();
|
|
155
|
+
expect(proto.showModal).toBe(native.showModal);
|
|
156
|
+
expect(proto.close).toBe(native.close);
|
|
157
|
+
} finally {
|
|
158
|
+
proto.showModal = installed.showModal;
|
|
159
|
+
proto.close = installed.close;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
});
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit-test support for the components in this package, published at the
|
|
3
|
+
* `@terpjs/react-core/testing` subpath rather than the package root, so none of it arrives in
|
|
4
|
+
* an app's production code through the import every screen already has (ADR 0155). Its two
|
|
5
|
+
* callers are the vitest setup files: this package's own, and the one every generated app
|
|
6
|
+
* ships.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Give jsdom enough of the native `<dialog>` modal API for `ConfirmDialog` to open, confirm
|
|
11
|
+
* and dismiss under a unit test the way it does in a browser.
|
|
12
|
+
*
|
|
13
|
+
* jsdom implements the element and its `open` attribute but not `showModal()` or `close()`,
|
|
14
|
+
* and `ConfirmDialog` — the one dialog an app may render — calls both, so without this any
|
|
15
|
+
* test that opens one throws `showModal is not a function`.
|
|
16
|
+
*
|
|
17
|
+
* What it reproduces, each measured against Chromium's own implementation:
|
|
18
|
+
*
|
|
19
|
+
* - `showModal()` sets `open`; `close()` clears it and fires `close`.
|
|
20
|
+
* - **Escape fires a cancelable `cancel` at the topmost open modal, and closes it unless that
|
|
21
|
+
* event is cancelled.** This is the one that matters: `ConfirmDialog`'s Escape handling
|
|
22
|
+
* lives in its `cancel` handler, which cancels the event and asks its owner to close —
|
|
23
|
+
* or, while `isPending`, cancels it and does nothing. Without this, a test pressing
|
|
24
|
+
* Escape would see nothing happen, and a test of the pending state would pass whether
|
|
25
|
+
* or not the guard worked.
|
|
26
|
+
* - An Escape `keydown` whose default was prevented is not a close request — so a
|
|
27
|
+
* `Combobox` inside the dialog, which cancels its own Escape to close its option list,
|
|
28
|
+
* closes that list and leaves the dialog open, as it does in a browser.
|
|
29
|
+
* - Only the topmost modal is asked, and a modal removed from the document while open has
|
|
30
|
+
* left the top layer, so it is skipped rather than handed an Escape meant for the one
|
|
31
|
+
* beneath it.
|
|
32
|
+
*
|
|
33
|
+
* What it does NOT reproduce, so a test must not rely on it: focus (a browser moves focus
|
|
34
|
+
* into the dialog on open and back to the opener on close), inertness of the page behind a
|
|
35
|
+
* modal, the backdrop, and the event timing (`close` fires synchronously here, a task later
|
|
36
|
+
* in a browser). An Escape whose propagation is stopped before it reaches `window` is also
|
|
37
|
+
* missed here, where a browser would still close, and `close()` on a dialog that is not open
|
|
38
|
+
* still fires `close` here, where a browser does nothing. Those belong to the Playwright
|
|
39
|
+
* suite, which runs a real browser.
|
|
40
|
+
*
|
|
41
|
+
* A no-op wherever `HTMLDialogElement` is absent (a node-environment test file sharing the
|
|
42
|
+
* setup file) or already has `showModal` — an environment with a real one owns the whole
|
|
43
|
+
* modal contract, Escape included, and a second implementation beside it would hand every
|
|
44
|
+
* Escape to the dialog twice. Calling it again is therefore harmless.
|
|
45
|
+
*/
|
|
46
|
+
export function installDialogPolyfill(): void {
|
|
47
|
+
if (typeof HTMLDialogElement === "undefined") {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const proto = HTMLDialogElement.prototype as {
|
|
51
|
+
showModal?: (this: HTMLDialogElement) => void;
|
|
52
|
+
close?: (this: HTMLDialogElement) => void;
|
|
53
|
+
};
|
|
54
|
+
if (typeof proto.showModal === "function") {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// The top layer, in the order its modals were opened: the last one still in the document
|
|
59
|
+
// is the one Escape reaches. A Set keeps insertion order, and forgetting a dialog that was
|
|
60
|
+
// never shown is a no-op rather than a case to guard.
|
|
61
|
+
const topLayer = new Set<HTMLDialogElement>();
|
|
62
|
+
|
|
63
|
+
proto.showModal = function showModal() {
|
|
64
|
+
this.setAttribute("open", "");
|
|
65
|
+
topLayer.add(this);
|
|
66
|
+
};
|
|
67
|
+
proto.close = function close() {
|
|
68
|
+
this.removeAttribute("open");
|
|
69
|
+
topLayer.delete(this);
|
|
70
|
+
this.dispatchEvent(new Event("close"));
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// Bubble phase on `window`, the last stop before a browser's own default handling, so a
|
|
74
|
+
// component inside the dialog that cancels its Escape has already had the chance to.
|
|
75
|
+
window.addEventListener("keydown", (event) => {
|
|
76
|
+
if (event.key !== "Escape" || event.defaultPrevented) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const topmost = [...topLayer].filter((dialog) => dialog.isConnected).at(-1);
|
|
80
|
+
if (topmost?.dispatchEvent(new Event("cancel", { cancelable: true }))) {
|
|
81
|
+
topmost.close();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
package/src/tokens.guard.test.ts
CHANGED
|
@@ -103,15 +103,17 @@ const UNREAD_TOKENS: Record<string, string[]> = {
|
|
|
103
103
|
// still catches the sixteenth. `--color-sidebar-` stays above because it makes a narrower
|
|
104
104
|
// claim worth keeping: that family is read in full.
|
|
105
105
|
//
|
|
106
|
-
// None of these
|
|
107
|
-
// ahead of its consumers —
|
|
106
|
+
// None of these fourteen is a defect on its own. They are a published vocabulary that shipped
|
|
107
|
+
// ahead of its consumers — three surface tokens, three borders, one interactive state, a
|
|
108
108
|
// five-step chart ramp and two neutrals — and the point of booking them is that the list can
|
|
109
|
-
// only shrink from here, so wiring one is visible and adding a
|
|
109
|
+
// only shrink from here, so wiring one is visible and adding a fifteenth has to argue.
|
|
110
|
+
//
|
|
111
|
+
// --color-bg-surface came off it when the surface model went back to filling its in-flow
|
|
112
|
+
// blocks: card, hubcard-body, profile-card, resource-list-row, empty-state, dataview-card and
|
|
113
|
+
// the full DataView's frame all name it now. That is the shrink this list was booked for.
|
|
110
114
|
"--color-": [
|
|
111
115
|
"--color-bg-canvas",
|
|
112
|
-
"--color-bg-inset",
|
|
113
116
|
"--color-bg-raised",
|
|
114
|
-
"--color-bg-surface",
|
|
115
117
|
"--color-border-default",
|
|
116
118
|
"--color-border-strong",
|
|
117
119
|
"--color-border-subtle",
|
|
@@ -145,7 +147,10 @@ const BARE_TYPE_LITERALS: Record<string, Record<string, number>> = {
|
|
|
145
147
|
// naming a token would pin it to whatever that header uses TODAY. The UA stylesheet resets
|
|
146
148
|
// letter-spacing on form controls, so inheritance has to be asked for explicitly — the
|
|
147
149
|
// header and its button were rendering two different treatments side by side until it was.
|
|
148
|
-
|
|
150
|
+
//
|
|
151
|
+
// One `0` retired when page-title took --font-letter-spacing-tight: the leaf is set at lg now,
|
|
152
|
+
// and the tight step is worth a third of a pixel there where at sm it was worth nothing.
|
|
153
|
+
"letter-spacing": { "0": 3, "0.04em": 1, "0.06em": 1, inherit: 1 },
|
|
149
154
|
};
|
|
150
155
|
|
|
151
156
|
describe("design tokens", () => {
|
|
@@ -47,20 +47,33 @@ export function useControlMessages(
|
|
|
47
47
|
return {
|
|
48
48
|
hasError,
|
|
49
49
|
describedBy,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
50
|
+
// One box around both, and it is rendered ONLY when there is something to say.
|
|
51
|
+
// That condition is the whole of why this is safe: an envelope emitted
|
|
52
|
+
// unconditionally would add a grid row and a gap to every field in every app that
|
|
53
|
+
// carries neither a hint nor an error, which is most of them, and the change would
|
|
54
|
+
// arrive as a few pixels of drift on screens nobody touched. With the guard, a field
|
|
55
|
+
// with no messages renders exactly the DOM it rendered before.
|
|
56
|
+
//
|
|
57
|
+
// What the box buys is FieldRow. There, a field spans three shared lines -- label,
|
|
58
|
+
// control, messages -- and two loose spans would take two of them, so a field with
|
|
59
|
+
// both a hint and an error would push its own messages line down and every other
|
|
60
|
+
// field's with it. As one box they occupy the third line together and stack inside
|
|
61
|
+
// it, which is also the arrangement they already had.
|
|
62
|
+
messages:
|
|
63
|
+
hint === undefined && !hasError ? null : (
|
|
64
|
+
<div data-terp="field-messages">
|
|
65
|
+
{hint !== undefined && (
|
|
66
|
+
<span id={hintId} data-terp="field-hint">
|
|
67
|
+
{resolve(hint)}
|
|
68
|
+
</span>
|
|
69
|
+
)}
|
|
70
|
+
{hasError && (
|
|
71
|
+
<span id={errorId} role="alert" data-terp="field-error">
|
|
72
|
+
{error}
|
|
73
|
+
</span>
|
|
74
|
+
)}
|
|
75
|
+
</div>
|
|
76
|
+
),
|
|
64
77
|
};
|
|
65
78
|
}
|
|
66
79
|
|
|
@@ -160,7 +160,7 @@ describe("user-facing strings", () => {
|
|
|
160
160
|
).toEqual([]);
|
|
161
161
|
});
|
|
162
162
|
|
|
163
|
-
it("never defaults a UiText prop
|
|
163
|
+
it("never defaults a UiText prop, or fills a table of them, with a bare string", () => {
|
|
164
164
|
// The subtler half, and the one that looks fine in review. A `UiText` prop defaulted to
|
|
165
165
|
// `"Select date"` IS overridable — and still untranslatable: a plain string resolves
|
|
166
166
|
// as-is, so an app that does not pass the prop shows English in every locale. The fix is
|
|
@@ -181,9 +181,20 @@ describe("user-facing strings", () => {
|
|
|
181
181
|
// (`const { value, onChange, removeLabel = "Remove", ...rest } = props`) was
|
|
182
182
|
// invisible, and became visible only when the line was split for unrelated reasons.
|
|
183
183
|
// A check that a reformat can switch on and off is not a check.
|
|
184
|
-
|
|
184
|
+
//
|
|
185
|
+
// And a property as well as a default (`:` beside `=`), which is the shape a whole
|
|
186
|
+
// second string table takes. DataView's strings were an interface of `UiText` keys
|
|
187
|
+
// with an object of English values under it, in one file, and this check read every
|
|
188
|
+
// line of it without a finding, because a table entry is `searchPlaceholder: "Search…"`
|
|
189
|
+
// and the check only knew `=`. Every one of those strings rendered in English under
|
|
190
|
+
// every locale, and the completeness guard in locale.test.tsx could not see them
|
|
191
|
+
// either, because they were not in the table it walks.
|
|
192
|
+
for (const assignment of line.matchAll(/(\w+)\s*([=:])\s*"([A-Za-z][^"]*)"/g)) {
|
|
185
193
|
if (uiTextProps.has(assignment[1]!)) {
|
|
186
|
-
offenders.push(
|
|
194
|
+
offenders.push(
|
|
195
|
+
`${file}:${index + 1} ${assignment[1]}${assignment[2] === ":" ? ":" : " ="} ` +
|
|
196
|
+
`"${assignment[3]}"`,
|
|
197
|
+
);
|
|
187
198
|
}
|
|
188
199
|
}
|
|
189
200
|
});
|
|
@@ -191,9 +202,11 @@ describe("user-facing strings", () => {
|
|
|
191
202
|
|
|
192
203
|
expect(
|
|
193
204
|
offenders,
|
|
194
|
-
"a UiText prop defaulted to a plain string
|
|
195
|
-
"
|
|
196
|
-
"
|
|
205
|
+
"a UiText prop defaulted to a plain string — or a table of plain strings under an " +
|
|
206
|
+
"interface of UiText keys — renders that string in every locale for any app that does " +
|
|
207
|
+
"not override it: the prop is translatable and its default is not. Leave the default " +
|
|
208
|
+
"`undefined` and fall back to a TerpStrings key at the use site; a set of defaults " +
|
|
209
|
+
"belongs in TerpStrings itself, where every locale catalog must translate it.",
|
|
197
210
|
).toEqual([]);
|
|
198
211
|
});
|
|
199
212
|
});
|
package/src/uiText.test.tsx
CHANGED
|
@@ -1,14 +1,90 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
import { cleanup, render, screen } from "@testing-library/react";
|
|
3
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { defineModuleManifest } from "@terpjs/contract";
|
|
5
|
+
import type { FrameworkText, UiText } from "@terpjs/contract";
|
|
4
6
|
|
|
5
7
|
import { Page } from "./Page";
|
|
6
8
|
import { ResourceList } from "./ResourceList";
|
|
7
9
|
import { TerpProvider } from "./TerpProvider";
|
|
8
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
resolveUiText,
|
|
12
|
+
resolveUiTextNode,
|
|
13
|
+
UiTextProvider,
|
|
14
|
+
usePlural,
|
|
15
|
+
useUiText,
|
|
16
|
+
} from "./uiText";
|
|
17
|
+
import type { PluralText } from "./uiText";
|
|
9
18
|
|
|
10
19
|
afterEach(cleanup);
|
|
11
20
|
|
|
21
|
+
describe("usePlural", () => {
|
|
22
|
+
// Polish, because its categories differ from English for the same counts: 3 is "few" and 5
|
|
23
|
+
// is "many" there, and both are "other" in English — so a form chosen by the wrong rules is
|
|
24
|
+
// visible, where Dutch and English would agree on every integer.
|
|
25
|
+
const FORMS: PluralText = { one: "one", few: "few", many: "many", other: "other" };
|
|
26
|
+
|
|
27
|
+
function Chosen({ count, text = FORMS }: { count: number; text?: PluralText }) {
|
|
28
|
+
const plural = usePlural();
|
|
29
|
+
return <li>{`${count}:${plural(text, count)}`}</li>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
it("chooses the form the provider's locale gives each count", () => {
|
|
33
|
+
render(
|
|
34
|
+
<UiTextProvider locale="pl">
|
|
35
|
+
<Chosen count={1} />
|
|
36
|
+
<Chosen count={3} />
|
|
37
|
+
<Chosen count={5} />
|
|
38
|
+
</UiTextProvider>,
|
|
39
|
+
);
|
|
40
|
+
expect(screen.getAllByRole("listitem").map((item) => item.textContent)).toEqual([
|
|
41
|
+
"1:one",
|
|
42
|
+
"3:few",
|
|
43
|
+
"5:many",
|
|
44
|
+
]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("uses English rules with no provider, and a nested provider inherits its parent's", () => {
|
|
48
|
+
render(
|
|
49
|
+
<>
|
|
50
|
+
<Chosen count={3} />
|
|
51
|
+
<UiTextProvider locale="pl">
|
|
52
|
+
<UiTextProvider strings={{ loading: "Ładowanie…" }}>
|
|
53
|
+
<Chosen count={3} />
|
|
54
|
+
</UiTextProvider>
|
|
55
|
+
</UiTextProvider>
|
|
56
|
+
</>,
|
|
57
|
+
);
|
|
58
|
+
expect(screen.getAllByRole("listitem").map((item) => item.textContent)).toEqual([
|
|
59
|
+
"3:other",
|
|
60
|
+
"3:few",
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("answers with the other form when the text lacks the chosen one", () => {
|
|
65
|
+
render(
|
|
66
|
+
<UiTextProvider locale="pl">
|
|
67
|
+
<Chosen count={3} text={{ other: "some" }} />
|
|
68
|
+
</UiTextProvider>,
|
|
69
|
+
);
|
|
70
|
+
expect(screen.getByRole("listitem")).toHaveTextContent("3:some");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("refuses a table given directly that writes a count-bearing string as one string", () => {
|
|
74
|
+
// The old shape. Unchecked, `plural` would have handed `undefined` to the caller that
|
|
75
|
+
// fills the placeholders, and the access pane would have thrown on `.replace`.
|
|
76
|
+
expect(() =>
|
|
77
|
+
render(
|
|
78
|
+
<UiTextProvider
|
|
79
|
+
strings={{ accessUnexplainedRoutes: "{count} acties" as unknown as PluralText }}
|
|
80
|
+
>
|
|
81
|
+
<span />
|
|
82
|
+
</UiTextProvider>,
|
|
83
|
+
),
|
|
84
|
+
).toThrow(/UiTextProvider framework string "accessUnexplainedRoutes" counts something/);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
12
88
|
describe("resolveUiText", () => {
|
|
13
89
|
it("passes plain strings through and falls back to a descriptor's message", () => {
|
|
14
90
|
expect(resolveUiText("Tasks")).toBe("Tasks");
|
|
@@ -105,3 +181,68 @@ describe("UiTextProvider", () => {
|
|
|
105
181
|
expect(screen.getByText("Nog niets.")).toBeInTheDocument();
|
|
106
182
|
});
|
|
107
183
|
});
|
|
184
|
+
|
|
185
|
+
describe("FrameworkText", () => {
|
|
186
|
+
function Label({ text }: { text: UiText | FrameworkText }) {
|
|
187
|
+
const resolve = useUiText();
|
|
188
|
+
return <span>{resolve(text)}</span>;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
it("reads the active framework strings and never reaches the app's resolver", () => {
|
|
192
|
+
// The app's resolver is the one that treats a descriptor's `message` as source-locale text,
|
|
193
|
+
// which is exactly what framework copy is not. Handed a FrameworkText, this one would say so.
|
|
194
|
+
const appResolver = vi.fn((text: UiText) =>
|
|
195
|
+
typeof text === "string" ? text : `app:${text.id}`,
|
|
196
|
+
);
|
|
197
|
+
render(
|
|
198
|
+
<UiTextProvider strings={{ admin: "Beheer" }} resolveText={appResolver}>
|
|
199
|
+
<Label text={{ framework: "admin" }} />
|
|
200
|
+
</UiTextProvider>,
|
|
201
|
+
);
|
|
202
|
+
expect(screen.getByText("Beheer")).toBeInTheDocument();
|
|
203
|
+
expect(appResolver).not.toHaveBeenCalled();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("refuses a key the table does not have, including one it only inherits", () => {
|
|
207
|
+
// Cast past the type on purpose: this is the manifest a typecheck never saw.
|
|
208
|
+
const misspelt = { framework: "admn" } as unknown as FrameworkText;
|
|
209
|
+
expect(() => render(<Label text={misspelt} />)).toThrow(
|
|
210
|
+
/FrameworkText names "admn", which is not a framework string/,
|
|
211
|
+
);
|
|
212
|
+
const inherited = { framework: "toString" } as unknown as FrameworkText;
|
|
213
|
+
expect(() => render(<Label text={inherited} />)).toThrow(/names "toString"/);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("refuses a count-bearing key, which has forms and no one string to render", () => {
|
|
217
|
+
const counted = { framework: "dataViewResultsRange" } as unknown as FrameworkText;
|
|
218
|
+
expect(() => render(<Label text={counted} />)).toThrow(
|
|
219
|
+
/FrameworkText names "dataViewResultsRange", which counts something/,
|
|
220
|
+
);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("is a typecheck error for a count-bearing key", () => {
|
|
224
|
+
// As below: `tsc --noEmit` runs the directive, which fails as unused if the manifest's key
|
|
225
|
+
// type ever widens back to every TerpStrings key.
|
|
226
|
+
// @ts-expect-error dataViewResultsRange holds plural forms, not a label.
|
|
227
|
+
const counted: FrameworkText = { framework: "dataViewResultsRange" };
|
|
228
|
+
expect(counted.framework).toBe("dataViewResultsRange");
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("is a typecheck error for a key the table does not have", () => {
|
|
232
|
+
// The directive is the test, and `tsc --noEmit` is what runs it: it fails as unused the
|
|
233
|
+
// moment a misspelt key compiles, as one would if the key were widened to `string`. The
|
|
234
|
+
// assertion below only proves the literal is still an ordinary manifest at runtime.
|
|
235
|
+
const manifest = defineModuleManifest({
|
|
236
|
+
name: "typo",
|
|
237
|
+
routes: [],
|
|
238
|
+
nav: [
|
|
239
|
+
{
|
|
240
|
+
// @ts-expect-error "admn" is not a TerpStrings key.
|
|
241
|
+
label: { framework: "admn" },
|
|
242
|
+
to: "/",
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
});
|
|
246
|
+
expect(manifest.nav).toHaveLength(1);
|
|
247
|
+
});
|
|
248
|
+
});
|