@rind-ui/testing 0.2.0 → 0.4.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/dist/{adapter-UPhu17S6.d.mts → adapter-C3LQTPFi.d.mts} +20 -1
- package/dist/{compose-DPLnCCnJ.mjs → compose-m76ER6GL.mjs} +34 -2
- package/dist/{dom-BxnIjEBk.d.mts → dom-BH8CVBb2.d.mts} +12 -3
- package/dist/{dom-DZtl6G1E.mjs → dom-DjurZVd0.mjs} +411 -18
- package/dist/dom.d.mts +1 -1
- package/dist/dom.mjs +1 -1
- package/dist/index.d.mts +12533 -1552
- package/dist/index.mjs +2943 -53
- package/dist/inpage.iife.js +7 -4
- package/dist/{playwright-C2LKzGS_.mjs → playwright-BbVj53fU.mjs} +308 -8
- package/dist/{playwright-CtIxjMKr.d.mts → playwright-D2coDhsT.d.mts} +6 -3
- package/dist/playwright.d.mts +1 -1
- package/dist/playwright.mjs +1 -1
- package/dist/setup.mjs +1 -1
- package/package.json +4 -3
|
@@ -17,8 +17,12 @@ interface ElementInfo {
|
|
|
17
17
|
readonly inAriaHidden: boolean;
|
|
18
18
|
readonly inInert: boolean;
|
|
19
19
|
readonly focusable: boolean;
|
|
20
|
-
/**
|
|
20
|
+
/** A grid cell (the roving focus target itself) holding a focusable descendant. */
|
|
21
|
+
readonly focusableInside: boolean;
|
|
22
|
+
/** Finite animations running (or held) on the element: exit animations presence waits for. */
|
|
21
23
|
readonly animations: number;
|
|
24
|
+
/** Its computed style animates (a transition or animation duration above zero). */
|
|
25
|
+
readonly motion: boolean;
|
|
22
26
|
/** The element's identity across snapshots of one page (same node, same number). */
|
|
23
27
|
readonly node: number;
|
|
24
28
|
/** Its border box in viewport coordinates (all zero without layout). */
|
|
@@ -120,6 +124,17 @@ interface AdapterEnv extends Env {
|
|
|
120
124
|
announcements(): Promise<readonly Announcement[]>;
|
|
121
125
|
/** Uncaught errors and `console.error` calls since the last call. */
|
|
122
126
|
takeErrors(): Promise<readonly string[]>;
|
|
127
|
+
/**
|
|
128
|
+
* Pauses every running animation in the document and returns their resume: holds an exit
|
|
129
|
+
* animation while the runner reopens during it, however slow the run (CI Linux).
|
|
130
|
+
*/
|
|
131
|
+
holdAnimations?(): Promise<() => Promise<void>>;
|
|
132
|
+
/**
|
|
133
|
+
* Called before a step that closes a presence layer, so its exit cannot end before
|
|
134
|
+
* `holdAnimations` runs (a slow round trip outlasts a 150 ms exit): animations starting
|
|
135
|
+
* now stay at their start until the hold, which ends this.
|
|
136
|
+
*/
|
|
137
|
+
armHold?(): Promise<void>;
|
|
123
138
|
/**
|
|
124
139
|
* Chromium accessibility-tree checks for the instance (Playwright in Chromium only):
|
|
125
140
|
* problems found, or "unavailable" elsewhere.
|
|
@@ -138,6 +153,10 @@ interface AdapterEnv extends Env {
|
|
|
138
153
|
}
|
|
139
154
|
interface EnvOptions {
|
|
140
155
|
readonly scope: string;
|
|
156
|
+
/** A hosted component's part names (the contract's `partName`): a part's `data-part`. */
|
|
157
|
+
readonly partName?: ((part: string) => string) | undefined;
|
|
158
|
+
/** The inverse: a hosted `data-part` read from the DOM, as the contract names the part. */
|
|
159
|
+
readonly anatomyName?: ((part: string) => string) | undefined;
|
|
141
160
|
readonly anchor: By;
|
|
142
161
|
readonly modality: Modality;
|
|
143
162
|
/** The platform the test runs as: decides the `mod` key (Meta on Apple, Control elsewhere). */
|
|
@@ -1,4 +1,36 @@
|
|
|
1
1
|
//#region src/adapter.ts
|
|
2
|
+
/**
|
|
3
|
+
* A contract's `partName` for the adapters: applied to its anatomy names only, so a name read
|
|
4
|
+
* back from the DOM (already the host's) is used as it is.
|
|
5
|
+
*/
|
|
6
|
+
function hostedPartName(contract) {
|
|
7
|
+
const map = contract.partName;
|
|
8
|
+
if (!map) return {};
|
|
9
|
+
const anatomy = new Set(contract.anatomy);
|
|
10
|
+
const back = new Map(contract.anatomy.map((part) => [map(part), part]));
|
|
11
|
+
return {
|
|
12
|
+
partName: (part) => anatomy.has(part) ? map(part) : part,
|
|
13
|
+
anatomyName: (part) => back.get(part) ?? part
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** A snapshot with its parts named as the contract names them (a hosted component's parts). */
|
|
17
|
+
function renamedSnapshot(snapshot, anatomyName) {
|
|
18
|
+
if (!anatomyName) return snapshot;
|
|
19
|
+
const rename = (el) => el.part === null ? el : {
|
|
20
|
+
...el,
|
|
21
|
+
part: anatomyName(el.part)
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
...snapshot,
|
|
25
|
+
parts: snapshot.parts.map(rename),
|
|
26
|
+
elements: snapshot.elements.map(rename),
|
|
27
|
+
focus: {
|
|
28
|
+
...snapshot.focus,
|
|
29
|
+
part: snapshot.focus.part === null ? null : anatomyName(snapshot.focus.part),
|
|
30
|
+
element: snapshot.focus.element === null ? null : rename(snapshot.focus.element)
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
2
34
|
const isEnvFactory = (value) => typeof value === "object" && value !== null && "~rindEnvFactory" in value;
|
|
3
35
|
/**
|
|
4
36
|
* Shared `until` loop with a deadline. Elapsed time is the larger of the wall clock and the
|
|
@@ -35,7 +67,7 @@ var UntilTimeout = class extends Error {
|
|
|
35
67
|
};
|
|
36
68
|
/** Mirrors horizontal arrows for right-to-left runs; everything else is unchanged. */
|
|
37
69
|
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))}`}` : "part" in by ? `part "${by.part}"` : `text ${JSON.stringify(String(by.text))}`;
|
|
70
|
+
const describeBy = (by) => "role" in by ? `role "${[by.role].flat().join(" | ")}"${by.name === void 0 ? "" : ` named ${JSON.stringify(String(by.name))}`}` : "label" in by ? `control labelled ${JSON.stringify(String(by.label))}` : "part" in by ? `part "${by.part}"` : `text ${JSON.stringify(String(by.text))}`;
|
|
39
71
|
//#endregion
|
|
40
72
|
//#region src/adapters/compose.ts
|
|
41
73
|
/**
|
|
@@ -108,4 +140,4 @@ function composeInPage(el, { updates, commit }) {
|
|
|
108
140
|
}));
|
|
109
141
|
}
|
|
110
142
|
//#endregion
|
|
111
|
-
export {
|
|
143
|
+
export { isEnvFactory as a, renamedSnapshot as c, hostedPartName as i, UntilTimeout as n, mirror as o, describeBy as r, poll as s, composeInPage as t };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { r as EnvFactory } from "./adapter-
|
|
1
|
+
import { r as EnvFactory } from "./adapter-C3LQTPFi.mjs";
|
|
2
2
|
//#region src/adapters/dom.d.ts
|
|
3
3
|
/** The overlap of user-event's API and Vitest browser mode's `userEvent`. */
|
|
4
4
|
interface UserLike {
|
|
@@ -17,9 +17,18 @@ interface DomAdapterOptions {
|
|
|
17
17
|
readonly root?: ParentNode;
|
|
18
18
|
/** user-event by default; pass `userEvent` from "vitest/browser" for real input. */
|
|
19
19
|
readonly user?: UserLike;
|
|
20
|
-
/** Fake-timer advance, e.g. `vi.advanceTimersByTime
|
|
20
|
+
/** Fake-timer advance, e.g. `vi.advanceTimersByTime` (polling advances it too). */
|
|
21
21
|
readonly advanceTimers?: (ms: number) => unknown;
|
|
22
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* A virtual clock's advance (`virtualClock().advance`) for the explicit waits only
|
|
24
|
+
* (`advance`, `longPress`): polling stays on real time, so frames keep running.
|
|
25
|
+
*/
|
|
26
|
+
readonly advanceClock?: (ms: number) => unknown;
|
|
27
|
+
/**
|
|
28
|
+
* `until` timeout in ms. Default 1000 in jsdom (Testing Library's waitFor default) and
|
|
29
|
+
* 3000 in a real browser, where a CPU-starved page can deliver its first animation frame
|
|
30
|
+
* after more than a second (an overlay's initial focus waits for one).
|
|
31
|
+
*/
|
|
23
32
|
readonly timeout?: number;
|
|
24
33
|
}
|
|
25
34
|
/** The Testing Library adapter. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ANNOUNCEMENTS, KEY_DEBOUNCE_MS, PENDING_ANNOUNCEMENTS, isKeptVisible } from "@rind-ui/core";
|
|
1
|
+
import { c as renamedSnapshot, r as describeBy, s as poll, t as composeInPage } from "./compose-m76ER6GL.mjs";
|
|
2
|
+
import { ANNOUNCEMENTS, KEY_DEBOUNCE_MS, PENDING_ANNOUNCEMENTS, isKeptVisible, tabbables } from "@rind-ui/core";
|
|
3
3
|
import { computeAccessibleDescription, computeAccessibleName } from "dom-accessibility-api";
|
|
4
4
|
//#region src/announcements.ts
|
|
5
5
|
/** An announcer that only records (for machine tests in Node, or as an env override). */
|
|
@@ -52,6 +52,14 @@ const norm = (s) => (s ?? "").replace(/\s+/g, " ").trim();
|
|
|
52
52
|
* `hidden` (including `until-found`), `inert` or `display: none`, and it is not
|
|
53
53
|
* `visibility: hidden`. `display: contents` counts as visible: its children render.
|
|
54
54
|
*/
|
|
55
|
+
/** Any transition or animation duration above zero in the computed style. */
|
|
56
|
+
function hasMotion(el) {
|
|
57
|
+
const view = el.ownerDocument.defaultView;
|
|
58
|
+
if (!view) return false;
|
|
59
|
+
const cs = view.getComputedStyle(el);
|
|
60
|
+
const positive = (list) => list.split(",").some((d) => Number.parseFloat(d) > 0);
|
|
61
|
+
return positive(cs.transitionDuration || "0s") || positive(cs.animationDuration || "0s");
|
|
62
|
+
}
|
|
55
63
|
function isVisible(el) {
|
|
56
64
|
if (!el.isConnected) return false;
|
|
57
65
|
const view = el.ownerDocument.defaultView;
|
|
@@ -158,7 +166,16 @@ function collectDom(arg) {
|
|
|
158
166
|
const groupOf = (el) => {
|
|
159
167
|
const parent = el.parentElement;
|
|
160
168
|
if (!parents.has(parent)) parents.set(parent, parents.size);
|
|
161
|
-
|
|
169
|
+
const level = el.getAttribute("aria-level");
|
|
170
|
+
let owner = "";
|
|
171
|
+
if (level !== null) for (let p = el.previousElementSibling; p; p = p.previousElementSibling) {
|
|
172
|
+
const l = p.getAttribute("aria-level");
|
|
173
|
+
if (l !== null && Number(l) < Number(level)) {
|
|
174
|
+
owner = p.id;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return `${parents.get(parent)}|${level ?? ""}|${owner}`;
|
|
162
179
|
};
|
|
163
180
|
const own = (el) => el.getAttribute("data-instance") === arg.instance && el.getAttribute("data-scope") === arg.scope;
|
|
164
181
|
const info = (el) => ({
|
|
@@ -184,7 +201,9 @@ function collectDom(arg) {
|
|
|
184
201
|
inAriaHidden: el.closest("[aria-hidden=true]") !== null,
|
|
185
202
|
inInert: el.closest("[inert]") !== null,
|
|
186
203
|
focusable: focusable(el),
|
|
187
|
-
|
|
204
|
+
focusableInside: roleOf(el) === "gridcell" && [...el.querySelectorAll("*")].some(focusable),
|
|
205
|
+
animations: typeof el.getAnimations === "function" ? el.getAnimations().filter((a) => (a.playState === "running" || a.playState === "paused") && a.effect?.getComputedTiming().endTime !== Number.POSITIVE_INFINITY).length : 0,
|
|
206
|
+
motion: hasMotion(el),
|
|
188
207
|
group: groupOf(el),
|
|
189
208
|
within: ancestorIds(el)
|
|
190
209
|
});
|
|
@@ -223,6 +242,7 @@ function collectDom(arg) {
|
|
|
223
242
|
const v = sibling.getAttribute(name);
|
|
224
243
|
if (v !== null) picked[name] = v;
|
|
225
244
|
}
|
|
245
|
+
if (picked["inert"] === void 0 && sibling.parentElement?.closest("[inert]")) picked["inert"] = "";
|
|
226
246
|
if (isKeptVisible(sibling)) picked["data-rind-kept"] = "";
|
|
227
247
|
if ([
|
|
228
248
|
"SCRIPT",
|
|
@@ -237,6 +257,7 @@ function collectDom(arg) {
|
|
|
237
257
|
if (partEls.length === 0 && doc.body) for (const el of Array.from(doc.body.querySelectorAll("*"))) record(el);
|
|
238
258
|
const active = doc.activeElement;
|
|
239
259
|
const activePart = active?.closest(selector);
|
|
260
|
+
const inControlled = active != null && elements.some((el) => (el.getAttribute("aria-controls") ?? "").split(/\s+/).filter(Boolean).some((id) => doc.getElementById(id)?.contains(active) === true));
|
|
240
261
|
const fieldRoot = partEls[0]?.closest("[data-scope=\"field\"][data-part=\"root\"]") ?? null;
|
|
241
262
|
const fieldPart = (part) => fieldRoot ? Array.from(doc.querySelectorAll(`[data-scope="field"][data-part="${part}"][data-instance="${fieldRoot.getAttribute("data-instance")}"]`)) : [];
|
|
242
263
|
const fieldLabel = fieldPart("label")[0];
|
|
@@ -257,7 +278,7 @@ function collectDom(arg) {
|
|
|
257
278
|
focus: {
|
|
258
279
|
onBody: active === null || active === doc.body,
|
|
259
280
|
part: activePart?.getAttribute("data-part") ?? null,
|
|
260
|
-
inInstance: activePart != null,
|
|
281
|
+
inInstance: activePart != null || inControlled,
|
|
261
282
|
element: active && active !== doc.body ? info(active) : null
|
|
262
283
|
},
|
|
263
284
|
html: attrsOf(doc.documentElement),
|
|
@@ -413,6 +434,34 @@ const nextCoords = (el) => {
|
|
|
413
434
|
};
|
|
414
435
|
};
|
|
415
436
|
/**
|
|
437
|
+
* A point on the page itself, inside the viewport's client area (dismissal treats a press
|
|
438
|
+
* past it as a scrollbar press): the step's coordinates when they qualify, else the first
|
|
439
|
+
* viewport corner that hits only the page. Without layout (jsdom) every point is the page.
|
|
440
|
+
*/
|
|
441
|
+
const outsideCoords = () => {
|
|
442
|
+
const c = nextCoords();
|
|
443
|
+
const root = document.documentElement;
|
|
444
|
+
const page = (x, y) => {
|
|
445
|
+
if (root.clientWidth > 0 && (x >= root.clientWidth || y >= root.clientHeight)) return false;
|
|
446
|
+
const hit = document.elementFromPoint?.(x, y);
|
|
447
|
+
return !hit || hit === document.body || hit === root;
|
|
448
|
+
};
|
|
449
|
+
if (page(c.clientX, c.clientY)) return c;
|
|
450
|
+
const w = root.clientWidth;
|
|
451
|
+
const h = root.clientHeight;
|
|
452
|
+
for (const [x, y] of [
|
|
453
|
+
[w - 2, h - 2],
|
|
454
|
+
[w - 2, 2],
|
|
455
|
+
[2, h - 2]
|
|
456
|
+
]) if (page(x, y)) return {
|
|
457
|
+
clientX: x,
|
|
458
|
+
clientY: y,
|
|
459
|
+
pageX: x + window.scrollX,
|
|
460
|
+
pageY: y + window.scrollY
|
|
461
|
+
};
|
|
462
|
+
return c;
|
|
463
|
+
};
|
|
464
|
+
/**
|
|
416
465
|
* Resolves once no finite transition or animation is running in the document (or after `max`
|
|
417
466
|
* ms). A sibling panel opening shifts the target's box for a while, and a press aimed at
|
|
418
467
|
* coordinates read before the shift releases outside it (the press is then not a press).
|
|
@@ -442,6 +491,7 @@ const KEY_NAMES = {
|
|
|
442
491
|
ArrowRight: "{ArrowRight}",
|
|
443
492
|
Backspace: "{Backspace}",
|
|
444
493
|
Delete: "{Delete}",
|
|
494
|
+
F6: "{F6}",
|
|
445
495
|
F10: "{F10}",
|
|
446
496
|
ContextMenu: "{ContextMenu}",
|
|
447
497
|
A: "a"
|
|
@@ -484,9 +534,10 @@ function installErrorLog(win = globalThis.window ?? null) {
|
|
|
484
534
|
}
|
|
485
535
|
return log;
|
|
486
536
|
}
|
|
537
|
+
const isJsdom = () => (globalThis.navigator?.userAgent ?? "").includes("jsdom");
|
|
487
538
|
/** The Testing Library adapter. */
|
|
488
539
|
function domEnv(options = {}) {
|
|
489
|
-
const timeout = options.timeout ?? 1e3;
|
|
540
|
+
const timeout = options.timeout ?? (isJsdom() ? 1e3 : 3e3);
|
|
490
541
|
const doc = options.root?.ownerDocument ?? globalThis.document;
|
|
491
542
|
const errors = installErrorLog(doc?.defaultView ?? null);
|
|
492
543
|
if (doc?.defaultView) interceptNativeNavigation(doc.defaultView);
|
|
@@ -507,7 +558,7 @@ function domEnv(options = {}) {
|
|
|
507
558
|
const ACT_ENV = "IS_REACT_ACT_ENVIRONMENT";
|
|
508
559
|
/** The `key` value a native event carries for a contract key. */
|
|
509
560
|
const NATIVE_KEY = { Space: " " };
|
|
510
|
-
function createDomEnv({ scope, anchor, modality, platform }, options, getUser, errors) {
|
|
561
|
+
function createDomEnv({ scope, anchor, modality, platform, partName, anatomyName }, options, getUser, errors) {
|
|
511
562
|
const doc = () => options.root?.ownerDocument ?? document;
|
|
512
563
|
const apple = platform?.apple === true;
|
|
513
564
|
/** The navigation log's length when the current action began. */
|
|
@@ -556,9 +607,14 @@ function createDomEnv({ scope, anchor, modality, platform }, options, getUser, e
|
|
|
556
607
|
if ("role" in by) {
|
|
557
608
|
const { queryAllByRole } = await loadTL();
|
|
558
609
|
const name = by.name;
|
|
559
|
-
return queryAllByRole(within,
|
|
610
|
+
return [...new Set([by.role].flat().flatMap((role) => queryAllByRole(within, role, { ...name === void 0 ? {} : { name: typeof name === "string" ? (n) => normalize(n) === name : name } })))].sort((a, b) => a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1);
|
|
560
611
|
}
|
|
561
|
-
if ("
|
|
612
|
+
if ("label" in by) {
|
|
613
|
+
const { queryAllByLabelText } = await loadTL();
|
|
614
|
+
const { label } = by;
|
|
615
|
+
return queryAllByLabelText(within, typeof label === "string" ? (n) => normalize(n) === label : label);
|
|
616
|
+
}
|
|
617
|
+
if ("part" in by) return Array.from(within.querySelectorAll(`[data-scope="${CSS.escape(scope)}"][data-part="${CSS.escape(partName?.(by.part) ?? by.part)}"]`));
|
|
562
618
|
const { text } = by;
|
|
563
619
|
const matches = (el) => {
|
|
564
620
|
const own = normalize(el.textContent);
|
|
@@ -698,6 +754,17 @@ function createDomEnv({ scope, anchor, modality, platform }, options, getUser, e
|
|
|
698
754
|
const el = await waitFor();
|
|
699
755
|
await act(() => el.focus());
|
|
700
756
|
},
|
|
757
|
+
async focusWithTab() {
|
|
758
|
+
const el = await waitFor();
|
|
759
|
+
const stops = tabbables(doc().body);
|
|
760
|
+
const before = stops[stops.indexOf(el) - 1];
|
|
761
|
+
await act(() => {
|
|
762
|
+
if (before) before.focus();
|
|
763
|
+
else doc().activeElement?.blur?.();
|
|
764
|
+
});
|
|
765
|
+
const u = await getUser();
|
|
766
|
+
await act(() => u.keyboard("{Tab}"));
|
|
767
|
+
},
|
|
701
768
|
async press(key, keyOptions) {
|
|
702
769
|
const el = await waitFor();
|
|
703
770
|
if (doc().activeElement !== el) await act(() => el.focus());
|
|
@@ -720,11 +787,11 @@ function createDomEnv({ scope, anchor, modality, platform }, options, getUser, e
|
|
|
720
787
|
commit
|
|
721
788
|
}));
|
|
722
789
|
},
|
|
723
|
-
async pressAtPosition() {
|
|
790
|
+
async pressAtPosition(at) {
|
|
724
791
|
const el = await waitFor();
|
|
725
792
|
const rect = el.getBoundingClientRect();
|
|
726
|
-
const x = rect.left + rect.width / 2;
|
|
727
|
-
const y = rect.top + rect.height / 2;
|
|
793
|
+
const x = at?.corner ? rect.left + 2 : rect.left + rect.width / 2;
|
|
794
|
+
const y = at?.corner ? rect.top + 2 : rect.top + rect.height / 2;
|
|
728
795
|
const hit = (rect.width > 0 ? doc().elementFromPoint(x, y) : null) ?? el;
|
|
729
796
|
const u = await getUser();
|
|
730
797
|
if (u.pointer) await act(() => u.pointer?.([{
|
|
@@ -737,6 +804,280 @@ function createDomEnv({ scope, anchor, modality, platform }, options, getUser, e
|
|
|
737
804
|
}]));
|
|
738
805
|
else await act(() => u.click(hit));
|
|
739
806
|
},
|
|
807
|
+
async longPress(hold) {
|
|
808
|
+
const el = await waitFor();
|
|
809
|
+
const rect = el.getBoundingClientRect();
|
|
810
|
+
const clientX = rect.left + rect.width * (hold?.at?.x ?? .5);
|
|
811
|
+
const clientY = rect.top + rect.height * (hold?.at?.y ?? .5);
|
|
812
|
+
const hit = (rect.width > 0 ? doc().elementFromPoint(clientX, clientY) : null) ?? el;
|
|
813
|
+
const init = {
|
|
814
|
+
bubbles: true,
|
|
815
|
+
cancelable: true,
|
|
816
|
+
composed: true,
|
|
817
|
+
clientX,
|
|
818
|
+
clientY,
|
|
819
|
+
pointerId: 7,
|
|
820
|
+
pointerType: hold?.pointerType ?? "touch",
|
|
821
|
+
isPrimary: true,
|
|
822
|
+
pressure: .5,
|
|
823
|
+
width: 20,
|
|
824
|
+
height: 20,
|
|
825
|
+
detail: 1
|
|
826
|
+
};
|
|
827
|
+
await act(async () => {
|
|
828
|
+
hit.dispatchEvent(new PointerEvent("pointerdown", {
|
|
829
|
+
...init,
|
|
830
|
+
buttons: 1
|
|
831
|
+
}));
|
|
832
|
+
});
|
|
833
|
+
const ms = hold?.ms ?? 520;
|
|
834
|
+
const advance = options.advanceClock ?? options.advanceTimers;
|
|
835
|
+
if (advance) await act(() => advance(ms));
|
|
836
|
+
else await act(() => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
837
|
+
await act(async () => {
|
|
838
|
+
hit.dispatchEvent(new PointerEvent("pointerup", {
|
|
839
|
+
...init,
|
|
840
|
+
buttons: 0
|
|
841
|
+
}));
|
|
842
|
+
});
|
|
843
|
+
},
|
|
844
|
+
async drag(vector, dragOptions) {
|
|
845
|
+
const el = await waitFor();
|
|
846
|
+
const rect = el.getBoundingClientRect();
|
|
847
|
+
const x0 = rect.left + rect.width / 2;
|
|
848
|
+
const y0 = rect.top + rect.height / 2;
|
|
849
|
+
const hit = (rect.width > 0 ? doc().elementFromPoint(x0, y0) : null) ?? el;
|
|
850
|
+
const type = dragOptions?.pointerType ?? "mouse";
|
|
851
|
+
const steps = Math.max(1, dragOptions?.steps ?? 10);
|
|
852
|
+
const stepMs = (dragOptions?.durationMs ?? 300) / steps;
|
|
853
|
+
const advance = options.advanceClock ?? options.advanceTimers;
|
|
854
|
+
const wait = async () => {
|
|
855
|
+
if (advance) await act(() => advance(stepMs));
|
|
856
|
+
else await act(() => new Promise((resolve) => setTimeout(resolve, stepMs)));
|
|
857
|
+
};
|
|
858
|
+
const at = (i) => ({
|
|
859
|
+
x: x0 + vector.x * i / steps,
|
|
860
|
+
y: y0 + vector.y * i / steps
|
|
861
|
+
});
|
|
862
|
+
const fire = (type, p, extra) => act(async () => {
|
|
863
|
+
hit.dispatchEvent(dragEvent(type, p, extra, hit));
|
|
864
|
+
});
|
|
865
|
+
const press = {
|
|
866
|
+
isPrimary: true,
|
|
867
|
+
pressure: .5,
|
|
868
|
+
width: 20,
|
|
869
|
+
height: 20,
|
|
870
|
+
detail: 1
|
|
871
|
+
};
|
|
872
|
+
if (type === "touch") {
|
|
873
|
+
await fire("pointerdown", at(0), {
|
|
874
|
+
...press,
|
|
875
|
+
pointerId: 10,
|
|
876
|
+
pointerType: "touch",
|
|
877
|
+
buttons: 1
|
|
878
|
+
});
|
|
879
|
+
await fire("touchstart", at(0), { touching: true });
|
|
880
|
+
for (let i = 1; i <= steps; i++) {
|
|
881
|
+
await wait();
|
|
882
|
+
await fire("touchmove", at(i), { touching: true });
|
|
883
|
+
}
|
|
884
|
+
await fire("touchend", at(steps), { touching: false });
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
const base = {
|
|
888
|
+
...press,
|
|
889
|
+
pointerId: 9,
|
|
890
|
+
pointerType: type,
|
|
891
|
+
button: 0
|
|
892
|
+
};
|
|
893
|
+
await fire("pointerdown", at(0), {
|
|
894
|
+
...base,
|
|
895
|
+
buttons: 1
|
|
896
|
+
});
|
|
897
|
+
for (let i = 1; i <= steps; i++) {
|
|
898
|
+
await wait();
|
|
899
|
+
await fire("pointermove", at(i), {
|
|
900
|
+
...base,
|
|
901
|
+
buttons: 1
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
await fire("pointerup", at(steps), {
|
|
905
|
+
...base,
|
|
906
|
+
buttons: 0
|
|
907
|
+
});
|
|
908
|
+
},
|
|
909
|
+
async contextMenu(options) {
|
|
910
|
+
const el = await waitFor();
|
|
911
|
+
const rect = el.getBoundingClientRect();
|
|
912
|
+
const fx = options?.at?.x ?? .5;
|
|
913
|
+
const fy = options?.at?.y ?? .5;
|
|
914
|
+
const clientX = rect.left + rect.width * fx;
|
|
915
|
+
const clientY = rect.top + rect.height * fy;
|
|
916
|
+
const hit = (rect.width > 0 ? doc().elementFromPoint(clientX, clientY) : null) ?? el;
|
|
917
|
+
const init = {
|
|
918
|
+
bubbles: true,
|
|
919
|
+
cancelable: true,
|
|
920
|
+
composed: true,
|
|
921
|
+
clientX,
|
|
922
|
+
clientY
|
|
923
|
+
};
|
|
924
|
+
await act(async () => {
|
|
925
|
+
if (modality !== "virtual") {
|
|
926
|
+
const button = {
|
|
927
|
+
...init,
|
|
928
|
+
button: 2,
|
|
929
|
+
buttons: 2,
|
|
930
|
+
pointerId: 1,
|
|
931
|
+
pointerType: "mouse",
|
|
932
|
+
pressure: .5,
|
|
933
|
+
detail: 1
|
|
934
|
+
};
|
|
935
|
+
hit.dispatchEvent(new PointerEvent("pointerdown", button));
|
|
936
|
+
const down = new MouseEvent("mousedown", button);
|
|
937
|
+
hit.dispatchEvent(down);
|
|
938
|
+
if (!down.defaultPrevented) hit.closest("button, a[href], input, select, textarea, [tabindex]")?.focus();
|
|
939
|
+
hit.dispatchEvent(new PointerEvent("pointerup", {
|
|
940
|
+
...button,
|
|
941
|
+
buttons: 0
|
|
942
|
+
}));
|
|
943
|
+
hit.dispatchEvent(new MouseEvent("mouseup", {
|
|
944
|
+
...button,
|
|
945
|
+
buttons: 0
|
|
946
|
+
}));
|
|
947
|
+
}
|
|
948
|
+
hit.dispatchEvent(new PointerEvent("contextmenu", {
|
|
949
|
+
...init,
|
|
950
|
+
button: 2,
|
|
951
|
+
pointerType: "mouse"
|
|
952
|
+
}));
|
|
953
|
+
});
|
|
954
|
+
},
|
|
955
|
+
box: async () => {
|
|
956
|
+
const r = (await now()).getBoundingClientRect();
|
|
957
|
+
return r.width === 0 && r.height === 0 ? null : {
|
|
958
|
+
x: r.x,
|
|
959
|
+
y: r.y,
|
|
960
|
+
width: r.width,
|
|
961
|
+
height: r.height
|
|
962
|
+
};
|
|
963
|
+
},
|
|
964
|
+
async pointer(phase, pointerOptions) {
|
|
965
|
+
const el = await waitFor();
|
|
966
|
+
const r = el.getBoundingClientRect();
|
|
967
|
+
if (r.width === 0 && r.height === 0) throw new Error(`${label}: pointer("${phase}") needs layout, which this environment does not have`);
|
|
968
|
+
const clientX = r.left + pointerOptions.at.x * r.width;
|
|
969
|
+
const clientY = r.top + pointerOptions.at.y * r.height;
|
|
970
|
+
const pointerType = pointerOptions.pointerType ?? "mouse";
|
|
971
|
+
const target = (phase === "down" ? doc().elementFromPoint(clientX, clientY) : null) ?? el;
|
|
972
|
+
const pressing = phase === "down" || phase === "move";
|
|
973
|
+
const type = {
|
|
974
|
+
down: "pointerdown",
|
|
975
|
+
move: "pointermove",
|
|
976
|
+
up: "pointerup",
|
|
977
|
+
cancel: "pointercancel"
|
|
978
|
+
}[phase];
|
|
979
|
+
await act(() => {
|
|
980
|
+
const init = {
|
|
981
|
+
bubbles: true,
|
|
982
|
+
cancelable: phase !== "cancel",
|
|
983
|
+
composed: true,
|
|
984
|
+
pointerId: pointerType === "mouse" ? 1 : 2,
|
|
985
|
+
pointerType,
|
|
986
|
+
isPrimary: true,
|
|
987
|
+
clientX,
|
|
988
|
+
clientY,
|
|
989
|
+
button: phase === "move" ? -1 : 0,
|
|
990
|
+
buttons: pressing ? 1 : 0,
|
|
991
|
+
pressure: pressing ? .5 : 0
|
|
992
|
+
};
|
|
993
|
+
const event = new PointerEvent(type, init);
|
|
994
|
+
target.dispatchEvent(event);
|
|
995
|
+
if (pointerType === "mouse" && phase !== "cancel" && !event.defaultPrevented) {
|
|
996
|
+
const mouse = {
|
|
997
|
+
down: "mousedown",
|
|
998
|
+
move: "mousemove",
|
|
999
|
+
up: "mouseup"
|
|
1000
|
+
}[phase];
|
|
1001
|
+
target.dispatchEvent(new MouseEvent(mouse, {
|
|
1002
|
+
...init,
|
|
1003
|
+
detail: phase === "move" ? 0 : 1
|
|
1004
|
+
}));
|
|
1005
|
+
}
|
|
1006
|
+
});
|
|
1007
|
+
},
|
|
1008
|
+
async fill(value) {
|
|
1009
|
+
const el = await waitFor();
|
|
1010
|
+
await act(() => {
|
|
1011
|
+
const input = el;
|
|
1012
|
+
const setter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(input), "value")?.set;
|
|
1013
|
+
if (setter) setter.call(input, value);
|
|
1014
|
+
else input.value = value;
|
|
1015
|
+
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
1016
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
1017
|
+
});
|
|
1018
|
+
},
|
|
1019
|
+
async paste(text) {
|
|
1020
|
+
const el = await waitFor();
|
|
1021
|
+
await act(() => {
|
|
1022
|
+
const input = el;
|
|
1023
|
+
const start = input.selectionStart ?? input.value.length;
|
|
1024
|
+
const end = input.selectionEnd ?? start;
|
|
1025
|
+
let paste;
|
|
1026
|
+
if (typeof DataTransfer === "function" && typeof ClipboardEvent === "function") {
|
|
1027
|
+
const clipboardData = new DataTransfer();
|
|
1028
|
+
clipboardData.setData("text/plain", text);
|
|
1029
|
+
paste = new ClipboardEvent("paste", {
|
|
1030
|
+
clipboardData,
|
|
1031
|
+
bubbles: true,
|
|
1032
|
+
cancelable: true
|
|
1033
|
+
});
|
|
1034
|
+
if (!paste.clipboardData?.getData("text/plain")) Object.defineProperty(paste, "clipboardData", { value: clipboardData });
|
|
1035
|
+
} else {
|
|
1036
|
+
paste = new Event("paste", {
|
|
1037
|
+
bubbles: true,
|
|
1038
|
+
cancelable: true
|
|
1039
|
+
});
|
|
1040
|
+
Object.defineProperty(paste, "clipboardData", { value: { getData: (type) => /^text(\/plain)?$/.test(type) ? text : "" } });
|
|
1041
|
+
}
|
|
1042
|
+
if (!input.dispatchEvent(paste)) return;
|
|
1043
|
+
const init = {
|
|
1044
|
+
inputType: "insertFromPaste",
|
|
1045
|
+
data: text,
|
|
1046
|
+
bubbles: true
|
|
1047
|
+
};
|
|
1048
|
+
const before = new InputEvent("beforeinput", {
|
|
1049
|
+
...init,
|
|
1050
|
+
cancelable: true
|
|
1051
|
+
});
|
|
1052
|
+
if (!input.dispatchEvent(before)) return;
|
|
1053
|
+
const next = input.value.slice(0, start) + text + input.value.slice(end);
|
|
1054
|
+
const setter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(input), "value")?.set;
|
|
1055
|
+
if (setter) setter.call(input, next);
|
|
1056
|
+
else input.value = next;
|
|
1057
|
+
input.setSelectionRange(start + text.length, start + text.length);
|
|
1058
|
+
input.dispatchEvent(new InputEvent("input", init));
|
|
1059
|
+
});
|
|
1060
|
+
},
|
|
1061
|
+
async setFiles(specs) {
|
|
1062
|
+
const el = await waitFor();
|
|
1063
|
+
await act(() => {
|
|
1064
|
+
const input = el;
|
|
1065
|
+
const files = specs.map((f) => new File([f.content], f.name, { type: f.type }));
|
|
1066
|
+
if (typeof DataTransfer === "function") {
|
|
1067
|
+
const dt = new DataTransfer();
|
|
1068
|
+
for (const f of files) dt.items.add(f);
|
|
1069
|
+
input.files = dt.files;
|
|
1070
|
+
} else {
|
|
1071
|
+
const list = Object.assign([...files], { item: (i) => files[i] ?? null });
|
|
1072
|
+
Object.defineProperty(input, "files", {
|
|
1073
|
+
configurable: true,
|
|
1074
|
+
value: list
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
1078
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
1079
|
+
});
|
|
1080
|
+
},
|
|
740
1081
|
waitForDetach: async () => {
|
|
741
1082
|
await until(async () => (await resolve()).length, (n) => n === 0);
|
|
742
1083
|
},
|
|
@@ -787,7 +1128,8 @@ function createDomEnv({ scope, anchor, modality, platform }, options, getUser, e
|
|
|
787
1128
|
};
|
|
788
1129
|
const partEls = async (name, by) => {
|
|
789
1130
|
const id = await instance();
|
|
790
|
-
const
|
|
1131
|
+
const part = partName?.(name) ?? name;
|
|
1132
|
+
const sel = `[data-scope="${CSS.escape(scope)}"][data-part="${CSS.escape(part)}"][data-instance="${CSS.escape(id)}"]`;
|
|
791
1133
|
const parts = Array.from(doc().querySelectorAll(sel));
|
|
792
1134
|
if (!by) return parts;
|
|
793
1135
|
const matching = new Set(await queryBy(by, doc()));
|
|
@@ -811,24 +1153,33 @@ function createDomEnv({ scope, anchor, modality, platform }, options, getUser, e
|
|
|
811
1153
|
const u = await getUser();
|
|
812
1154
|
const body = doc().body;
|
|
813
1155
|
if (!u.pointer) return act(() => u.click(body));
|
|
814
|
-
const coords = nextCoords();
|
|
815
1156
|
await act(() => u.pointer?.([{
|
|
816
1157
|
keys: "[MouseLeft]",
|
|
817
1158
|
target: body,
|
|
818
|
-
coords
|
|
1159
|
+
coords: outsideCoords()
|
|
819
1160
|
}]));
|
|
820
1161
|
},
|
|
1162
|
+
async pointerAway() {
|
|
1163
|
+
const u = await getUser();
|
|
1164
|
+
const body = doc().body;
|
|
1165
|
+
if (!u.pointer) return act(() => u.hover(body));
|
|
1166
|
+
await act(() => u.pointer?.({
|
|
1167
|
+
target: body,
|
|
1168
|
+
coords: outsideCoords()
|
|
1169
|
+
}));
|
|
1170
|
+
},
|
|
821
1171
|
until,
|
|
822
1172
|
advance: async (ms) => {
|
|
823
|
-
|
|
1173
|
+
const advance = options.advanceClock ?? options.advanceTimers;
|
|
1174
|
+
if (advance) await act(() => advance(ms));
|
|
824
1175
|
else await act(() => new Promise((resolve) => setTimeout(resolve, Math.max(ms, 16))));
|
|
825
1176
|
},
|
|
826
1177
|
instance,
|
|
827
1178
|
anchorCount: async () => (await anchorEls()).length,
|
|
828
|
-
collect: async () => collectDom({
|
|
1179
|
+
collect: async () => renamedSnapshot(collectDom({
|
|
829
1180
|
scope,
|
|
830
1181
|
instance: await instance()
|
|
831
|
-
}),
|
|
1182
|
+
}), anatomyName),
|
|
832
1183
|
collectDocument: async () => collectDom({
|
|
833
1184
|
scope,
|
|
834
1185
|
instance: "\0unmounted"
|
|
@@ -838,6 +1189,13 @@ function createDomEnv({ scope, anchor, modality, platform }, options, getUser, e
|
|
|
838
1189
|
return [...installAnnouncementLog()];
|
|
839
1190
|
},
|
|
840
1191
|
takeErrors: async () => errors().splice(0),
|
|
1192
|
+
holdAnimations: async () => {
|
|
1193
|
+
const running = doc().getAnimations?.().filter((a) => a.playState === "running") ?? [];
|
|
1194
|
+
for (const a of running) a.pause();
|
|
1195
|
+
return async () => {
|
|
1196
|
+
for (const a of running) if (a.playState === "paused") a.play();
|
|
1197
|
+
};
|
|
1198
|
+
},
|
|
841
1199
|
selectedText: async () => doc().getSelection()?.toString() ?? "",
|
|
842
1200
|
markNavigations: async () => {
|
|
843
1201
|
navigationsFrom = navigationLog().length;
|
|
@@ -850,5 +1208,40 @@ function createDomEnv({ scope, anchor, modality, platform }, options, getUser, e
|
|
|
850
1208
|
}
|
|
851
1209
|
};
|
|
852
1210
|
}
|
|
1211
|
+
/**
|
|
1212
|
+
* A drag's event: a PointerEvent, or a touch event carrying one touch point (`touching`:
|
|
1213
|
+
* in `touches`; a touchend has none). Touch points are plain objects with the fields a
|
|
1214
|
+
* gesture reads, so it works where `Touch` cannot be constructed (jsdom).
|
|
1215
|
+
*/
|
|
1216
|
+
function dragEvent(type, p, extra, target) {
|
|
1217
|
+
if (!type.startsWith("touch")) return new PointerEvent(type, {
|
|
1218
|
+
bubbles: true,
|
|
1219
|
+
cancelable: true,
|
|
1220
|
+
composed: true,
|
|
1221
|
+
clientX: p.x,
|
|
1222
|
+
clientY: p.y,
|
|
1223
|
+
...extra
|
|
1224
|
+
});
|
|
1225
|
+
const e = new Event(type, {
|
|
1226
|
+
bubbles: true,
|
|
1227
|
+
cancelable: true,
|
|
1228
|
+
composed: true
|
|
1229
|
+
});
|
|
1230
|
+
const point = {
|
|
1231
|
+
identifier: 1,
|
|
1232
|
+
target,
|
|
1233
|
+
clientX: p.x,
|
|
1234
|
+
clientY: p.y,
|
|
1235
|
+
pageX: p.x,
|
|
1236
|
+
pageY: p.y
|
|
1237
|
+
};
|
|
1238
|
+
const touches = extra.touching ? [point] : [];
|
|
1239
|
+
Object.defineProperties(e, {
|
|
1240
|
+
touches: { value: touches },
|
|
1241
|
+
targetTouches: { value: touches },
|
|
1242
|
+
changedTouches: { value: [point] }
|
|
1243
|
+
});
|
|
1244
|
+
return e;
|
|
1245
|
+
}
|
|
853
1246
|
//#endregion
|
|
854
1247
|
export { navigationLog as a, interceptNativeNavigation as i, installErrorLog as n, installAnnouncementLog as o, fakeRouter as r, recordingAnnouncer as s, domEnv as t };
|