// @vitest-environment jsdom import type { JSX } from "solid-js"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { VaultEntry } from "../../shared/types"; import { renderView } from "../views/view-test-utils"; import { NoteRow } from "./NoteRow"; const note = { id: "note-1", type: "note", title: "Hybrid input", modifiedAt: Date.now(), } as VaultEntry; function touchEvent(type: string, x: number, y: number): TouchEvent { const event = new Event(type, { bubbles: true, cancelable: true }) as TouchEvent; const touch = { clientX: x, clientY: y } as Touch; Object.defineProperties(event, { touches: { value: type === "touchend" ? [] : [touch] }, changedTouches: { value: [touch] }, }); return event; } describe("NoteRow input", () => { const cleanups: Array<() => void> = []; afterEach(() => { while (cleanups.length) cleanups.pop()!(); }); function mounted(props: { onTap: () => void; onArchive?: () => void }) { const view = renderView(() => ( {}} onArchive={props.onArchive ?? (() => {})} /> ) as JSX.Element); cleanups.push(view.dispose); return view.container.querySelector(".peach-note-row") as HTMLDivElement; } it("opens once when a touch tap produces its click", () => { const onTap = vi.fn(); const row = mounted({ onTap }); row.dispatchEvent(touchEvent("touchstart", 20, 20)); row.dispatchEvent(touchEvent("touchend", 20, 20)); row.click(); expect(onTap).toHaveBeenCalledTimes(1); }); it("accepts a mouse click even when the browser supports touch", () => { const onTap = vi.fn(); const row = mounted({ onTap }); Object.defineProperty(window, "ontouchstart", { configurable: true, value: null }); row.click(); expect(onTap).toHaveBeenCalledTimes(1); }); it("does not open after a swipe click", () => { const onTap = vi.fn(); const onArchive = vi.fn(); const row = mounted({ onTap, onArchive }); row.dispatchEvent(touchEvent("touchstart", 10, 20)); row.dispatchEvent(touchEvent("touchmove", 140, 20)); row.dispatchEvent(touchEvent("touchmove", 140, 20)); row.dispatchEvent(touchEvent("touchend", 140, 20)); row.click(); expect(onArchive).toHaveBeenCalledTimes(1); expect(onTap).not.toHaveBeenCalled(); }); });