// : the tree rendered from the textarea's value and the // page's kinds, adding through the menu, the editor (fields in and out, no // form owner, Enter / Escape), removal with its confirmation, the field of // record and the "change" event, the form's reset and mid-edit // serialization, readonly, and declining to upgrade. Real layout is the // smoke's job (examples/sequence.html). import { afterEach, expect, test } from "bun:test"; import "../packages/sequence/index"; import { readFields, writeFields } from "../packages/sequence/index"; const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)); const KINDS = ` `; const VALUE = JSON.stringify({ root: { id: "a", kind: "invite", fields: {}, next: { id: "c", kind: "connected", fields: { days: 7 }, branches: [{ id: "m", kind: "message", fields: { text: "Hi there", inmail: false }, next: null }, null], }, }, }); afterEach(async () => { document.body.textContent = ""; await sleep(0); }); const mount = (value = VALUE, attrs = "") => { const wrap = document.createElement("div"); wrap.innerHTML = `
${KINDS}
`; // textContent, not innerHTML: the JSON is the textarea's default value, verbatim wrap.querySelector("textarea")!.textContent = value; document.body.appendChild(wrap); const host = wrap.querySelector("pebble-sequence") as HTMLElement; return { host, form: wrap.querySelector("form")!, field: [...host.children].find(c => c instanceof HTMLTextAreaElement) as HTMLTextAreaElement }; }; const value = (field: HTMLTextAreaElement) => JSON.parse(field.value); const item = (host: Element, id: string) => host.querySelector(`[data-step="${id}"]`) as HTMLElement; const click = (el: Element | null) => (el as HTMLElement).click(); const key = (target: Element, k: string) => { const event = new KeyboardEvent("keydown", { key: k, bubbles: true, cancelable: true }); target.dispatchEvent(event); return event; }; test("renders the runs, cards and outcomes, and hides the textarea", () => { const { host, field } = mount(); expect(field.hidden).toBe(true); expect(host.querySelector(".mark.start")!.textContent).toBe("Start"); const top = host.querySelector(".sequence > .run")!; const topItems = [...top.children].filter(li => li.hasAttribute("data-step")); expect(topItems.map(li => li.getAttribute("data-step"))).toEqual(["a", "c"]); expect(item(host, "a").querySelector(".label")!.textContent).toBe("Send an invitation"); const outcomes = [...item(host, "c").querySelectorAll(".branches > .branch")]; expect(outcomes.map(b => b.querySelector(".outcome")!.textContent)).toEqual(["Yes", "No"]); expect(outcomes[0].querySelector('[data-step="m"] .summary')!.textContent).toBe("Hi there"); // a branching step ends its run: no "+"/End after it; each outcome has its own end expect([...top.children].some(li => li.classList.contains("end"))).toBe(false); expect(outcomes.map(b => b.querySelectorAll(".item.end").length)).toEqual([1, 1]); // a kind without fields has no Edit expect(item(host, "a").querySelector(".card .edit")).toBeNull(); expect(item(host, "c").querySelector(".card .edit")).not.toBeNull(); }); test("the + menu lists the page's kinds; a kind without fields is inserted at once", async () => { const { host, field } = mount(); const changes: unknown[] = []; host.addEventListener("change", e => changes.push((e as CustomEvent).detail)); const add = host.querySelector('button.add[data-slot="/next"]')!; click(add); expect(add.getAttribute("aria-expanded")).toBe("true"); const kinds = [...host.querySelectorAll(".menu .kind")].map(b => b.textContent); expect(kinds).toEqual(["Send an invitation", "Wait", "Send a message", "Accepted?"]); click(host.querySelector('.menu .kind[data-kind="invite"]')); expect(host.querySelector(".menu")).toBeNull(); const root = value(field).root; expect(root.kind).toBe("invite"); expect(root.next.id).toBe("a"); expect(changes).toHaveLength(1); await sleep(0); expect(document.activeElement?.closest("[data-step]")?.getAttribute("data-step")).toBe(root.id); }); test("a new step with fields opens its editor with the kind's defaults; Done commits", async () => { const { host, field } = mount(); click(host.querySelector('button.add[data-slot="a/next"]')); click(host.querySelector('.menu .kind[data-kind="wait"]')); const editor = host.querySelector(".editor")!; const days = editor.querySelector("input[name=days]") as HTMLInputElement; expect(days.value).toBe("3"); expect(host.hasAttribute("data-editing")).toBe(true); // the inserted step is in the value already, with the defaults const inserted = value(field).root.next; expect(inserted).toMatchObject({ kind: "wait", fields: { days: 3 } }); expect(inserted.next.id).toBe("c"); await sleep(0); expect(document.activeElement).toBe(days); days.value = "5"; click(editor.querySelector(".done")); expect(host.querySelector(".editor")).toBeNull(); expect(value(field).root.next.fields).toEqual({ days: 5 }); expect(item(host, inserted.id).querySelector(".summary")!.textContent).toBe("5 days"); }); test("canceling a new step's first edit takes it back out", () => { const { host, field } = mount(); const before = field.value; click(host.querySelector('button.add[data-slot="/next"]')); click(host.querySelector('.menu .kind[data-kind="wait"]')); click(host.querySelector(".editor .cancel")); expect(host.querySelector(".editor")).toBeNull(); expect(JSON.parse(field.value)).toEqual(JSON.parse(before)); }); test("Edit fills the controls from the step; Escape cancels; Enter in an input commits", async () => { const { host, field } = mount(); click(item(host, "m").querySelector(".card .edit")); const editor = item(host, "m").querySelector(".editor")!; const text = editor.querySelector("textarea[name=text]") as HTMLTextAreaElement; const inmail = editor.querySelector("input[name=inmail]") as HTMLInputElement; expect(text.value).toBe("Hi there"); expect(inmail.checked).toBe(false); // other controls wait while an editor is open expect((item(host, "a").querySelector(".card .remove") as HTMLButtonElement).disabled).toBe(true); text.value = "Changed"; key(text, "Escape"); expect(item(host, "m").querySelector(".editor")).toBeNull(); expect(value(field).root.next.branches[0].fields.text).toBe("Hi there"); await sleep(0); expect(document.activeElement).toBe(item(host, "m").querySelector(".card .edit")); click(item(host, "c").querySelector(".card .edit")); const days = item(host, "c").querySelector(".editor input[name=days]") as HTMLInputElement; days.value = "10"; const enter = key(days, "Enter"); expect(enter.defaultPrevented).toBe(true); expect(value(field).root.next.fields.days).toBe(10); }); test("Done waits for valid fields", () => { const { host, field } = mount(); click(item(host, "m").querySelector(".card .edit")); const text = item(host, "m").querySelector(".editor textarea[name=text]") as HTMLTextAreaElement; text.value = ""; click(item(host, "m").querySelector(".editor .done")); expect(item(host, "m").querySelector(".editor")).not.toBeNull(); expect(value(field).root.next.branches[0].fields.text).toBe("Hi there"); }); // happy-dom resolves a `form` attribute naming no form to the ancestor form // (the spec says no owner) and never fires "formdata": the posted keys and // the mid-edit serialization on a real engine are the smoke's // (examples/sequence.page.ts). Here: the attribute, and the handler driven // by a dispatched event. test("the editor's controls point at no form and their events stay inside", () => { const { host, form } = mount(); const outside: string[] = []; form.addEventListener("input", () => outside.push("input")); form.addEventListener("change", e => outside.push(e instanceof CustomEvent ? "element change" : "native change")); click(item(host, "m").querySelector(".card .edit")); const editor = item(host, "m").querySelector(".editor")!; const text = editor.querySelector("textarea[name=text]") as HTMLTextAreaElement; for (const control of editor.querySelectorAll("[name]")) expect(control.getAttribute("form")).toBe("pebble-sequence-no-form"); expect(document.getElementById("pebble-sequence-no-form")).toBeNull(); text.value = "typed"; text.dispatchEvent(new Event("input", { bubbles: true })); text.dispatchEvent(new Event("change", { bubbles: true })); expect(outside).toEqual([]); click(editor.querySelector(".done")); expect(outside).toEqual(["element change"]); }); test("serializing the form mid-edit includes the open, valid edit", () => { const { host, form } = mount(); const serialize = () => { const formData = new FormData(); formData.set("sequence", "committed"); form.dispatchEvent(Object.assign(new Event("formdata"), { formData })); return formData.get("sequence") as string; }; click(item(host, "m").querySelector(".card .edit")); const text = item(host, "m").querySelector(".editor textarea[name=text]") as HTMLTextAreaElement; text.value = "not yet committed"; expect(JSON.parse(serialize()).root.next.branches[0].fields.text).toBe("not yet committed"); text.value = ""; // invalid (required): the committed value is left as the form put it expect(serialize()).toBe("committed"); click(item(host, "m").querySelector(".editor .cancel")); expect(serialize()).toBe("committed"); // no editor open: nothing to add }); test("removing a linear step puts its next in its place", async () => { const { host, field } = mount(); click(item(host, "a").querySelector(".card .remove")); expect(value(field).root.id).toBe("c"); await sleep(0); expect(document.activeElement?.closest("[data-step]")?.getAttribute("data-step")).toBe("c"); }); test("removing what would discard other steps asks first; Escape withdraws", () => { const { host, field } = mount( JSON.stringify({ root: { id: "c", kind: "connected", fields: {}, branches: [{ id: "y", kind: "invite", fields: {}, next: null }, { id: "n", kind: "invite", fields: {}, next: null }] } }), ); const remove = () => item(host, "c").querySelector(".card .remove") as HTMLButtonElement; click(remove()); expect(remove().textContent).toBe("Remove with the steps it leads to"); expect(item(host, "c").querySelector(".card")!.hasAttribute("data-confirming")).toBe(true); key(remove(), "Escape"); expect(remove().textContent).toBe("Remove"); click(remove()); click(remove()); expect(value(field).root.id).toBe("y"); }); test("a form reset restores the server's value and the tree", async () => { const { host, form, field } = mount(); click(item(host, "a").querySelector(".card .remove")); expect(item(host, "a")).toBeNull(); form.reset(); await sleep(10); expect(value(field).root.id).toBe("a"); expect(item(host, "a")).not.toBeNull(); }); test("labels come from the page", () => { const { host } = mount(VALUE, 'label-start="Début" label-end="Fin" label-edit="Modifier" label-add="Ajouter une étape"'); expect(host.querySelector(".mark.start")!.textContent).toBe("Début"); expect(host.querySelector(".item.end .mark")!.textContent).toBe("Fin"); expect(item(host, "c").querySelector(".card .edit")!.textContent).toBe("Modifier"); expect(host.querySelector("button.add")!.getAttribute("aria-label")).toBe("Ajouter une étape"); }); test("readonly renders the tree without controls", () => { const { host } = mount(VALUE, "readonly"); expect(host.querySelectorAll("[data-step]").length).toBe(3); expect(host.querySelector("button")).toBeNull(); }); test("an empty value shows a + and the end", () => { const { host } = mount(""); expect(host.querySelectorAll("[data-step]").length).toBe(0); expect(host.querySelector('button.add[data-slot="/next"]')).not.toBeNull(); }); test("a step of an unknown kind can be removed, not edited", () => { const { host } = mount(JSON.stringify({ root: { id: "z", kind: "teleport", fields: { to: "Mars" }, next: null } })); const card = item(host, "z").querySelector(".card")!; expect(card.hasAttribute("data-unknown")).toBe(true); expect(card.querySelector(".label")!.textContent).toBe("teleport"); expect(card.querySelector(".edit")).toBeNull(); expect(card.querySelector(".remove")).not.toBeNull(); }); test("declines to upgrade on a malformed value, and without kinds", () => { const bad = mount("{not json"); expect(bad.field.hidden).toBe(false); expect(bad.host.querySelector(".sequence")).toBeNull(); const wrap = document.createElement("div"); wrap.innerHTML = ``; document.body.appendChild(wrap); expect(wrap.querySelector(".sequence")).toBeNull(); }); test("disconnecting puts the textarea back", async () => { const { host, field } = mount(); host.remove(); await sleep(10); expect(field.hidden).toBe(false); expect(host.querySelector(".sequence")).toBeNull(); }); test("fields cross as JSON types", () => { const box = document.createElement("div"); box.innerHTML = ` `; expect(readFields(box)).toEqual({ n: 4, empty: null, flag: true, pick: "b", none: null, s: "two", t: "text" }); writeFields(box, { n: 9, flag: false, pick: "a", s: "one", t: "new" }); expect(readFields(box)).toEqual({ n: 9, empty: null, flag: false, pick: "a", none: null, s: "one", t: "new" }); });