70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { commandIds } from "@commands/ids";
|
|
import { createViewportPointerPanHandler, handleViewportWheel } from "./viewport";
|
|
|
|
const ignoredState = undefined as never;
|
|
|
|
describe("viewport input", () => {
|
|
test("wheel dispatches zoom around point", () => {
|
|
const dispatched: unknown[] = [];
|
|
const consumed = handleViewportWheel({
|
|
event: { position: { x: 10, y: 20 }, delta: { x: 0, y: -100 }, altKey: false, ctrlKey: false, metaKey: false, shiftKey: false },
|
|
globalConsumer: () => false,
|
|
currentZoom: 1,
|
|
dispatch: (commandId, payload) => {
|
|
dispatched.push({ commandId, payload });
|
|
return ignoredState;
|
|
},
|
|
});
|
|
|
|
expect(consumed).toBe(true);
|
|
expect(dispatched[0]).toEqual({ commandId: commandIds.viewportZoomAroundPoint, payload: { zoom: Math.exp(0.1), point: { x: 10, y: 20 } } });
|
|
});
|
|
|
|
test("global wheel consumer prevents command dispatch", () => {
|
|
let called = false;
|
|
const consumed = handleViewportWheel({
|
|
event: { position: { x: 0, y: 0 }, delta: { x: 0, y: 1 }, altKey: false, ctrlKey: false, metaKey: false, shiftKey: false },
|
|
globalConsumer: () => true,
|
|
currentZoom: 1,
|
|
dispatch: () => {
|
|
called = true;
|
|
return ignoredState;
|
|
},
|
|
});
|
|
|
|
expect(consumed).toBe(true);
|
|
expect(called).toBe(false);
|
|
});
|
|
|
|
test("middle mouse drag dispatches viewport pan", () => {
|
|
const dispatched: unknown[] = [];
|
|
const pan = createViewportPointerPanHandler({
|
|
globalConsumer: () => false,
|
|
getCurrentZoom: () => 2,
|
|
dispatch: (commandId, payload) => {
|
|
dispatched.push({ commandId, payload });
|
|
return ignoredState;
|
|
},
|
|
});
|
|
|
|
expect(pan.pointerDown(basePointer({ buttons: 4, position: { x: 10, y: 10 } }))).toBe(true);
|
|
expect(pan.pointerMove(basePointer({ buttons: 4, position: { x: 14, y: 6 } }))).toBe(true);
|
|
expect(dispatched[0]).toEqual({ commandId: commandIds.viewportPan, payload: { delta: { x: -2, y: 2 } } });
|
|
});
|
|
});
|
|
|
|
function basePointer(overrides: Partial<Parameters<ReturnType<typeof createViewportPointerPanHandler>["pointerDown"]>[0]> = {}) {
|
|
return {
|
|
pointerId: 1,
|
|
pointerType: "mouse" as const,
|
|
position: { x: 0, y: 0 },
|
|
buttons: 0,
|
|
altKey: false,
|
|
ctrlKey: false,
|
|
metaKey: false,
|
|
shiftKey: false,
|
|
...overrides,
|
|
};
|
|
}
|