refactor(web): extract timeline touch helpers

This commit is contained in:
2026-04-01 09:48:04 +02:00
parent 167eec31de
commit 3abb3bc865
4 changed files with 151 additions and 30 deletions
+75
View File
@@ -0,0 +1,75 @@
import { describe, expect, it } from "vitest";
import { 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,
});
});
});