import assert from "node:assert"; import { afterEach, describe, it, mock, } from "node:test"; import { Modals } from "./index"; describe("Modals plugin", () => { const originalInstance = Modals.instance; const originalFancybox = Modals.fancybox; afterEach(() => { Modals.instance = originalInstance; Modals.fancybox = originalFancybox; mock.restoreAll(); }); it("exposes reasons and closed default state", () => { assert.equal(typeof Modals.reasons.open, "string"); assert.equal(Modals.isOpen(), false); }); it("resolves close when requested id is missing and no active instances", async () => { Modals.instance = {} as Modals; Modals.fancybox = { getInstance: () => null, close: () => null, } as unknown as typeof Modals.fancybox; const result = await Modals.getClose("unknown-modal"); assert.equal(result.reason, Modals.reasons.close); assert.equal(result.instance, null); }); it("falls back to top-most instance when requested id is missing", async () => { let closeCalls = 0; const fakeInstance = { getContainer: () => null, close: () => { closeCalls += 1; }, }; Modals.instance = {} as Modals; Modals.fancybox = { getInstance: (id?: string) => { if (id) { return null; } return fakeInstance; }, close: () => null, } as unknown as typeof Modals.fancybox; const result = await Modals.getClose("unknown-modal"); assert.equal(result.reason, Modals.reasons.close); assert.equal(result.instance, fakeInstance); assert.equal(closeCalls, 1); }); });