// Once-only discipline: a page that vendors the bundle twice (or imports the // package through two specifiers) evaluates the module graph again; the // second evaluation must not re-register the element, and the announce path // must not install a second listener set. define() returns the existing // constructor for an already-registered tag; the test proves the whole path // by evaluating a second copy of the package (written into .size-fixtures, // the directory test/setup.ts pre-creates for exactly this) and announcing // through it. import { describe, expect, test } from "bun:test"; import { cpSync } from "node:fs"; const flush = (ms = 0): Promise => new Promise((r) => setTimeout(r, ms)); const TAG = "pebble-toaster"; const COPY = `${import.meta.dir}/../.size-fixtures/toaster-again`; describe("double install", () => { test("a second evaluation of the package is a no-op", async () => { const first = await import("../packages/toaster/index"); const ctor = customElements.get(TAG); expect(ctor).toBeDefined(); // the vendored-twice case: same sources, a distinct module graph cpSync(`${import.meta.dir}/../packages/toaster`, COPY, { recursive: true }); const specifier = `../.size-fixtures/toaster-again/index`; const again = (await import(specifier)) as typeof first; // define() returned the existing constructor; the registry is unchanged expect(customElements.get(TAG)).toBe(ctor); expect(again.announceToast).toBeDefined(); expect(again.announceToast === first.announceToast).toBe(false); // one connected element, one announce through the second copy: one card const host = document.createElement(TAG); document.body.appendChild(host); await flush(20); again.announceToast({ message: "once only", duration: 1000 }); await flush(20); expect(host.querySelectorAll(".toast").length).toBe(1); expect(host.querySelectorAll(".toaster").length).toBe(1); host.remove(); }); test("define() called again for the tag returns the existing constructor", async () => { const { define } = await import("@pebble/pebble"); const ctor = customElements.get(TAG)!; expect(define(TAG, { shadow: false }, () => null)).toBe(ctor); }); });