| @@ -0,0 +1,301 @@ |
| 1 | +// <pebble-sequence>: the tree rendered from the textarea's value and the |
| 2 | +// page's kinds, adding through the menu, the editor (fields in and out, no |
| 3 | +// form owner, Enter / Escape), removal with its confirmation, the field of |
| 4 | +// record and the "change" event, the form's reset and mid-edit |
| 5 | +// serialization, readonly, and declining to upgrade. Real layout is the |
| 6 | +// smoke's job (examples/sequence.html). |
| 7 | +import { afterEach, expect, test } from "bun:test"; |
| 8 | +import "../packages/sequence/index"; |
| 9 | +import { readFields, writeFields } from "../packages/sequence/index"; |
| 10 | + |
| 11 | +const sleep = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms)); |
| 12 | + |
| 13 | +const KINDS = ` |
| 14 | + <template data-kind="invite" data-label="Send an invitation"></template> |
| 15 | + <template data-kind="wait" data-label="Wait" data-summary="{days} days"> |
| 16 | + <label>Days <input name="days" type="number" min="1" value="3" required></label> |
| 17 | + </template> |
| 18 | + <template data-kind="message" data-label="Send a message"> |
| 19 | + <label>Message <textarea name="text" required></textarea></label> |
| 20 | + <label><input type="checkbox" name="inmail"> InMail</label> |
| 21 | + </template> |
| 22 | + <template data-kind="connected" data-label="Accepted?" data-branches="Yes|No"> |
| 23 | + <label>Give up after <input name="days" type="number" value="7"></label> |
| 24 | + </template>`; |
| 25 | + |
| 26 | +const VALUE = JSON.stringify({ |
| 27 | + root: { |
| 28 | + id: "a", |
| 29 | + kind: "invite", |
| 30 | + fields: {}, |
| 31 | + next: { |
| 32 | + id: "c", |
| 33 | + kind: "connected", |
| 34 | + fields: { days: 7 }, |
| 35 | + branches: [{ id: "m", kind: "message", fields: { text: "Hi there", inmail: false }, next: null }, null], |
| 36 | + }, |
| 37 | + }, |
| 38 | +}); |
| 39 | + |
| 40 | +afterEach(async () => { |
| 41 | + document.body.textContent = ""; |
| 42 | + await sleep(0); |
| 43 | +}); |
| 44 | + |
| 45 | +const mount = (value = VALUE, attrs = "") => { |
| 46 | + const wrap = document.createElement("div"); |
| 47 | + wrap.innerHTML = `<form><pebble-sequence ${attrs}>${KINDS}<textarea name="sequence"></textarea></pebble-sequence><input name="title" value="Q1"></form>`; |
| 48 | + // textContent, not innerHTML: the JSON is the textarea's default value, verbatim |
| 49 | + wrap.querySelector("textarea")!.textContent = value; |
| 50 | + document.body.appendChild(wrap); |
| 51 | + const host = wrap.querySelector("pebble-sequence") as HTMLElement; |
| 52 | + return { host, form: wrap.querySelector("form")!, field: [...host.children].find(c => c instanceof HTMLTextAreaElement) as HTMLTextAreaElement }; |
| 53 | +}; |
| 54 | + |
| 55 | +const value = (field: HTMLTextAreaElement) => JSON.parse(field.value); |
| 56 | +const item = (host: Element, id: string) => host.querySelector(`[data-step="${id}"]`) as HTMLElement; |
| 57 | +const click = (el: Element | null) => (el as HTMLElement).click(); |
| 58 | +const key = (target: Element, k: string) => { |
| 59 | + const event = new KeyboardEvent("keydown", { key: k, bubbles: true, cancelable: true }); |
| 60 | + target.dispatchEvent(event); |
| 61 | + return event; |
| 62 | +}; |
| 63 | + |
| 64 | +test("renders the runs, cards and outcomes, and hides the textarea", () => { |
| 65 | + const { host, field } = mount(); |
| 66 | + expect(field.hidden).toBe(true); |
| 67 | + expect(host.querySelector(".mark.start")!.textContent).toBe("Start"); |
| 68 | + const top = host.querySelector(".sequence > .run")!; |
| 69 | + const topItems = [...top.children].filter(li => li.hasAttribute("data-step")); |
| 70 | + expect(topItems.map(li => li.getAttribute("data-step"))).toEqual(["a", "c"]); |
| 71 | + expect(item(host, "a").querySelector(".label")!.textContent).toBe("Send an invitation"); |
| 72 | + const outcomes = [...item(host, "c").querySelectorAll(".branches > .branch")]; |
| 73 | + expect(outcomes.map(b => b.querySelector(".outcome")!.textContent)).toEqual(["Yes", "No"]); |
| 74 | + expect(outcomes[0].querySelector('[data-step="m"] .summary')!.textContent).toBe("Hi there"); |
| 75 | + // a branching step ends its run: no "+"/End after it; each outcome has its own end |
| 76 | + expect([...top.children].some(li => li.classList.contains("end"))).toBe(false); |
| 77 | + expect(outcomes.map(b => b.querySelectorAll(".item.end").length)).toEqual([1, 1]); |
| 78 | + // a kind without fields has no Edit |
| 79 | + expect(item(host, "a").querySelector(".card .edit")).toBeNull(); |
| 80 | + expect(item(host, "c").querySelector(".card .edit")).not.toBeNull(); |
| 81 | +}); |
| 82 | + |
| 83 | +test("the + menu lists the page's kinds; a kind without fields is inserted at once", async () => { |
| 84 | + const { host, field } = mount(); |
| 85 | + const changes: unknown[] = []; |
| 86 | + host.addEventListener("change", e => changes.push((e as CustomEvent).detail)); |
| 87 | + const add = host.querySelector('button.add[data-slot="/next"]')!; |
| 88 | + click(add); |
| 89 | + expect(add.getAttribute("aria-expanded")).toBe("true"); |
| 90 | + const kinds = [...host.querySelectorAll(".menu .kind")].map(b => b.textContent); |
| 91 | + expect(kinds).toEqual(["Send an invitation", "Wait", "Send a message", "Accepted?"]); |
| 92 | + click(host.querySelector('.menu .kind[data-kind="invite"]')); |
| 93 | + expect(host.querySelector(".menu")).toBeNull(); |
| 94 | + const root = value(field).root; |
| 95 | + expect(root.kind).toBe("invite"); |
| 96 | + expect(root.next.id).toBe("a"); |
| 97 | + expect(changes).toHaveLength(1); |
| 98 | + await sleep(0); |
| 99 | + expect(document.activeElement?.closest("[data-step]")?.getAttribute("data-step")).toBe(root.id); |
| 100 | +}); |
| 101 | + |
| 102 | +test("a new step with fields opens its editor with the kind's defaults; Done commits", async () => { |
| 103 | + const { host, field } = mount(); |
| 104 | + click(host.querySelector('button.add[data-slot="a/next"]')); |
| 105 | + click(host.querySelector('.menu .kind[data-kind="wait"]')); |
| 106 | + const editor = host.querySelector(".editor")!; |
| 107 | + const days = editor.querySelector("input[name=days]") as HTMLInputElement; |
| 108 | + expect(days.value).toBe("3"); |
| 109 | + expect(host.hasAttribute("data-editing")).toBe(true); |
| 110 | + // the inserted step is in the value already, with the defaults |
| 111 | + const inserted = value(field).root.next; |
| 112 | + expect(inserted).toMatchObject({ kind: "wait", fields: { days: 3 } }); |
| 113 | + expect(inserted.next.id).toBe("c"); |
| 114 | + await sleep(0); |
| 115 | + expect(document.activeElement).toBe(days); |
| 116 | + days.value = "5"; |
| 117 | + click(editor.querySelector(".done")); |
| 118 | + expect(host.querySelector(".editor")).toBeNull(); |
| 119 | + expect(value(field).root.next.fields).toEqual({ days: 5 }); |
| 120 | + expect(item(host, inserted.id).querySelector(".summary")!.textContent).toBe("5 days"); |
| 121 | +}); |
| 122 | + |
| 123 | +test("canceling a new step's first edit takes it back out", () => { |
| 124 | + const { host, field } = mount(); |
| 125 | + const before = field.value; |
| 126 | + click(host.querySelector('button.add[data-slot="/next"]')); |
| 127 | + click(host.querySelector('.menu .kind[data-kind="wait"]')); |
| 128 | + click(host.querySelector(".editor .cancel")); |
| 129 | + expect(host.querySelector(".editor")).toBeNull(); |
| 130 | + expect(JSON.parse(field.value)).toEqual(JSON.parse(before)); |
| 131 | +}); |
| 132 | + |
| 133 | +test("Edit fills the controls from the step; Escape cancels; Enter in an input commits", async () => { |
| 134 | + const { host, field } = mount(); |
| 135 | + click(item(host, "m").querySelector(".card .edit")); |
| 136 | + const editor = item(host, "m").querySelector(".editor")!; |
| 137 | + const text = editor.querySelector("textarea[name=text]") as HTMLTextAreaElement; |
| 138 | + const inmail = editor.querySelector("input[name=inmail]") as HTMLInputElement; |
| 139 | + expect(text.value).toBe("Hi there"); |
| 140 | + expect(inmail.checked).toBe(false); |
| 141 | + // other controls wait while an editor is open |
| 142 | + expect((item(host, "a").querySelector(".card .remove") as HTMLButtonElement).disabled).toBe(true); |
| 143 | + text.value = "Changed"; |
| 144 | + key(text, "Escape"); |
| 145 | + expect(item(host, "m").querySelector(".editor")).toBeNull(); |
| 146 | + expect(value(field).root.next.branches[0].fields.text).toBe("Hi there"); |
| 147 | + await sleep(0); |
| 148 | + expect(document.activeElement).toBe(item(host, "m").querySelector(".card .edit")); |
| 149 | + |
| 150 | + click(item(host, "c").querySelector(".card .edit")); |
| 151 | + const days = item(host, "c").querySelector(".editor input[name=days]") as HTMLInputElement; |
| 152 | + days.value = "10"; |
| 153 | + const enter = key(days, "Enter"); |
| 154 | + expect(enter.defaultPrevented).toBe(true); |
| 155 | + expect(value(field).root.next.fields.days).toBe(10); |
| 156 | +}); |
| 157 | + |
| 158 | +test("Done waits for valid fields", () => { |
| 159 | + const { host, field } = mount(); |
| 160 | + click(item(host, "m").querySelector(".card .edit")); |
| 161 | + const text = item(host, "m").querySelector(".editor textarea[name=text]") as HTMLTextAreaElement; |
| 162 | + text.value = ""; |
| 163 | + click(item(host, "m").querySelector(".editor .done")); |
| 164 | + expect(item(host, "m").querySelector(".editor")).not.toBeNull(); |
| 165 | + expect(value(field).root.next.branches[0].fields.text).toBe("Hi there"); |
| 166 | +}); |
| 167 | + |
| 168 | +// happy-dom resolves a `form` attribute naming no form to the ancestor form |
| 169 | +// (the spec says no owner) and never fires "formdata": the posted keys and |
| 170 | +// the mid-edit serialization on a real engine are the smoke's |
| 171 | +// (examples/sequence.page.ts). Here: the attribute, and the handler driven |
| 172 | +// by a dispatched event. |
| 173 | +test("the editor's controls point at no form and their events stay inside", () => { |
| 174 | + const { host, form } = mount(); |
| 175 | + const outside: string[] = []; |
| 176 | + form.addEventListener("input", () => outside.push("input")); |
| 177 | + form.addEventListener("change", e => outside.push(e instanceof CustomEvent ? "element change" : "native change")); |
| 178 | + click(item(host, "m").querySelector(".card .edit")); |
| 179 | + const editor = item(host, "m").querySelector(".editor")!; |
| 180 | + const text = editor.querySelector("textarea[name=text]") as HTMLTextAreaElement; |
| 181 | + for (const control of editor.querySelectorAll("[name]")) expect(control.getAttribute("form")).toBe("pebble-sequence-no-form"); |
| 182 | + expect(document.getElementById("pebble-sequence-no-form")).toBeNull(); |
| 183 | + text.value = "typed"; |
| 184 | + text.dispatchEvent(new Event("input", { bubbles: true })); |
| 185 | + text.dispatchEvent(new Event("change", { bubbles: true })); |
| 186 | + expect(outside).toEqual([]); |
| 187 | + click(editor.querySelector(".done")); |
| 188 | + expect(outside).toEqual(["element change"]); |
| 189 | +}); |
| 190 | + |
| 191 | +test("serializing the form mid-edit includes the open, valid edit", () => { |
| 192 | + const { host, form } = mount(); |
| 193 | + const serialize = () => { |
| 194 | + const formData = new FormData(); |
| 195 | + formData.set("sequence", "committed"); |
| 196 | + form.dispatchEvent(Object.assign(new Event("formdata"), { formData })); |
| 197 | + return formData.get("sequence") as string; |
| 198 | + }; |
| 199 | + click(item(host, "m").querySelector(".card .edit")); |
| 200 | + const text = item(host, "m").querySelector(".editor textarea[name=text]") as HTMLTextAreaElement; |
| 201 | + text.value = "not yet committed"; |
| 202 | + expect(JSON.parse(serialize()).root.next.branches[0].fields.text).toBe("not yet committed"); |
| 203 | + text.value = ""; // invalid (required): the committed value is left as the form put it |
| 204 | + expect(serialize()).toBe("committed"); |
| 205 | + click(item(host, "m").querySelector(".editor .cancel")); |
| 206 | + expect(serialize()).toBe("committed"); // no editor open: nothing to add |
| 207 | +}); |
| 208 | + |
| 209 | +test("removing a linear step puts its next in its place", async () => { |
| 210 | + const { host, field } = mount(); |
| 211 | + click(item(host, "a").querySelector(".card .remove")); |
| 212 | + expect(value(field).root.id).toBe("c"); |
| 213 | + await sleep(0); |
| 214 | + expect(document.activeElement?.closest("[data-step]")?.getAttribute("data-step")).toBe("c"); |
| 215 | +}); |
| 216 | + |
| 217 | +test("removing what would discard other steps asks first; Escape withdraws", () => { |
| 218 | + const { host, field } = mount( |
| 219 | + JSON.stringify({ root: { id: "c", kind: "connected", fields: {}, branches: [{ id: "y", kind: "invite", fields: {}, next: null }, { id: "n", kind: "invite", fields: {}, next: null }] } }), |
| 220 | + ); |
| 221 | + const remove = () => item(host, "c").querySelector(".card .remove") as HTMLButtonElement; |
| 222 | + click(remove()); |
| 223 | + expect(remove().textContent).toBe("Remove with the steps it leads to"); |
| 224 | + expect(item(host, "c").querySelector(".card")!.hasAttribute("data-confirming")).toBe(true); |
| 225 | + key(remove(), "Escape"); |
| 226 | + expect(remove().textContent).toBe("Remove"); |
| 227 | + click(remove()); |
| 228 | + click(remove()); |
| 229 | + expect(value(field).root.id).toBe("y"); |
| 230 | +}); |
| 231 | + |
| 232 | +test("a form reset restores the server's value and the tree", async () => { |
| 233 | + const { host, form, field } = mount(); |
| 234 | + click(item(host, "a").querySelector(".card .remove")); |
| 235 | + expect(item(host, "a")).toBeNull(); |
| 236 | + form.reset(); |
| 237 | + await sleep(10); |
| 238 | + expect(value(field).root.id).toBe("a"); |
| 239 | + expect(item(host, "a")).not.toBeNull(); |
| 240 | +}); |
| 241 | + |
| 242 | +test("labels come from the page", () => { |
| 243 | + const { host } = mount(VALUE, 'label-start="Début" label-end="Fin" label-edit="Modifier" label-add="Ajouter une étape"'); |
| 244 | + expect(host.querySelector(".mark.start")!.textContent).toBe("Début"); |
| 245 | + expect(host.querySelector(".item.end .mark")!.textContent).toBe("Fin"); |
| 246 | + expect(item(host, "c").querySelector(".card .edit")!.textContent).toBe("Modifier"); |
| 247 | + expect(host.querySelector("button.add")!.getAttribute("aria-label")).toBe("Ajouter une étape"); |
| 248 | +}); |
| 249 | + |
| 250 | +test("readonly renders the tree without controls", () => { |
| 251 | + const { host } = mount(VALUE, "readonly"); |
| 252 | + expect(host.querySelectorAll("[data-step]").length).toBe(3); |
| 253 | + expect(host.querySelector("button")).toBeNull(); |
| 254 | +}); |
| 255 | + |
| 256 | +test("an empty value shows a + and the end", () => { |
| 257 | + const { host } = mount(""); |
| 258 | + expect(host.querySelectorAll("[data-step]").length).toBe(0); |
| 259 | + expect(host.querySelector('button.add[data-slot="/next"]')).not.toBeNull(); |
| 260 | +}); |
| 261 | + |
| 262 | +test("a step of an unknown kind can be removed, not edited", () => { |
| 263 | + const { host } = mount(JSON.stringify({ root: { id: "z", kind: "teleport", fields: { to: "Mars" }, next: null } })); |
| 264 | + const card = item(host, "z").querySelector(".card")!; |
| 265 | + expect(card.hasAttribute("data-unknown")).toBe(true); |
| 266 | + expect(card.querySelector(".label")!.textContent).toBe("teleport"); |
| 267 | + expect(card.querySelector(".edit")).toBeNull(); |
| 268 | + expect(card.querySelector(".remove")).not.toBeNull(); |
| 269 | +}); |
| 270 | + |
| 271 | +test("declines to upgrade on a malformed value, and without kinds", () => { |
| 272 | + const bad = mount("{not json"); |
| 273 | + expect(bad.field.hidden).toBe(false); |
| 274 | + expect(bad.host.querySelector(".sequence")).toBeNull(); |
| 275 | + const wrap = document.createElement("div"); |
| 276 | + wrap.innerHTML = `<pebble-sequence><textarea name="s">{"root":null}</textarea></pebble-sequence>`; |
| 277 | + document.body.appendChild(wrap); |
| 278 | + expect(wrap.querySelector(".sequence")).toBeNull(); |
| 279 | +}); |
| 280 | + |
| 281 | +test("disconnecting puts the textarea back", async () => { |
| 282 | + const { host, field } = mount(); |
| 283 | + host.remove(); |
| 284 | + await sleep(10); |
| 285 | + expect(field.hidden).toBe(false); |
| 286 | + expect(host.querySelector(".sequence")).toBeNull(); |
| 287 | +}); |
| 288 | + |
| 289 | +test("fields cross as JSON types", () => { |
| 290 | + const box = document.createElement("div"); |
| 291 | + box.innerHTML = ` |
| 292 | + <input name="n" type="number" value="4"><input name="empty" type="number" value=""> |
| 293 | + <input name="flag" type="checkbox" checked> |
| 294 | + <input name="pick" type="radio" value="a"><input name="pick" type="radio" value="b" checked> |
| 295 | + <input name="none" type="radio" value="x"> |
| 296 | + <select name="s"><option>one</option><option selected>two</option></select> |
| 297 | + <textarea name="t">text</textarea><input type="submit" name="ignored">`; |
| 298 | + expect(readFields(box)).toEqual({ n: 4, empty: null, flag: true, pick: "b", none: null, s: "two", t: "text" }); |
| 299 | + writeFields(box, { n: 9, flag: false, pick: "a", s: "one", t: "new" }); |
| 300 | + expect(readFields(box)).toEqual({ n: 9, empty: null, flag: false, pick: "a", none: null, s: "one", t: "new" }); |
| 301 | +}); |