import type ymaps from "yandex-maps"; import assert from "node:assert"; import { describe, test, } from "node:test"; import { handlePreventableEvent } from "./index"; type TEventStub = ymaps.IEvent; type TSourceEventStub = Pick; const getEventStub = (): TEventStub => { let isPrevented = false; const sourceEvent: TSourceEventStub = { isDefaultPrevented: () => isPrevented, preventDefault: () => { isPrevented = true; return isPrevented; }, }; return { getSourceEvent: () => sourceEvent as TEventStub, } as TEventStub; }; describe("handlePreventableEvent", () => { test("runs default action when no custom action provided", () => { const event = getEventStub(); let isDefaultCalled = false; handlePreventableEvent({ event, defaultFn: () => { isDefaultCalled = true; }, }); assert.strictEqual(isDefaultCalled, true); }); test("runs custom action first and then default when not prevented", () => { const event = getEventStub(); const calls: string[] = []; handlePreventableEvent({ event, action: () => { calls.push("action"); }, defaultFn: () => { calls.push("default"); }, }); assert.deepStrictEqual(calls, [ "action", "default" ]); }); test("skips default action when preventDefault is called in custom action", () => { const event = getEventStub(); const calls: string[] = []; handlePreventableEvent({ event, action: () => { calls.push("action"); event.getSourceEvent()?.preventDefault(); }, defaultFn: () => { calls.push("default"); }, }); assert.deepStrictEqual(calls, [ "action" ]); }); });