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
212 lignes · 10950 octetsBrut · Historique
1// <pebble-timeline> as an element: it reads its table, draws lanes, follows
2// the table when the page rewrites it, steps with the keys and emits select.
3// The engine's arithmetic is proven in packages/timeline/{chart,engine}/*.test.ts;
4// geometry in a real browser is the headless-Chromium smoke's job
5// (examples/timeline-smoke.ts).
6import { afterEach, expect, test } from "bun:test";
7import "../packages/timeline/index";
8import properties from "../packages/timeline/properties.css";
9import styles from "../packages/timeline/timeline.css";
10
11const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
12
13const TABLE = `<table>
14 <caption>Two schedules</caption>
15 <thead><tr><th>Method</th><th>Phase</th><th>Starts</th><th>Days</th><th>Source</th></tr></thead>
16 <tbody>
17 <tr><th>Standard</th><td>Rooting</td><td>0</td><td>10</td><td>Fig. 2</td></tr>
18 <tr data-texture="hatch"><th>Standard</th><td>Recovery</td><td>10</td><td>14</td><td>Fig. 2</td></tr>
19 <tr><th>Standard</th><td>Flowering</td><td>24</td><td>56</td><td>Fig. 2</td></tr>
20 <tr data-series="muted"><th>Early</th><td>Wait</td><td>−14</td><td>14</td><td>Abstract</td></tr>
21 <tr><th>Early</th><td>Rooting</td><td>0</td><td>10</td><td>Fig. 2</td></tr>
22 <tr><th>Early</th><td>Flowering</td><td>10</td><td>56</td><td>Fig. 2</td></tr>
23 </tbody>
24</table>`;
25
26const mount = (attrs = 'min="-14" max="84" step="14" per="7" unit="wk"', html = TABLE) => {
27 const host = document.createElement("div");
28 host.innerHTML = `<pebble-timeline ${attrs}>${html}</pebble-timeline>`;
29 document.body.appendChild(host);
30 return host.querySelector("pebble-timeline") as HTMLElement;
31};
32
33const key = (el: Element, k: string) =>
34 el.dispatchEvent(new KeyboardEvent("keydown", { key: k, bubbles: true, cancelable: true }));
35
36afterEach(async () => {
37 document.body.textContent = "";
38 await sleep(0); // define() releases one macrotask later
39});
40
41test("draws a lane per track and a segment per row, and keeps the table", () => {
42 const el = mount();
43 expect(el.getAttribute("data-enhanced")).toBe("");
44 expect([...el.querySelectorAll(".track-label")].map(n => n.textContent)).toEqual(["Standard", "Early"]);
45 expect(el.querySelectorAll(".lane").length).toBe(2);
46 expect(el.querySelectorAll(".segment").length).toBe(6);
47 expect(el.querySelector("table")).not.toBeNull();
48 expect(el.querySelector(".plot")!.getAttribute("aria-label")).toBe("Two schedules");
49});
50
51test("segments carry their series, texture and ends; positions are fractions of the lane", () => {
52 const el = mount();
53 const segments = [...el.querySelectorAll<HTMLElement>(".segment")];
54 expect(segments.map(s => s.dataset.series)).toEqual(["1", "2", "3", "muted", "1", "3"]);
55 expect(segments[1]!.dataset.texture).toBe("hatch");
56 expect(segments[0]!.hasAttribute("data-first")).toBe(true);
57 expect(segments[2]!.hasAttribute("data-last")).toBe(true);
58 // Rooting starts at day 0 on a -14…84 axis: 14/98 of the lane.
59 expect(Number(segments[0]!.style.getPropertyValue("--pebble-timeline-start"))).toBeCloseTo(14 / 98);
60 expect(Number(segments[0]!.style.getPropertyValue("--pebble-timeline-size"))).toBeCloseTo(10 / 98);
61});
62
63test("totals in the page's unit, the difference from the first track, and the legend", () => {
64 const el = mount();
65 const totals = [...el.querySelectorAll(".total")].map(n => n.textContent!.replace(/\s+/g, " ").trim());
66 expect(totals).toEqual(["11.4 wk", "9.4 wk −2 wk"]);
67 expect([...el.querySelectorAll(".legend li")].map(n => n.textContent)).toEqual(["Rooting", "Recovery", "Flowering", "Wait"]);
68 expect([...el.querySelectorAll(".tick")].map(n => n.textContent)).toEqual(["−2", "0", "2", "4", "6", "8", "10", "12"]);
69});
70
71test("rewriting the table's rows redraws the lanes", async () => {
72 const el = mount();
73 el.querySelector("tbody")!.lastElementChild!.remove();
74 await sleep(0); // MutationObserver callbacks are microtasks
75 expect(el.querySelectorAll(".segment").length).toBe(5);
76});
77
78test("a re-read keeps each segment's element: a changed row moves it, it is not redrawn", async () => {
79 const el = mount();
80 const before = [...el.querySelectorAll<HTMLElement>(".segment")];
81 const labels = [...el.querySelectorAll(".track-label")];
82 // The recovery shortens from 14 days to 7, and flowering starts a week earlier.
83 const rows = el.querySelectorAll("tbody > tr");
84 rows[1]!.children[3]!.textContent = "7";
85 rows[2]!.children[2]!.textContent = "17";
86 await sleep(0);
87 const after = [...el.querySelectorAll<HTMLElement>(".segment")];
88 expect(after).toHaveLength(6);
89 after.forEach((segment, i) => expect(segment).toBe(before[i]!));
90 expect([...el.querySelectorAll(".track-label")]).toEqual(labels);
91 expect(Number(after[1]!.style.getPropertyValue("--pebble-timeline-size"))).toBeCloseTo(7 / 98);
92 expect(Number(after[2]!.style.getPropertyValue("--pebble-timeline-start"))).toBeCloseTo(31 / 98);
93});
94
95test("with show-table the page's table moves into a labelled scroll region, and back where it was", async () => {
96 const el = mount('show-table min="-14" max="84"', `${TABLE}<p>A note the page keeps after its table</p>`);
97 const table = el.querySelector("table")!;
98 const next = el.querySelector("p")!;
99 expect(table.nextSibling).toBe(next);
100 const view = el.querySelector<HTMLElement>(".timeline > .table-view")!;
101 // Not before the element has inserted what it renders: define() keeps the children it found.
102 expect(table.parentNode).toBe(el);
103 await sleep(0);
104 expect(table.parentNode).toBe(view);
105 expect(view.previousElementSibling!.classList.contains("plot")).toBe(true);
106 expect(view.hidden).toBe(false);
107 expect([view.getAttribute("role"), view.getAttribute("tabindex"), view.getAttribute("aria-label")]).toEqual(["region", "0", "Two schedules"]);
108 // The same node, still observed: a rewritten row is read.
109 table.querySelector("tbody")!.lastElementChild!.remove();
110 await sleep(0);
111 expect(el.querySelectorAll(".segment").length).toBe(5);
112 // The attribute goes: the table returns where it was, and the region hides.
113 el.removeAttribute("show-table");
114 expect(table.parentNode).toBe(el);
115 expect(table.nextSibling).toBe(next);
116 expect(view.hidden).toBe(true);
117 el.setAttribute("show-table", "");
118 expect(table.parentNode).toBe(view);
119 // Disconnected: the table is the page's again, where it was, and the element's nodes are gone.
120 el.remove();
121 await sleep(0);
122 expect(table.parentNode).toBe(el);
123 expect(el.firstElementChild).toBe(table);
124 expect(table.nextSibling).toBe(next);
125 expect(el.lastElementChild).toBe(next);
126 expect(el.querySelector(".timeline")).toBeNull();
127});
128
129test("a caption with an id labels the region by reference", async () => {
130 const el = mount("show-table", TABLE.replace("<caption>", '<caption id="schedules">'));
131 await sleep(0);
132 const view = el.querySelector(".table-view")!;
133 expect(view.getAttribute("aria-labelledby")).toBe("schedules");
134 expect(view.hasAttribute("aria-label")).toBe(false);
135});
136
137test("the table view's sheet scrolls the table rather than squeezing it", () => {
138 // Only a text match on the shipped sheet, as a tripwire for a minifier or edit that drops the
139 // rules: happy-dom applies no @scope sheet and lays nothing out. That the region scrolls, the
140 // page never does and words stay whole is asserted in Chromium (examples/timeline-smoke.ts).
141 expect(styles).toMatch(/\.table-view\{[^}]*overflow-x:auto/);
142 expect(styles).toMatch(/animation-timeline:scroll\(self inline\)/);
143 expect(styles).not.toMatch(/table-layout|overflow-wrap:anywhere|hyphens:auto|data-table-fit/);
144});
145
146test("--pebble-timeline-start and -size are registered in a sheet of their own, which define() does not scope", () => {
147 // @property is ignored inside @scope, and define() wraps strings in one: the registrations are a
148 // CSSStyleSheet the element passes as is. That Chromium registers them is the smoke's.
149 for (const name of ["--pebble-timeline-start", "--pebble-timeline-size"])
150 expect(properties).toContain(`@property ${name}{syntax:"<number>";inherits:false;initial-value:0}`);
151 expect(properties).not.toMatch(/@property --(start|size)\b/);
152 expect(styles).not.toContain("@property");
153});
154
155test("segment rows are marked for the table view: their palette slot, and where a track starts", async () => {
156 const el = mount();
157 const rows = [...el.querySelectorAll<HTMLElement>("tbody > tr")];
158 const segments = [...el.querySelectorAll<HTMLElement>(".segment")];
159 // Every row's swatch matches its segment, including rows whose slot came from their label.
160 expect(rows.map(r => r.dataset.fill)).toEqual(segments.map(s => s.dataset.series!));
161 expect(rows.map(r => r.hasAttribute("data-track-start"))).toEqual([true, false, false, true, false, false]);
162 // A row that stops being a segment loses its marks; the marks never trigger a re-read.
163 rows[3]!.children[2]!.textContent = "";
164 await sleep(0);
165 expect(rows[3]!.hasAttribute("data-fill")).toBe(false);
166 expect(rows[3]!.hasAttribute("data-track-start")).toBe(false);
167 expect(rows[4]!.hasAttribute("data-track-start")).toBe(true);
168 expect(el.querySelectorAll(".segment").length).toBe(5);
169 el.remove();
170 await sleep(0);
171 expect(rows.some(r => r.hasAttribute("data-fill") || r.hasAttribute("data-track-start"))).toBe(false);
172});
173
174test("the keys step through segments and fill the tooltip; Escape lets go", () => {
175 const el = mount();
176 const plot = el.querySelector<HTMLElement>(".plot")!;
177 const tip = el.querySelector<HTMLElement>(".tooltip")!;
178 key(plot, "ArrowRight");
179 key(plot, "ArrowRight");
180 expect(tip.hasAttribute("data-open")).toBe(true);
181 expect(tip.querySelector(".heading")!.textContent).toBe("Recovery");
182 expect([...tip.querySelectorAll("li")].map(li => li.textContent)).toEqual(["10 Starts", "14 Days", "Fig. 2 Source"]);
183 expect(el.querySelectorAll(".segment")[1]!.hasAttribute("data-active")).toBe(true);
184 key(plot, "Escape");
185 expect(tip.hasAttribute("data-open")).toBe(false);
186});
187
188test("Enter on the active segment, or a click, emits select", () => {
189 const el = mount();
190 const seen: unknown[] = [];
191 el.addEventListener("select", event => seen.push((event as CustomEvent).detail));
192 const plot = el.querySelector<HTMLElement>(".plot")!;
193 key(plot, "End");
194 key(plot, "Enter");
195 el.querySelectorAll<HTMLElement>(".segment")[0]!.click();
196 expect(seen).toEqual([
197 { index: 5, track: "Early", label: "Flowering" },
198 { index: 0, track: "Standard", label: "Rooting" },
199 ]);
200});
201
202test("keys pressed outside the plot are not the chart's", () => {
203 const el = mount();
204 key(el.querySelector(".legend")!, "ArrowRight");
205 expect(el.querySelector(".tooltip")!.hasAttribute("data-open")).toBe(false);
206});
207
208test("without a table the element leaves its children alone", () => {
209 const el = mount("", "<p>No data</p>");
210 expect(el.innerHTML).toBe("<p>No data</p>");
211 expect(el.hasAttribute("data-enhanced")).toBe(false);
212});