// as an element: it reads its table, draws lanes, follows // the table when the page rewrites it, steps with the keys and emits select. // The engine's arithmetic is proven in packages/timeline/{chart,engine}/*.test.ts; // geometry in a real browser is the headless-Chromium smoke's job // (examples/timeline-smoke.ts). import { afterEach, expect, test } from "bun:test"; import "../packages/timeline/index"; import properties from "../packages/timeline/properties.css"; import styles from "../packages/timeline/timeline.css"; const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); const TABLE = `
Two schedules
MethodPhaseStartsDaysSource
StandardRooting010Fig. 2
StandardRecovery1014Fig. 2
StandardFlowering2456Fig. 2
EarlyWait−1414Abstract
EarlyRooting010Fig. 2
EarlyFlowering1056Fig. 2
`; const mount = (attrs = 'min="-14" max="84" step="14" per="7" unit="wk"', html = TABLE) => { const host = document.createElement("div"); host.innerHTML = `${html}`; document.body.appendChild(host); return host.querySelector("pebble-timeline") as HTMLElement; }; const key = (el: Element, k: string) => el.dispatchEvent(new KeyboardEvent("keydown", { key: k, bubbles: true, cancelable: true })); afterEach(async () => { document.body.textContent = ""; await sleep(0); // define() releases one macrotask later }); test("draws a lane per track and a segment per row, and keeps the table", () => { const el = mount(); expect(el.getAttribute("data-enhanced")).toBe(""); expect([...el.querySelectorAll(".track-label")].map(n => n.textContent)).toEqual(["Standard", "Early"]); expect(el.querySelectorAll(".lane").length).toBe(2); expect(el.querySelectorAll(".segment").length).toBe(6); expect(el.querySelector("table")).not.toBeNull(); expect(el.querySelector(".plot")!.getAttribute("aria-label")).toBe("Two schedules"); }); test("segments carry their series, texture and ends; positions are fractions of the lane", () => { const el = mount(); const segments = [...el.querySelectorAll(".segment")]; expect(segments.map(s => s.dataset.series)).toEqual(["1", "2", "3", "muted", "1", "3"]); expect(segments[1]!.dataset.texture).toBe("hatch"); expect(segments[0]!.hasAttribute("data-first")).toBe(true); expect(segments[2]!.hasAttribute("data-last")).toBe(true); // Rooting starts at day 0 on a -14…84 axis: 14/98 of the lane. expect(Number(segments[0]!.style.getPropertyValue("--pebble-timeline-start"))).toBeCloseTo(14 / 98); expect(Number(segments[0]!.style.getPropertyValue("--pebble-timeline-size"))).toBeCloseTo(10 / 98); }); test("totals in the page's unit, the difference from the first track, and the legend", () => { const el = mount(); const totals = [...el.querySelectorAll(".total")].map(n => n.textContent!.replace(/\s+/g, " ").trim()); expect(totals).toEqual(["11.4 wk", "9.4 wk −2 wk"]); expect([...el.querySelectorAll(".legend li")].map(n => n.textContent)).toEqual(["Rooting", "Recovery", "Flowering", "Wait"]); expect([...el.querySelectorAll(".tick")].map(n => n.textContent)).toEqual(["−2", "0", "2", "4", "6", "8", "10", "12"]); }); test("rewriting the table's rows redraws the lanes", async () => { const el = mount(); el.querySelector("tbody")!.lastElementChild!.remove(); await sleep(0); // MutationObserver callbacks are microtasks expect(el.querySelectorAll(".segment").length).toBe(5); }); test("a re-read keeps each segment's element: a changed row moves it, it is not redrawn", async () => { const el = mount(); const before = [...el.querySelectorAll(".segment")]; const labels = [...el.querySelectorAll(".track-label")]; // The recovery shortens from 14 days to 7, and flowering starts a week earlier. const rows = el.querySelectorAll("tbody > tr"); rows[1]!.children[3]!.textContent = "7"; rows[2]!.children[2]!.textContent = "17"; await sleep(0); const after = [...el.querySelectorAll(".segment")]; expect(after).toHaveLength(6); after.forEach((segment, i) => expect(segment).toBe(before[i]!)); expect([...el.querySelectorAll(".track-label")]).toEqual(labels); expect(Number(after[1]!.style.getPropertyValue("--pebble-timeline-size"))).toBeCloseTo(7 / 98); expect(Number(after[2]!.style.getPropertyValue("--pebble-timeline-start"))).toBeCloseTo(31 / 98); }); test("with show-table the page's table moves into a labelled scroll region, and back where it was", async () => { const el = mount('show-table min="-14" max="84"', `${TABLE}

A note the page keeps after its table

`); const table = el.querySelector("table")!; const next = el.querySelector("p")!; expect(table.nextSibling).toBe(next); const view = el.querySelector(".timeline > .table-view")!; // Not before the element has inserted what it renders: define() keeps the children it found. expect(table.parentNode).toBe(el); await sleep(0); expect(table.parentNode).toBe(view); expect(view.previousElementSibling!.classList.contains("plot")).toBe(true); expect(view.hidden).toBe(false); expect([view.getAttribute("role"), view.getAttribute("tabindex"), view.getAttribute("aria-label")]).toEqual(["region", "0", "Two schedules"]); // The same node, still observed: a rewritten row is read. table.querySelector("tbody")!.lastElementChild!.remove(); await sleep(0); expect(el.querySelectorAll(".segment").length).toBe(5); // The attribute goes: the table returns where it was, and the region hides. el.removeAttribute("show-table"); expect(table.parentNode).toBe(el); expect(table.nextSibling).toBe(next); expect(view.hidden).toBe(true); el.setAttribute("show-table", ""); expect(table.parentNode).toBe(view); // Disconnected: the table is the page's again, where it was, and the element's nodes are gone. el.remove(); await sleep(0); expect(table.parentNode).toBe(el); expect(el.firstElementChild).toBe(table); expect(table.nextSibling).toBe(next); expect(el.lastElementChild).toBe(next); expect(el.querySelector(".timeline")).toBeNull(); }); test("a caption with an id labels the region by reference", async () => { const el = mount("show-table", TABLE.replace("", '')); await sleep(0); const view = el.querySelector(".table-view")!; expect(view.getAttribute("aria-labelledby")).toBe("schedules"); expect(view.hasAttribute("aria-label")).toBe(false); }); test("the table view's sheet scrolls the table rather than squeezing it", () => { // Only a text match on the shipped sheet, as a tripwire for a minifier or edit that drops the // rules: happy-dom applies no @scope sheet and lays nothing out. That the region scrolls, the // page never does and words stay whole is asserted in Chromium (examples/timeline-smoke.ts). expect(styles).toMatch(/\.table-view\{[^}]*overflow-x:auto/); expect(styles).toMatch(/animation-timeline:scroll\(self inline\)/); expect(styles).not.toMatch(/table-layout|overflow-wrap:anywhere|hyphens:auto|data-table-fit/); }); test("--pebble-timeline-start and -size are registered in a sheet of their own, which define() does not scope", () => { // @property is ignored inside @scope, and define() wraps strings in one: the registrations are a // CSSStyleSheet the element passes as is. That Chromium registers them is the smoke's. for (const name of ["--pebble-timeline-start", "--pebble-timeline-size"]) expect(properties).toContain(`@property ${name}{syntax:"";inherits:false;initial-value:0}`); expect(properties).not.toMatch(/@property --(start|size)\b/); expect(styles).not.toContain("@property"); }); test("segment rows are marked for the table view: their palette slot, and where a track starts", async () => { const el = mount(); const rows = [...el.querySelectorAll("tbody > tr")]; const segments = [...el.querySelectorAll(".segment")]; // Every row's swatch matches its segment, including rows whose slot came from their label. expect(rows.map(r => r.dataset.fill)).toEqual(segments.map(s => s.dataset.series!)); expect(rows.map(r => r.hasAttribute("data-track-start"))).toEqual([true, false, false, true, false, false]); // A row that stops being a segment loses its marks; the marks never trigger a re-read. rows[3]!.children[2]!.textContent = ""; await sleep(0); expect(rows[3]!.hasAttribute("data-fill")).toBe(false); expect(rows[3]!.hasAttribute("data-track-start")).toBe(false); expect(rows[4]!.hasAttribute("data-track-start")).toBe(true); expect(el.querySelectorAll(".segment").length).toBe(5); el.remove(); await sleep(0); expect(rows.some(r => r.hasAttribute("data-fill") || r.hasAttribute("data-track-start"))).toBe(false); }); test("the keys step through segments and fill the tooltip; Escape lets go", () => { const el = mount(); const plot = el.querySelector(".plot")!; const tip = el.querySelector(".tooltip")!; key(plot, "ArrowRight"); key(plot, "ArrowRight"); expect(tip.hasAttribute("data-open")).toBe(true); expect(tip.querySelector(".heading")!.textContent).toBe("Recovery"); expect([...tip.querySelectorAll("li")].map(li => li.textContent)).toEqual(["10 Starts", "14 Days", "Fig. 2 Source"]); expect(el.querySelectorAll(".segment")[1]!.hasAttribute("data-active")).toBe(true); key(plot, "Escape"); expect(tip.hasAttribute("data-open")).toBe(false); }); test("Enter on the active segment, or a click, emits select", () => { const el = mount(); const seen: unknown[] = []; el.addEventListener("select", event => seen.push((event as CustomEvent).detail)); const plot = el.querySelector(".plot")!; key(plot, "End"); key(plot, "Enter"); el.querySelectorAll(".segment")[0]!.click(); expect(seen).toEqual([ { index: 5, track: "Early", label: "Flowering" }, { index: 0, track: "Standard", label: "Rooting" }, ]); }); test("keys pressed outside the plot are not the chart's", () => { const el = mount(); key(el.querySelector(".legend")!, "ArrowRight"); expect(el.querySelector(".tooltip")!.hasAttribute("data-open")).toBe(false); }); test("without a table the element leaves its children alone", () => { const el = mount("", "

No data

"); expect(el.innerHTML).toBe("

No data

"); expect(el.hasAttribute("data-enhanced")).toBe(false); });