oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1import assert from "node:assert/strict";
2import test from "node:test";
3import { mount } from "./status.mjs";
4
5function region(labels = {}) {
6 const attributes = new Map(Object.entries({"data-state": "idle", ...labels}));
7 let text = "";
8 let writes = 0;
9 return {
10 getAttribute: name => attributes.get(name) ?? null,
11 setAttribute: (name, value) => { attributes.set(name, value); writes++; },
12 get textContent() { return text; },
13 set textContent(value) { text = value; writes++; },
14 set innerHTML(_) { throw new Error("HTML mutation"); },
15 get writes() { return writes; },
16 };
17}
18
19test("mount keeps one region and renders only supplied state text", () => {
20 const element = region({"data-text-saved": "gespeichert", "data-text-saving": "guardando", "data-text-retrying": "再試行", "data-text-failed": "فشل"});
21 const update = mount(element);
22 for (const [state, text] of [["saving", "guardando"], ["retrying", "再試行"], ["failed", "فشل"], ["saved", "gespeichert"], ["idle", ""]]) {
23 update(state);
24 assert.equal(element.textContent, text);
25 assert.equal(element.getAttribute("data-state"), state);
26 const writes = element.writes;
27 update(state);
28 assert.equal(element.writes, writes);
29 }
30});
31
32test("missing messages and unknown states are silent, never success", () => {
33 const element = region({"data-text-saved": "saved fixture"});
34 const update = mount(element);
35 update("saved");
36 for (const state of ["failed", "saving", "retrying", "__proto__", "constructor", "SAVED", null, {toString: () => "saved"}]) {
37 update(state);
38 assert.equal(element.textContent, "");
39 }
40});
41
42test("status containing markup is assigned as text and labels stay stable", () => {
43 const text = '<img src=x onerror="fixture()">';
44 const element = region({"data-state": "failed", "data-text-failed": text});
45 const update = mount(element);
46 assert.equal(element.textContent, text);
47 element.setAttribute("data-text-failed", "changed externally");
48 update("idle");
49 update("failed");
50 assert.equal(element.textContent, text);
51});
52