|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { |
| 3 | + eventTypeToPhase, |
| 4 | + normalizePointerType, |
| 5 | + getButton, |
| 6 | + getDeviceId, |
| 7 | +} from "./utils.js"; |
| 8 | + |
| 9 | +describe("eventTypeToPhase", () => { |
| 10 | + it("should map start events to 'start'", () => { |
| 11 | + expect(eventTypeToPhase("pointerdown")).toBe("start"); |
| 12 | + expect(eventTypeToPhase("mousedown")).toBe("start"); |
| 13 | + expect(eventTypeToPhase("touchstart")).toBe("start"); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should map move events to 'move'", () => { |
| 17 | + expect(eventTypeToPhase("pointermove")).toBe("move"); |
| 18 | + expect(eventTypeToPhase("mousemove")).toBe("move"); |
| 19 | + expect(eventTypeToPhase("touchmove")).toBe("move"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should map end events to 'end'", () => { |
| 23 | + expect(eventTypeToPhase("pointerup")).toBe("end"); |
| 24 | + expect(eventTypeToPhase("mouseup")).toBe("end"); |
| 25 | + expect(eventTypeToPhase("touchend")).toBe("end"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should map cancel events to 'cancel'", () => { |
| 29 | + expect(eventTypeToPhase("pointercancel")).toBe("cancel"); |
| 30 | + expect(eventTypeToPhase("touchcancel")).toBe("cancel"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should default to 'move' for unknown events", () => { |
| 34 | + expect(eventTypeToPhase("unknown")).toBe("move"); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("normalizePointerType", () => { |
| 39 | + it("should normalize known pointer types", () => { |
| 40 | + expect(normalizePointerType("mouse")).toBe("mouse"); |
| 41 | + expect(normalizePointerType("touch")).toBe("touch"); |
| 42 | + expect(normalizePointerType("pen")).toBe("pen"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should return 'unknown' for unrecognized types", () => { |
| 46 | + expect(normalizePointerType("stylus")).toBe("unknown"); |
| 47 | + expect(normalizePointerType("")).toBe("unknown"); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe("getButton", () => { |
| 52 | + it("should return 'none' for move events", () => { |
| 53 | + const event = { type: "pointermove", button: 0 } as PointerEvent; |
| 54 | + |
| 55 | + expect(getButton(event)).toBe("none"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should return button type for non-move events", () => { |
| 59 | + expect(getButton({ type: "pointerdown", button: 0 } as PointerEvent)).toBe( |
| 60 | + "primary" |
| 61 | + ); |
| 62 | + expect(getButton({ type: "pointerdown", button: 2 } as PointerEvent)).toBe( |
| 63 | + "secondary" |
| 64 | + ); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("getDeviceId", () => { |
| 69 | + it("should return pointer-based id for PointerEvent", () => { |
| 70 | + const event = { |
| 71 | + pointerType: "mouse", |
| 72 | + pointerId: 1, |
| 73 | + } as PointerEvent; |
| 74 | + |
| 75 | + expect(getDeviceId(event)).toBe("mouse-1"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return 'touch-device' for TouchEvent", () => { |
| 79 | + const event = { touches: [] } as unknown as TouchEvent; |
| 80 | + |
| 81 | + expect(getDeviceId(event)).toBe("touch-device"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should return 'mouse-device' for MouseEvent", () => { |
| 85 | + const event = {} as MouseEvent; |
| 86 | + |
| 87 | + expect(getDeviceId(event)).toBe("mouse-device"); |
| 88 | + }); |
| 89 | +}); |
0 commit comments