tobogganSe connecter

library

Prebuilt pebble components: drop-in custom elements, no compiler needed

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Dernier runAucun
0Étoiles
0Forks
2Issues
50 lignes · 2253 octetsBrut · Historique
1// Once-only discipline: a page that vendors the bundle twice (or imports the
2// package through two specifiers) evaluates the module graph again; the
3// second evaluation must not re-register the element, and the announce path
4// must not install a second listener set. define() returns the existing
5// constructor for an already-registered tag; the test proves the whole path
6// by evaluating a second copy of the package (written into .size-fixtures,
7// the directory test/setup.ts pre-creates for exactly this) and announcing
8// through it.
9
10import { describe, expect, test } from "bun:test";
11import { cpSync } from "node:fs";
12
13const flush = (ms = 0): Promise<void> => new Promise((r) => setTimeout(r, ms));
14
15const TAG = "pebble-toaster";
16const COPY = `${import.meta.dir}/../.size-fixtures/toaster-again`;
17
18describe("double install", () => {
19 test("a second evaluation of the package is a no-op", async () => {
20 const first = await import("../packages/toaster/index");
21 const ctor = customElements.get(TAG);
22 expect(ctor).toBeDefined();
23
24 // the vendored-twice case: same sources, a distinct module graph
25 cpSync(`${import.meta.dir}/../packages/toaster`, COPY, { recursive: true });
26 const specifier = `../.size-fixtures/toaster-again/index`;
27 const again = (await import(specifier)) as typeof first;
28
29 // define() returned the existing constructor; the registry is unchanged
30 expect(customElements.get(TAG)).toBe(ctor);
31 expect(again.announceToast).toBeDefined();
32 expect(again.announceToast === first.announceToast).toBe(false);
33
34 // one connected element, one announce through the second copy: one card
35 const host = document.createElement(TAG);
36 document.body.appendChild(host);
37 await flush(20);
38 again.announceToast({ message: "once only", duration: 1000 });
39 await flush(20);
40 expect(host.querySelectorAll(".toast").length).toBe(1);
41 expect(host.querySelectorAll(".toaster").length).toBe(1);
42 host.remove();
43 });
44
45 test("define() called again for the tag returns the existing constructor", async () => {
46 const { define } = await import("@pebble/pebble");
47 const ctor = customElements.get(TAG)!;
48 expect(define(TAG, { shadow: false }, () => null)).toBe(ctor);
49 });
50});