import { describe, it, expect } from "vitest"; import { detectSwipe } from "../use-swipe"; import type { SwipePoint } from "../use-swipe"; const pt = (x: number, y: number): SwipePoint => ({ x, y }); describe("detectSwipe", () => { // ── left swipe ────────────────────────────────────────────────────────── describe("left swipe", () => { it("returns 'left' for a clean horizontal left swipe", () => { expect(detectSwipe(pt(300, 100), pt(200, 102))).toBe("left"); }); it("returns 'left' at exactly the threshold distance", () => { // deltaX = -60, default threshold = 60 expect(detectSwipe(pt(200, 100), pt(140, 100))).toBe("left"); }); it("returns null when deltaX is 1px below threshold", () => { // deltaX = -59, threshold = 60 expect(detectSwipe(pt(200, 100), pt(141, 100))).toBeNull(); }); }); // ── right swipe ───────────────────────────────────────────────────────── describe("right swipe", () => { it("returns 'right' for a clean horizontal right swipe", () => { expect(detectSwipe(pt(100, 200), pt(220, 205))).toBe("right"); }); it("returns 'right' at exactly the threshold distance", () => { // deltaX = +60 expect(detectSwipe(pt(100, 200), pt(160, 200))).toBe("right"); }); it("returns null when deltaX is 1px below threshold", () => { // deltaX = +59 expect(detectSwipe(pt(100, 200), pt(159, 200))).toBeNull(); }); }); // ── vertical drift guard ───────────────────────────────────────────────── describe("vertical drift guard", () => { it("returns null when vertical ratio exceeds maxVerticalRatio", () => { // deltaX = -80, deltaY = 60 → ratio = 0.75 > 0.5 expect( detectSwipe(pt(300, 100), pt(220, 160), { maxVerticalRatio: 0.5 }) ).toBeNull(); }); it("returns direction when vertical drift is within ratio", () => { // deltaX = -100, deltaY = 30 → ratio = 0.3 < 0.5 expect( detectSwipe(pt(300, 100), pt(200, 130), { maxVerticalRatio: 0.5 }) ).toBe("left"); }); it("returns null at exactly the maxVerticalRatio boundary (exclusive)", () => { // deltaX = -100, deltaY = 50 → ratio = 0.5, NOT > 0.5 → allowed expect( detectSwipe(pt(300, 100), pt(200, 150), { maxVerticalRatio: 0.5 }) ).toBe("left"); }); it("blocks a nearly-vertical diagonal despite sufficient deltaX", () => { // deltaX = -80, deltaY = 200 → ratio = 2.5 > 0.5 expect(detectSwipe(pt(300, 100), pt(220, 300))).toBeNull(); }); }); // ── custom threshold ───────────────────────────────────────────────────── describe("custom threshold", () => { it("respects a larger custom threshold", () => { // 100px — below custom threshold of 120 expect( detectSwipe(pt(300, 100), pt(200, 102), { threshold: 120 }) ).toBeNull(); }); it("fires when distance exceeds custom threshold", () => { // 130px — above custom threshold of 120 expect( detectSwipe(pt(300, 100), pt(170, 102), { threshold: 120 }) ).toBe("left"); }); it("respects a smaller custom threshold", () => { // 25px — above custom threshold of 20 expect( detectSwipe(pt(100, 100), pt(125, 100), { threshold: 20 }) ).toBe("right"); }); }); // ── edge cases ─────────────────────────────────────────────────────────── describe("edge cases", () => { it("returns null for a stationary touch (no movement)", () => { expect(detectSwipe(pt(200, 200), pt(200, 200))).toBeNull(); }); it("returns null for a purely vertical swipe", () => { // deltaX = 0, deltaY = 200 expect(detectSwipe(pt(200, 100), pt(200, 300))).toBeNull(); }); it("uses defaults when options are not provided", () => { // default threshold = 60, maxVerticalRatio = 0.5 expect(detectSwipe(pt(300, 100), pt(200, 102))).toBe("left"); }); it("handles large swipe distances correctly", () => { expect(detectSwipe(pt(1000, 300), pt(100, 305))).toBe("left"); expect(detectSwipe(pt(100, 300), pt(1000, 305))).toBe("right"); }); }); });