Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

selectionActions.browser.test.ts

48 lines1.6 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import { fakeTimers } from "../../test/render";
4import { observeSelectionActions, SELECTION_MULTI_CLICK_INTERVAL_MS } from "./selectionActions";
5​
6describe("observeSelectionActions", () => {
7 it.each([
8 { immediateMultiClick: false, nextFrame: [], afterInterval: [[{ x: 40, y: 12 }]] },
9 { immediateMultiClick: true, nextFrame: [[null]], afterInterval: [[null]] },
10 ])(
11 "double-click with immediateMultiClick=$immediateMultiClick",
12 ({ immediateMultiClick, nextFrame, afterInterval }) => {
13 using stack = new DisposableStack();
14 const element = stack.adopt(document.body.appendChild(document.createElement("div")), (e) =>
15 e.remove(),
16 );
17 stack.use(fakeTimers());
18 const onSelection = vi.fn();
19 const actions = observeSelectionActions({
20 element,
21 immediateMultiClick,
22 onSelection,
23 onDismiss: () => {},
24 });
25 stack.defer(() => actions.dispose());
26​
27 element.dispatchEvent(
28 new PointerEvent("pointerdown", { bubbles: true, isPrimary: true, button: 0 }),
29 );
30 element.dispatchEvent(
31 new MouseEvent("mouseup", {
32 bubbles: true,
33 button: 0,
34 detail: 2,
35 clientX: 40,
36 clientY: 12,
37 }),
38 );
39​
40 vi.advanceTimersByTime(20);
41 expect(onSelection.mock.calls).toEqual(nextFrame);
42 vi.advanceTimersByTime(SELECTION_MULTI_CLICK_INTERVAL_MS - 21);
43 expect(onSelection.mock.calls).toEqual(nextFrame);
44 vi.advanceTimersByTime(50);
45 expect(onSelection.mock.calls).toEqual(afterInterval);
46 },
47 );
48});