@blackbelt-technology/pi-dashboard-client-utils 0.5.4 → 0.6.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.
@@ -0,0 +1,215 @@
1
+ import { describe, it, expect, afterEach, vi } from "vitest";
2
+ import { render, cleanup, fireEvent } from "@testing-library/react";
3
+ import React from "react";
4
+ import { mdiAlert } from "@mdi/js";
5
+ import { Dialog } from "../Dialog.js";
6
+
7
+ afterEach(() => cleanup());
8
+
9
+ describe("Dialog", () => {
10
+ it("renders nothing when closed", () => {
11
+ const { baseElement } = render(
12
+ <Dialog open={false} onClose={() => {}} testId="d">
13
+ <p>hi</p>
14
+ </Dialog>,
15
+ );
16
+ expect(baseElement.querySelector("[data-testid='d']")).toBeNull();
17
+ });
18
+
19
+ it("renders at document.body via portal when open", () => {
20
+ const { baseElement } = render(
21
+ <Dialog open onClose={() => {}} testId="d">
22
+ <p>body</p>
23
+ </Dialog>,
24
+ );
25
+ const container = baseElement.querySelector("[data-testid='d']");
26
+ expect(container).toBeTruthy();
27
+ expect(container!.getAttribute("role")).toBe("dialog");
28
+ expect(container!.getAttribute("aria-modal")).toBe("true");
29
+ });
30
+
31
+ it("calls onClose on Esc", () => {
32
+ const onClose = vi.fn();
33
+ render(
34
+ <Dialog open onClose={onClose} testId="d">
35
+ <p>x</p>
36
+ </Dialog>,
37
+ );
38
+ // Escape now routes through the shared escape-stack (document-bubble listener).
39
+ fireEvent.keyDown(document, { key: "Escape" });
40
+ expect(onClose).toHaveBeenCalledTimes(1);
41
+ });
42
+
43
+ it("calls onClose on overlay click", () => {
44
+ const onClose = vi.fn();
45
+ const { baseElement } = render(
46
+ <Dialog open onClose={onClose} testId="d">
47
+ <p>x</p>
48
+ </Dialog>,
49
+ );
50
+ fireEvent.click(baseElement.querySelector("[data-testid='d-overlay']")!);
51
+ expect(onClose).toHaveBeenCalledTimes(1);
52
+ });
53
+
54
+ it("does not call onClose when clicking inside the container", () => {
55
+ const onClose = vi.fn();
56
+ const { getByText } = render(
57
+ <Dialog open onClose={onClose} testId="d">
58
+ <p>inside</p>
59
+ </Dialog>,
60
+ );
61
+ fireEvent.click(getByText("inside"));
62
+ expect(onClose).not.toHaveBeenCalled();
63
+ });
64
+
65
+ it("sets aria-labelledby when title is given", () => {
66
+ const { baseElement } = render(
67
+ <Dialog open onClose={() => {}} title="Hello" testId="d">
68
+ <p>x</p>
69
+ </Dialog>,
70
+ );
71
+ const container = baseElement.querySelector("[data-testid='d']")!;
72
+ const labelledBy = container.getAttribute("aria-labelledby");
73
+ expect(labelledBy).toBeTruthy();
74
+ expect(document.getElementById(labelledBy!)!.textContent).toBe("Hello");
75
+ });
76
+
77
+ it("omits aria-labelledby and header without title/icon", () => {
78
+ const { baseElement } = render(
79
+ <Dialog open onClose={() => {}} testId="d">
80
+ <p>x</p>
81
+ </Dialog>,
82
+ );
83
+ const container = baseElement.querySelector("[data-testid='d']")!;
84
+ expect(container.getAttribute("aria-labelledby")).toBeNull();
85
+ expect(container.querySelector("h3")).toBeNull();
86
+ });
87
+
88
+ it("renders header icon + title", () => {
89
+ const { baseElement } = render(
90
+ <Dialog open onClose={() => {}} title="T" icon={mdiAlert} testId="d">
91
+ <p>x</p>
92
+ </Dialog>,
93
+ );
94
+ const container = baseElement.querySelector("[data-testid='d']")!;
95
+ expect(container.querySelector("h3")!.textContent).toBe("T");
96
+ expect(container.querySelector("svg")).toBeTruthy();
97
+ });
98
+
99
+ it("applies overlay tint and z-index chrome", () => {
100
+ const { baseElement } = render(
101
+ <Dialog open onClose={() => {}} testId="d">
102
+ <p>x</p>
103
+ </Dialog>,
104
+ );
105
+ const overlay = baseElement.querySelector("[data-testid='d-overlay']")!;
106
+ expect(overlay.className).toContain("bg-black/60");
107
+ const root = overlay.parentElement!;
108
+ expect(root.className).toContain("z-[60]");
109
+ const container = baseElement.querySelector("[data-testid='d']")!;
110
+ expect(container.className).toContain("bg-[var(--bg-primary)]");
111
+ expect(container.className).toContain("border-[var(--border-primary)]");
112
+ });
113
+
114
+ it.each([
115
+ ["sm", "max-w-sm"],
116
+ ["md", "max-w-md"],
117
+ ["lg", "max-w-lg"],
118
+ ["full", "max-w-[95vw]"],
119
+ ] as const)("applies %s size class %s", (size, cls) => {
120
+ const { baseElement } = render(
121
+ <Dialog open onClose={() => {}} size={size} testId="d">
122
+ <p>x</p>
123
+ </Dialog>,
124
+ );
125
+ expect(baseElement.querySelector("[data-testid='d']")!.className).toContain(cls);
126
+ });
127
+
128
+ it.each([
129
+ ["sm", "max-h-[80vh]"],
130
+ ["md", "max-h-[80vh]"],
131
+ ["lg", "max-h-[80vh]"],
132
+ ["full", "max-h-[92vh]"],
133
+ ] as const)("applies %s height cap %s", (size, cls) => {
134
+ const { baseElement } = render(
135
+ <Dialog open onClose={() => {}} size={size} testId="d">
136
+ <p>x</p>
137
+ </Dialog>,
138
+ );
139
+ expect(baseElement.querySelector("[data-testid='d']")!.className).toContain(cls);
140
+ });
141
+
142
+ it("flush drops padding and clips overflow (edge-to-edge body)", () => {
143
+ const { baseElement } = render(
144
+ <Dialog open onClose={() => {}} flush testId="d">
145
+ <p>x</p>
146
+ </Dialog>,
147
+ );
148
+ const cls = baseElement.querySelector("[data-testid='d']")!.className;
149
+ expect(cls).toContain("overflow-hidden");
150
+ expect(cls).not.toContain("p-5");
151
+ });
152
+
153
+ it("non-flush keeps padding + scroll", () => {
154
+ const { baseElement } = render(
155
+ <Dialog open onClose={() => {}} testId="d">
156
+ <p>x</p>
157
+ </Dialog>,
158
+ );
159
+ const cls = baseElement.querySelector("[data-testid='d']")!.className;
160
+ expect(cls).toContain("p-5");
161
+ expect(cls).toContain("overflow-y-auto");
162
+ });
163
+
164
+ it("defaults to max-w-md", () => {
165
+ const { baseElement } = render(
166
+ <Dialog open onClose={() => {}} testId="d">
167
+ <p>x</p>
168
+ </Dialog>,
169
+ );
170
+ expect(baseElement.querySelector("[data-testid='d']")!.className).toContain(
171
+ "max-w-md",
172
+ );
173
+ });
174
+
175
+ it("maps action intents to classes and derives testIds", () => {
176
+ const onAction = vi.fn();
177
+ const onCancel = vi.fn();
178
+ const { baseElement } = render(
179
+ <Dialog open onClose={() => {}} testId="d">
180
+ <Dialog.Footer>
181
+ <Dialog.Cancel onClick={onCancel} testId="d-cancel" />
182
+ <Dialog.Action onClick={onAction} intent="danger" testId="d-action">
183
+ Go
184
+ </Dialog.Action>
185
+ </Dialog.Footer>
186
+ </Dialog>,
187
+ );
188
+ const action = baseElement.querySelector("[data-testid='d-action']")!;
189
+ expect(action.className).toContain("bg-red-600");
190
+ const cancel = baseElement.querySelector("[data-testid='d-cancel']")!;
191
+ fireEvent.click(cancel);
192
+ fireEvent.click(action);
193
+ expect(onCancel).toHaveBeenCalledTimes(1);
194
+ expect(onAction).toHaveBeenCalledTimes(1);
195
+ });
196
+
197
+ it("primary intent uses accent, neutral uses border", () => {
198
+ const { baseElement } = render(
199
+ <Dialog open onClose={() => {}} testId="d">
200
+ <Dialog.Action onClick={() => {}} testId="p">
201
+ P
202
+ </Dialog.Action>
203
+ <Dialog.Action onClick={() => {}} intent="neutral" testId="n">
204
+ N
205
+ </Dialog.Action>
206
+ </Dialog>,
207
+ );
208
+ expect(baseElement.querySelector("[data-testid='p']")!.className).toContain(
209
+ "bg-[var(--accent-primary)]",
210
+ );
211
+ expect(baseElement.querySelector("[data-testid='n']")!.className).toContain(
212
+ "border-[var(--border-primary)]",
213
+ );
214
+ });
215
+ });
@@ -0,0 +1,44 @@
1
+ import { cleanup, fireEvent, render } from "@testing-library/react";
2
+ import React from "react";
3
+ import { afterEach, describe, expect, it, vi } from "vitest";
4
+ import { EmptyState } from "../EmptyState.js";
5
+
6
+ afterEach(() => cleanup());
7
+
8
+ describe("EmptyState", () => {
9
+ it("renders title, body, and a single primary CTA", () => {
10
+ const onClick = vi.fn();
11
+ const { getByText, getAllByRole, container } = render(
12
+ <EmptyState
13
+ title="No sessions yet"
14
+ body="Spawn a pi session to see it here."
15
+ action={{ label: "Spawn session", onClick }}
16
+ />,
17
+ );
18
+ expect(getByText("No sessions yet")).toBeDefined();
19
+ expect(getByText("Spawn a pi session to see it here.")).toBeDefined();
20
+ const primaries = container.querySelectorAll('[data-empty-state-action="primary"]');
21
+ expect(primaries).toHaveLength(1);
22
+ fireEvent.click(getAllByRole("button")[0]);
23
+ expect(onClick).toHaveBeenCalledTimes(1);
24
+ });
25
+
26
+ it("allows at most one primary action (single `action` prop)", () => {
27
+ const { container } = render(
28
+ <EmptyState
29
+ title="Empty"
30
+ action={{ label: "Primary", onClick: () => {} }}
31
+ secondaryAction={{ label: "Escape", onClick: () => {} }}
32
+ />,
33
+ );
34
+ // The contract: exactly one primary, at most one secondary — never >1 primary.
35
+ expect(container.querySelectorAll('[data-empty-state-action="primary"]')).toHaveLength(1);
36
+ expect(container.querySelectorAll('[data-empty-state-action="secondary"]')).toHaveLength(1);
37
+ });
38
+
39
+ it("renders without any actions", () => {
40
+ const { container, getByText } = render(<EmptyState title="Nothing here" />);
41
+ expect(getByText("Nothing here")).toBeDefined();
42
+ expect(container.querySelectorAll("button")).toHaveLength(0);
43
+ });
44
+ });
@@ -0,0 +1,51 @@
1
+ import { cleanup, render } from "@testing-library/react";
2
+ import React from "react";
3
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4
+ import { Skeleton } from "../Skeleton.js";
5
+
6
+ afterEach(() => cleanup());
7
+
8
+ function mockReducedMotion(reduce: boolean) {
9
+ vi.stubGlobal(
10
+ "matchMedia",
11
+ (query: string) =>
12
+ ({
13
+ matches: query.includes("prefers-reduced-motion") ? reduce : false,
14
+ media: query,
15
+ addEventListener: () => {},
16
+ removeEventListener: () => {},
17
+ addListener: () => {},
18
+ removeListener: () => {},
19
+ dispatchEvent: () => false,
20
+ onchange: null,
21
+ }) as unknown as MediaQueryList,
22
+ );
23
+ // jsdom attaches matchMedia to window; mirror the stub there too.
24
+ (window as unknown as { matchMedia: typeof window.matchMedia }).matchMedia =
25
+ globalThis.matchMedia;
26
+ }
27
+
28
+ describe("Skeleton", () => {
29
+ beforeEach(() => mockReducedMotion(false));
30
+
31
+ it("renders `count` shaped placeholder rows", () => {
32
+ const { container } = render(<Skeleton variant="bubble" count={3} />);
33
+ expect(container.querySelectorAll("[data-skeleton-item]")).toHaveLength(3);
34
+ expect(container.querySelector("[data-skeleton='bubble']")).toBeTruthy();
35
+ });
36
+
37
+ it("animates (shimmer) when reduced motion is off", () => {
38
+ mockReducedMotion(false);
39
+ const { container } = render(<Skeleton variant="row" />);
40
+ const item = container.querySelector("[data-skeleton-item]");
41
+ expect(item?.className).toContain("animate-pulse");
42
+ });
43
+
44
+ it("renders static (no shimmer) under prefers-reduced-motion: reduce", () => {
45
+ mockReducedMotion(true);
46
+ const { container } = render(<Skeleton variant="row" />);
47
+ expect(container.querySelector("[data-skeleton][data-static='true']")).toBeTruthy();
48
+ const item = container.querySelector("[data-skeleton-item]");
49
+ expect(item?.className).not.toContain("animate-pulse");
50
+ });
51
+ });
@@ -0,0 +1,166 @@
1
+ import { describe, it, expect, afterEach, vi } from "vitest";
2
+ import { createElement, StrictMode } from "react";
3
+ import { render, cleanup } from "@testing-library/react";
4
+ import {
5
+ registerEscapeLayer,
6
+ unregisterEscapeLayer,
7
+ useEscapeDismiss,
8
+ __resetEscapeStack,
9
+ } from "../escape-stack.js";
10
+
11
+ afterEach(() => {
12
+ cleanup();
13
+ __resetEscapeStack();
14
+ });
15
+
16
+ function pressEscape(init: KeyboardEventInit = {}): KeyboardEvent {
17
+ const ev = new KeyboardEvent("keydown", {
18
+ key: "Escape",
19
+ bubbles: true,
20
+ cancelable: true,
21
+ ...init,
22
+ });
23
+ document.dispatchEvent(ev);
24
+ return ev;
25
+ }
26
+
27
+ describe("escape-stack — topmost-only dispatch", () => {
28
+ it("E1 topmost-only: only the top layer's onEscape fires", () => {
29
+ const a = vi.fn();
30
+ const b = vi.fn();
31
+ registerEscapeLayer("A", a);
32
+ registerEscapeLayer("B", b);
33
+ pressEscape();
34
+ expect(b).toHaveBeenCalledTimes(1);
35
+ expect(a).not.toHaveBeenCalled();
36
+ });
37
+
38
+ it("E2 successive peel: three Escapes dismiss C, B, A in order", () => {
39
+ const order: string[] = [];
40
+ registerEscapeLayer("A", () => {
41
+ order.push("A");
42
+ unregisterEscapeLayer("A");
43
+ });
44
+ registerEscapeLayer("B", () => {
45
+ order.push("B");
46
+ unregisterEscapeLayer("B");
47
+ });
48
+ registerEscapeLayer("C", () => {
49
+ order.push("C");
50
+ unregisterEscapeLayer("C");
51
+ });
52
+ pressEscape();
53
+ pressEscape();
54
+ pressEscape();
55
+ expect(order).toEqual(["C", "B", "A"]);
56
+ });
57
+
58
+ it("E3 lone layer: single layer still dismisses", () => {
59
+ const a = vi.fn();
60
+ registerEscapeLayer("A", a);
61
+ pressEscape();
62
+ expect(a).toHaveBeenCalledTimes(1);
63
+ });
64
+ });
65
+
66
+ describe("escape-stack — lifecycle", () => {
67
+ it("E4 order-independent unregister: removing A by id leaves B live", () => {
68
+ const a = vi.fn();
69
+ const b = vi.fn();
70
+ registerEscapeLayer("A", a);
71
+ registerEscapeLayer("B", b);
72
+ unregisterEscapeLayer("A");
73
+ pressEscape();
74
+ expect(b).toHaveBeenCalledTimes(1);
75
+ expect(a).not.toHaveBeenCalled();
76
+ });
77
+
78
+ it("E11 latest handler without re-registration: re-register same id uses cb2", () => {
79
+ const cb1 = vi.fn();
80
+ const cb2 = vi.fn();
81
+ registerEscapeLayer("A", cb1);
82
+ registerEscapeLayer("A", cb2); // same id → refresh, not a second entry
83
+ pressEscape();
84
+ expect(cb2).toHaveBeenCalledTimes(1);
85
+ expect(cb1).not.toHaveBeenCalled();
86
+ });
87
+ });
88
+
89
+ describe("escape-stack — consume + passthrough", () => {
90
+ it("E5 empty passthrough: no layers → event not consumed, window spy fires", () => {
91
+ const spy = vi.fn();
92
+ window.addEventListener("keydown", spy);
93
+ const ev = pressEscape();
94
+ expect(spy).toHaveBeenCalledTimes(1);
95
+ expect(ev.defaultPrevented).toBe(false);
96
+ window.removeEventListener("keydown", spy);
97
+ });
98
+
99
+ it("E6 consume blocks window: layer dismisses, window spy does not fire", () => {
100
+ const a = vi.fn();
101
+ const spy = vi.fn();
102
+ window.addEventListener("keydown", spy);
103
+ registerEscapeLayer("A", a);
104
+ const ev = pressEscape();
105
+ expect(a).toHaveBeenCalledTimes(1);
106
+ expect(spy).not.toHaveBeenCalled();
107
+ expect(ev.defaultPrevented).toBe(true);
108
+ window.removeEventListener("keydown", spy);
109
+ });
110
+ });
111
+
112
+ describe("escape-stack — guards", () => {
113
+ it("E7 key-repeat guard: repeat dismisses nothing, non-repeat dismisses B", () => {
114
+ const a = vi.fn();
115
+ const b = vi.fn();
116
+ registerEscapeLayer("A", a);
117
+ registerEscapeLayer("B", b);
118
+ pressEscape({ repeat: true });
119
+ expect(a).not.toHaveBeenCalled();
120
+ expect(b).not.toHaveBeenCalled();
121
+ pressEscape({ repeat: false });
122
+ expect(b).toHaveBeenCalledTimes(1);
123
+ expect(a).not.toHaveBeenCalled();
124
+ });
125
+
126
+ it("E8 defaultPrevented opt-out: layer stays, onEscape not called", () => {
127
+ const a = vi.fn();
128
+ registerEscapeLayer("A", a);
129
+ // A focused input already handled Escape (defaultPrevented) before it bubbles
130
+ // to document — simulate via a capture-phase listener that preventDefaults.
131
+ const pre = (e: KeyboardEvent) => e.preventDefault();
132
+ document.addEventListener("keydown", pre, { capture: true });
133
+ pressEscape();
134
+ expect(a).not.toHaveBeenCalled();
135
+ document.removeEventListener("keydown", pre, { capture: true });
136
+ // A is still registered → a clean Escape now dismisses it.
137
+ pressEscape();
138
+ expect(a).toHaveBeenCalledTimes(1);
139
+ });
140
+ });
141
+
142
+ describe("escape-stack — hook lifecycle", () => {
143
+ it("E9 StrictMode id stability: one entry, fires once, no leak/drop", () => {
144
+ const onEscape = vi.fn();
145
+ function Layer() {
146
+ useEscapeDismiss(true, onEscape);
147
+ return null;
148
+ }
149
+ render(createElement(StrictMode, null, createElement(Layer)));
150
+ pressEscape();
151
+ expect(onEscape).toHaveBeenCalledTimes(1);
152
+ });
153
+ });
154
+
155
+ describe("escape-stack — attach-once / never-detach", () => {
156
+ it("E10 attaches document keydown once, no duplicate across empty→refill", () => {
157
+ __resetEscapeStack(); // clean module state: detached
158
+ const addSpy = vi.spyOn(document, "addEventListener");
159
+ registerEscapeLayer("A", vi.fn()); // first registration attaches
160
+ unregisterEscapeLayer("A"); // stack empty, listener stays attached
161
+ registerEscapeLayer("B", vi.fn()); // already attached → no new listener
162
+ const keydownAdds = addSpy.mock.calls.filter((c) => c[0] === "keydown");
163
+ expect(keydownAdds).toHaveLength(1);
164
+ addSpy.mockRestore();
165
+ });
166
+ });
@@ -0,0 +1,34 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ type StatusKind,
4
+ statusAriaLabel,
5
+ statusPresentation,
6
+ } from "../statusPresentation.js";
7
+
8
+ describe("statusPresentation", () => {
9
+ const kinds: StatusKind[] = ["done", "current", "todo", "error"];
10
+
11
+ it("provides a non-color glyph indicator for every state", () => {
12
+ for (const kind of kinds) {
13
+ expect(statusPresentation(kind).glyph.trim().length).toBeGreaterThan(0);
14
+ }
15
+ });
16
+
17
+ it("renders a distinct glyph per state (distinguishable without color)", () => {
18
+ const glyphs = kinds.map((k) => statusPresentation(k).glyph);
19
+ expect(new Set(glyphs).size).toBe(kinds.length);
20
+ });
21
+
22
+ it("done is distinguishable from todo by glyph alone", () => {
23
+ expect(statusPresentation("done").glyph).not.toBe(statusPresentation("todo").glyph);
24
+ });
25
+
26
+ it("maps each state to a semantic --status / token var", () => {
27
+ expect(statusPresentation("done").tokenVar).toContain("var(--");
28
+ expect(statusPresentation("error").tokenVar).toContain("var(--status-error)");
29
+ });
30
+
31
+ it("statusAriaLabel names the item and its state", () => {
32
+ expect(statusAriaLabel("Proposal", "done")).toBe("Proposal, done");
33
+ });
34
+ });
@@ -0,0 +1,68 @@
1
+ import { describe, it, expect, afterEach } from "vitest";
2
+ import { render, cleanup, fireEvent } from "@testing-library/react";
3
+ import React, { useRef } from "react";
4
+ import { useFocusTrap } from "../useFocusTrap.js";
5
+
6
+ afterEach(() => cleanup());
7
+
8
+ function Harness({ open }: { open: boolean }) {
9
+ const ref = useRef<HTMLDivElement>(null);
10
+ useFocusTrap(ref, open);
11
+ if (!open) return null;
12
+ return (
13
+ <div ref={ref} tabIndex={-1} data-testid="trap">
14
+ <button data-testid="first">First</button>
15
+ <button data-testid="mid">Mid</button>
16
+ <button data-testid="last">Last</button>
17
+ </div>
18
+ );
19
+ }
20
+
21
+ describe("useFocusTrap", () => {
22
+ it("focuses the first focusable child on open", () => {
23
+ const { getByTestId } = render(<Harness open={true} />);
24
+ expect(document.activeElement).toBe(getByTestId("first"));
25
+ });
26
+
27
+ it("wraps Tab from last to first", () => {
28
+ const { getByTestId } = render(<Harness open={true} />);
29
+ const last = getByTestId("last");
30
+ last.focus();
31
+ fireEvent.keyDown(document, { key: "Tab" });
32
+ expect(document.activeElement).toBe(getByTestId("first"));
33
+ });
34
+
35
+ it("wraps Shift+Tab from first to last", () => {
36
+ const { getByTestId } = render(<Harness open={true} />);
37
+ const first = getByTestId("first");
38
+ first.focus();
39
+ fireEvent.keyDown(document, { key: "Tab", shiftKey: true });
40
+ expect(document.activeElement).toBe(getByTestId("last"));
41
+ });
42
+
43
+ it("restores focus to the previously-focused element on close", () => {
44
+ const outside = document.createElement("button");
45
+ outside.setAttribute("data-testid", "outside");
46
+ document.body.appendChild(outside);
47
+ outside.focus();
48
+ expect(document.activeElement).toBe(outside);
49
+
50
+ const { rerender } = render(<Harness open={true} />);
51
+ // focus moved into the trap
52
+ expect(document.activeElement).not.toBe(outside);
53
+
54
+ rerender(<Harness open={false} />);
55
+ expect(document.activeElement).toBe(outside);
56
+ outside.remove();
57
+ });
58
+
59
+ it("focuses the container itself when no focusable child exists", () => {
60
+ function Empty() {
61
+ const ref = useRef<HTMLDivElement>(null);
62
+ useFocusTrap(ref, true);
63
+ return <div ref={ref} tabIndex={-1} data-testid="empty" />;
64
+ }
65
+ const { getByTestId } = render(<Empty />);
66
+ expect(document.activeElement).toBe(getByTestId("empty"));
67
+ });
68
+ });
@@ -1,5 +1,5 @@
1
- import { describe, it, expect } from "vitest";
2
- import { renderHook, act } from "@testing-library/react";
1
+ import { act, renderHook } from "@testing-library/react";
2
+ import { describe, expect, it } from "vitest";
3
3
  import { useZoomPan } from "../useZoomPan.js";
4
4
 
5
5
  describe("useZoomPan", () => {
@@ -62,3 +62,67 @@ describe("useZoomPan", () => {
62
62
  expect(keys).toContain("onDoubleClick");
63
63
  });
64
64
  });
65
+
66
+ describe("useZoomPan click-vs-drag threshold", () => {
67
+ function makeTarget() {
68
+ let captures = 0;
69
+ const target = {
70
+ setPointerCapture: () => { captures += 1; },
71
+ releasePointerCapture: () => {},
72
+ };
73
+ return { target, captureCount: () => captures };
74
+ }
75
+
76
+ // A press+release with no movement must remain a click: no capture (which would
77
+ // steal the click from a child onClick), no translate.
78
+ it("tap with no movement: no capture, no translate", () => {
79
+ const { result } = renderHook(() => useZoomPan());
80
+ const { target, captureCount } = makeTarget();
81
+ act(() => {
82
+ result.current.handlers.onPointerDown({ button: 0, clientX: 50, clientY: 50, pointerId: 1, currentTarget: target } as never);
83
+ result.current.handlers.onPointerUp();
84
+ });
85
+ expect(captureCount()).toBe(0);
86
+ expect(result.current.state).toEqual({ scale: 1, translateX: 0, translateY: 0 });
87
+ });
88
+
89
+ it("movement at or below threshold (3px): no capture, no translate", () => {
90
+ const { result } = renderHook(() => useZoomPan());
91
+ const { target, captureCount } = makeTarget();
92
+ act(() => {
93
+ result.current.handlers.onPointerDown({ button: 0, clientX: 50, clientY: 50, pointerId: 1, currentTarget: target } as never);
94
+ result.current.handlers.onPointerMove({ clientX: 53, clientY: 50, pointerId: 1, currentTarget: target } as never);
95
+ });
96
+ expect(captureCount()).toBe(0);
97
+ expect(result.current.state.translateX).toBe(0);
98
+ expect(result.current.state.translateY).toBe(0);
99
+ });
100
+
101
+ it("movement beyond threshold: captures once and translates", () => {
102
+ const { result } = renderHook(() => useZoomPan());
103
+ const { target, captureCount } = makeTarget();
104
+ act(() => {
105
+ result.current.handlers.onPointerDown({ button: 0, clientX: 50, clientY: 50, pointerId: 1, currentTarget: target } as never);
106
+ result.current.handlers.onPointerMove({ clientX: 60, clientY: 50, pointerId: 1, currentTarget: target } as never);
107
+ });
108
+ expect(captureCount()).toBe(1);
109
+ expect(result.current.state.translateX).toBe(10);
110
+ // A further move keeps dragging without re-capturing.
111
+ act(() => {
112
+ result.current.handlers.onPointerMove({ clientX: 65, clientY: 50, pointerId: 1, currentTarget: target } as never);
113
+ });
114
+ expect(captureCount()).toBe(1);
115
+ expect(result.current.state.translateX).toBe(15);
116
+ });
117
+
118
+ it("non-primary button does not start a pending drag", () => {
119
+ const { result } = renderHook(() => useZoomPan());
120
+ const { target, captureCount } = makeTarget();
121
+ act(() => {
122
+ result.current.handlers.onPointerDown({ button: 2, clientX: 50, clientY: 50, pointerId: 1, currentTarget: target } as never);
123
+ result.current.handlers.onPointerMove({ clientX: 80, clientY: 50, pointerId: 1, currentTarget: target } as never);
124
+ });
125
+ expect(captureCount()).toBe(0);
126
+ expect(result.current.state.translateX).toBe(0);
127
+ });
128
+ });