@rind-ui/testing 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 rind-ui contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,16 @@
1
- # Temporary Holding Version
1
+ # @rind-ui/testing
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Official test drivers for rind-ui components, for Testing Library and Playwright.
4
+
5
+ ```sh
6
+ pnpm add @rind-ui/testing
7
+ ```
8
+
9
+ ```ts
10
+ import { getAccordion } from "@rind-ui/testing"
11
+ ```
12
+
13
+ Part of [rind-ui](https://github.com/WatermelonBros/rind-ui): see the main README and
14
+ `docs/components/` for the components and their APIs.
15
+
16
+ MIT © rind-ui contributors
@@ -0,0 +1,152 @@
1
+ import { Announcement, By, Env, Modality, PlatformFlags } from "@rind-ui/core";
2
+ //#region src/collect.d.ts
3
+ interface ElementInfo {
4
+ /** `button#id[data-part=trigger]` — for failure messages. */
5
+ readonly describe: string;
6
+ /** The rind-ui part this element is, if any. */
7
+ readonly part: string | null;
8
+ readonly role: string | null;
9
+ readonly name: string;
10
+ readonly description: string;
11
+ /** Whitespace-normalised textContent. */
12
+ readonly text: string;
13
+ readonly id: string | null;
14
+ readonly attrs: Readonly<Record<string, string>>;
15
+ /** Not visible: see `isVisible`. */
16
+ readonly hidden: boolean;
17
+ readonly inAriaHidden: boolean;
18
+ readonly inInert: boolean;
19
+ readonly focusable: boolean;
20
+ /** Finite animations running on the element (exit animations a presence waits for). */
21
+ readonly animations: number;
22
+ /** The element's identity across snapshots of one page (same node, same number). */
23
+ readonly node: number;
24
+ /** Its border box in viewport coordinates (all zero without layout). */
25
+ readonly rect: {
26
+ readonly x: number;
27
+ readonly y: number;
28
+ readonly width: number;
29
+ readonly height: number;
30
+ };
31
+ /** Items of one set share it (parent element and aria-level), for set metadata. */
32
+ readonly group: string;
33
+ /** Ids of its ancestors, nearest first. */
34
+ readonly within: readonly string[];
35
+ }
36
+ interface FormInputInfo {
37
+ readonly describe: string;
38
+ readonly checked: boolean;
39
+ readonly indeterminate: boolean;
40
+ readonly value: string;
41
+ readonly ariaHidden: string | null;
42
+ readonly tabIndex: number;
43
+ readonly focused: boolean;
44
+ /** `change` / `input` / `click` events whose target was this input. */
45
+ readonly changes: number;
46
+ readonly inputs: number;
47
+ readonly clicks: number;
48
+ /** Inside a group's select-all Parent (`data-parent`): submits nothing. */
49
+ readonly inParent: boolean;
50
+ }
51
+ interface IdRef {
52
+ readonly from: string;
53
+ readonly attr: string;
54
+ readonly id: string;
55
+ readonly exists: boolean;
56
+ readonly targetInInert: boolean;
57
+ }
58
+ interface DomSnapshot {
59
+ /** Part elements of the instance, anywhere in the document, in document order. */
60
+ readonly parts: readonly ElementInfo[];
61
+ /** Parts and every element inside them. */
62
+ readonly elements: readonly ElementInfo[];
63
+ readonly refs: readonly IdRef[];
64
+ /** Ids rendered by the instance that appear more than once in the document. */
65
+ readonly duplicateIds: readonly string[];
66
+ readonly focus: {
67
+ readonly onBody: boolean;
68
+ readonly part: string | null;
69
+ readonly inInstance: boolean;
70
+ readonly element: ElementInfo | null;
71
+ };
72
+ readonly html: Readonly<Record<string, string>>;
73
+ readonly body: Readonly<Record<string, string>>;
74
+ /** aria-hidden, inert and style of the siblings of every part's ancestors, by identity. */
75
+ readonly outside: Readonly<Record<string, Readonly<Record<string, string>>>>;
76
+ /**
77
+ * The rind-ui Field around the instance (field-associated trait): its root's attributes,
78
+ * its Label, and the ids of its rendered Description and ErrorMessage parts.
79
+ */
80
+ readonly field: {
81
+ readonly attrs: Readonly<Record<string, string>>;
82
+ readonly label: {
83
+ readonly id: string;
84
+ readonly text: string;
85
+ readonly for: string | null;
86
+ } | null;
87
+ readonly partIds: readonly string[];
88
+ } | null;
89
+ /** Live regions (explicit `aria-live` other than "off", or by role) inside the parts. */
90
+ readonly liveRegions: readonly string[];
91
+ /**
92
+ * The instance's hidden form inputs (`data-part="hidden-input"`, form-associated trait):
93
+ * DOM properties (never attributes) and the events each dispatched since the page's first
94
+ * snapshot.
95
+ */
96
+ readonly formInputs: readonly FormInputInfo[];
97
+ /** The visual viewport, and whether the page has real layout (not jsdom). */
98
+ readonly viewport: {
99
+ readonly x: number;
100
+ readonly y: number;
101
+ readonly width: number;
102
+ readonly height: number;
103
+ readonly layout: boolean;
104
+ };
105
+ }
106
+ //#endregion
107
+ //#region src/adapter.d.ts
108
+ type Direction = "ltr" | "rtl";
109
+ /** An `Env` plus what drivers and the model runner need from an adapter. */
110
+ interface AdapterEnv extends Env {
111
+ /** The anchor's `data-instance` value. */
112
+ instance(): Promise<string>;
113
+ /** The anchor element, resolved now: how many elements match it. */
114
+ anchorCount(): Promise<number>;
115
+ /** What the generic invariants read, collected in the page. */
116
+ collect(): Promise<DomSnapshot>;
117
+ /** The document alone (<html>, <body>), when the instance is gone (unmounted). */
118
+ collectDocument(): Promise<DomSnapshot>;
119
+ /** Every announcement made in the page so far (the page-wide log). */
120
+ announcements(): Promise<readonly Announcement[]>;
121
+ /** Uncaught errors and `console.error` calls since the last call. */
122
+ takeErrors(): Promise<readonly string[]>;
123
+ /**
124
+ * Chromium accessibility-tree checks for the instance (Playwright in Chromium only):
125
+ * problems found, or "unavailable" elsewhere.
126
+ */
127
+ chromiumChecks?(): Promise<readonly string[] | "unavailable">;
128
+ /**
129
+ * Runs a whole test inside the framework's async scope (React: Testing Library's
130
+ * asyncWrapper), so what a component defers to a later frame — a layer's initial focus and
131
+ * the state its focus event updates — is not reported as an update outside act.
132
+ */
133
+ scope?<T>(fn: () => Promise<T>): Promise<T>;
134
+ /** Marks the start of an action: `navigated()` only counts what happens after it. */
135
+ markNavigations(): Promise<void>;
136
+ /** Sets `dir` on the document element (right-to-left runs); null removes it. */
137
+ setDirection(direction: Direction | null): Promise<void>;
138
+ }
139
+ interface EnvOptions {
140
+ readonly scope: string;
141
+ readonly anchor: By;
142
+ readonly modality: Modality;
143
+ /** The platform the test runs as: decides the `mod` key (Meta on Apple, Control elsewhere). */
144
+ readonly platform?: PlatformFlags;
145
+ }
146
+ /** Creates environments for one target; adapters return one of these. */
147
+ interface EnvFactory {
148
+ readonly "~rindEnvFactory": true;
149
+ create(options: EnvOptions): AdapterEnv;
150
+ }
151
+ //#endregion
152
+ export { DomSnapshot as a, EnvOptions as i, Direction as n, ElementInfo as o, EnvFactory as r, IdRef as s, AdapterEnv as t };
@@ -0,0 +1,111 @@
1
+ //#region src/adapter.ts
2
+ const isEnvFactory = (value) => typeof value === "object" && value !== null && "~rindEnvFactory" in value;
3
+ /**
4
+ * Shared `until` loop with a deadline. Elapsed time is the larger of the wall clock and the
5
+ * time waited, so slow reads count (no multiplied nested timeouts) and fake timers that do
6
+ * not mock `Date` still end the loop. Reads should not wait themselves.
7
+ */
8
+ async function poll(read, accept, options) {
9
+ const interval = 20;
10
+ const start = Date.now();
11
+ let waited = 0;
12
+ let last;
13
+ let error;
14
+ for (;;) {
15
+ try {
16
+ last = await read();
17
+ error = void 0;
18
+ if (accept(last)) return last;
19
+ } catch (e) {
20
+ error = e;
21
+ }
22
+ if (Math.max(Date.now() - start, waited) >= options.timeout) break;
23
+ await options.wait(interval);
24
+ waited += interval;
25
+ }
26
+ if (error !== void 0) throw error;
27
+ throw new UntilTimeout(last, options.timeout);
28
+ }
29
+ var UntilTimeout = class extends Error {
30
+ last;
31
+ constructor(last, timeout) {
32
+ super(`condition not met within ${timeout} ms; last value: ${JSON.stringify(last)}`);
33
+ this.last = last;
34
+ }
35
+ };
36
+ /** Mirrors horizontal arrows for right-to-left runs; everything else is unchanged. */
37
+ const mirror = (key, direction) => direction === "ltr" ? key : key === "ArrowLeft" ? "ArrowRight" : key === "ArrowRight" ? "ArrowLeft" : key;
38
+ const describeBy = (by) => "role" in by ? `role "${by.role}"${by.name === void 0 ? "" : ` named ${JSON.stringify(String(by.name))}`}` : `text ${JSON.stringify(String(by.text))}`;
39
+ //#endregion
40
+ //#region src/adapters/compose.ts
41
+ /**
42
+ * compositionstart; per update a composing keydown, compositionupdate and input; then the
43
+ * commit as a last composing input (value = committed text) and compositionend with it.
44
+ * Without `commit` the composition stays open; a later call continues it (no new
45
+ * compositionstart) and can end it.
46
+ */
47
+ function composeInPage(el, { updates, commit }) {
48
+ const field = el;
49
+ const open = field.__rindComposition;
50
+ const start = field.selectionStart ?? field.value.length;
51
+ const end = field.selectionEnd ?? start;
52
+ const { before, after } = open ?? {
53
+ before: field.value.slice(0, start),
54
+ after: field.value.slice(end)
55
+ };
56
+ const setter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(field), "value")?.set;
57
+ /** The field's value with `text` composed at the caret, the caret after it. */
58
+ const setValue = (text) => {
59
+ const value = before + text + after;
60
+ if (setter) setter.call(field, value);
61
+ else field.value = value;
62
+ const caret = before.length + text.length;
63
+ field.setSelectionRange?.(caret, caret);
64
+ };
65
+ const key = (type) => {
66
+ const e = new KeyboardEvent(type, {
67
+ bubbles: true,
68
+ cancelable: true,
69
+ key: "Process",
70
+ isComposing: true
71
+ });
72
+ Object.defineProperty(e, "keyCode", { value: 229 });
73
+ field.dispatchEvent(e);
74
+ };
75
+ const input = (text) => field.dispatchEvent(new InputEvent("input", {
76
+ bubbles: true,
77
+ isComposing: true,
78
+ inputType: "insertCompositionText",
79
+ data: text
80
+ }));
81
+ if (!open) {
82
+ field.dispatchEvent(new CompositionEvent("compositionstart", {
83
+ bubbles: true,
84
+ data: ""
85
+ }));
86
+ field.__rindComposition = {
87
+ before,
88
+ after
89
+ };
90
+ }
91
+ for (const text of updates) {
92
+ key("keydown");
93
+ field.dispatchEvent(new CompositionEvent("compositionupdate", {
94
+ bubbles: true,
95
+ data: text
96
+ }));
97
+ setValue(text);
98
+ input(text);
99
+ key("keyup");
100
+ }
101
+ if (commit === void 0) return;
102
+ setValue(commit);
103
+ input(commit);
104
+ delete field.__rindComposition;
105
+ field.dispatchEvent(new CompositionEvent("compositionend", {
106
+ bubbles: true,
107
+ data: commit
108
+ }));
109
+ }
110
+ //#endregion
111
+ export { mirror as a, isEnvFactory as i, UntilTimeout as n, poll as o, describeBy as r, composeInPage as t };
@@ -0,0 +1,28 @@
1
+ import { r as EnvFactory } from "./adapter-UPhu17S6.mjs";
2
+ //#region src/adapters/dom.d.ts
3
+ /** The overlap of user-event's API and Vitest browser mode's `userEvent`. */
4
+ interface UserLike {
5
+ click(element: Element): Promise<unknown>;
6
+ hover(element: Element): Promise<unknown>;
7
+ keyboard(text: string): Promise<unknown>;
8
+ type(element: Element, text: string, options?: {
9
+ skipClick?: boolean;
10
+ }): Promise<unknown>;
11
+ dblClick?(element: Element): Promise<unknown>;
12
+ /** user-event only; used for distinct coordinates and touch. */
13
+ pointer?(actions: unknown): Promise<unknown>;
14
+ }
15
+ interface DomAdapterOptions {
16
+ /** Defaults to document. Limits the anchor search; never limits parts (portals). */
17
+ readonly root?: ParentNode;
18
+ /** user-event by default; pass `userEvent` from "vitest/browser" for real input. */
19
+ readonly user?: UserLike;
20
+ /** Fake-timer advance, e.g. `vi.advanceTimersByTime`. */
21
+ readonly advanceTimers?: (ms: number) => unknown;
22
+ /** `until` timeout in ms (default 1000, Testing Library's waitFor default). */
23
+ readonly timeout?: number;
24
+ }
25
+ /** The Testing Library adapter. */
26
+ declare function domEnv(options?: DomAdapterOptions): EnvFactory;
27
+ //#endregion
28
+ export { UserLike as n, domEnv as r, DomAdapterOptions as t };