97 lines
2.6 KiB
TypeScript
97 lines
2.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { createTouchMouseDownEvent, getTouchPoint, resolveTouchDragDecision } from "./timelineTouch.js";
|
|
|
|
describe("timelineTouch", () => {
|
|
it("falls back from active touches to changedTouches and then zeroes", () => {
|
|
expect(
|
|
getTouchPoint({
|
|
touches: [{ clientX: 12, clientY: 18 }],
|
|
changedTouches: [{ clientX: 40, clientY: 55 }],
|
|
}),
|
|
).toEqual({ clientX: 12, clientY: 18 });
|
|
|
|
expect(
|
|
getTouchPoint({
|
|
touches: [],
|
|
changedTouches: [{ clientX: 40, clientY: 55 }],
|
|
}),
|
|
).toEqual({ clientX: 40, clientY: 55 });
|
|
|
|
expect(
|
|
getTouchPoint({
|
|
touches: [],
|
|
changedTouches: [],
|
|
}),
|
|
).toEqual({ clientX: 0, clientY: 0 });
|
|
});
|
|
|
|
it("stays undecided and ignores tiny movements below the threshold", () => {
|
|
expect(
|
|
resolveTouchDragDecision(
|
|
{ x: 100, y: 100, decided: false },
|
|
{ clientX: 106, clientY: 107 },
|
|
),
|
|
).toEqual({
|
|
nextState: { x: 100, y: 100, decided: false },
|
|
shouldHandleDrag: false,
|
|
});
|
|
});
|
|
|
|
it("lets vertical scrolling win once movement is mostly vertical", () => {
|
|
expect(
|
|
resolveTouchDragDecision(
|
|
{ x: 100, y: 100, decided: false },
|
|
{ clientX: 106, clientY: 118 },
|
|
),
|
|
).toEqual({
|
|
nextState: { x: 100, y: 100, decided: true },
|
|
shouldHandleDrag: false,
|
|
});
|
|
});
|
|
|
|
it("switches to drag handling once horizontal movement wins", () => {
|
|
expect(
|
|
resolveTouchDragDecision(
|
|
{ x: 100, y: 100, decided: false },
|
|
{ clientX: 118, clientY: 106 },
|
|
),
|
|
).toEqual({
|
|
nextState: { x: 100, y: 100, decided: true },
|
|
shouldHandleDrag: true,
|
|
});
|
|
});
|
|
|
|
it("keeps handling drag after the decision was already made", () => {
|
|
expect(
|
|
resolveTouchDragDecision(
|
|
{ x: 100, y: 100, decided: true },
|
|
{ clientX: 101, clientY: 140 },
|
|
),
|
|
).toEqual({
|
|
nextState: { x: 100, y: 100, decided: true },
|
|
shouldHandleDrag: true,
|
|
});
|
|
});
|
|
|
|
it("creates a primary-button mouse-down adapter for touch handlers", () => {
|
|
const currentTarget = new EventTarget();
|
|
|
|
expect(createTouchMouseDownEvent({ clientX: 42, clientY: 17 }, currentTarget)).toMatchObject({
|
|
button: 0,
|
|
clientX: 42,
|
|
currentTarget,
|
|
shiftKey: false,
|
|
});
|
|
});
|
|
|
|
it("allows missing targets when creating a touch mouse-down adapter", () => {
|
|
const event = createTouchMouseDownEvent({ clientX: 7, clientY: 9 });
|
|
|
|
expect(event.currentTarget).toBeNull();
|
|
expect(() => {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
}).not.toThrow();
|
|
});
|
|
});
|