Download src/hooks/__tests__/use-swipe.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 4.77 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/hooks/__tests__/use-swipe.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/hooks/__tests__/use-swipe.test.ts
-
curl -L -o use-swipe.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/hooks/__tests__/use-swipe.test.ts
4.77 kB
| 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"); | |
| }); | |
| }); | |
| }); | |