|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + */ |
| 5 | +import { test, expect } from "vitest"; |
| 6 | +import { For, Show, Errored, action, refresh, createSignal, createMemo, createOptimisticStore, flush } from "solid-js"; |
| 7 | +import { render } from "../src/index.js"; |
| 8 | +const tick = async () => { for (let i = 0; i < 4; i++) { await new Promise(r => setTimeout(r, 0)); flush(); } }; |
| 9 | + |
| 10 | +for (const V of ["base", "nofail"]) test(`${V}: a rename guess shows in the row as in the detail, after the row's list failed a re-ask`, async () => { |
| 11 | + const asks: { k: string; res: (v: any) => void; rej: (e: any) => void }[] = []; |
| 12 | + const ask = (k: string) => new Promise<any>((res, rej) => asks.push({ k, res, rej })); |
| 13 | + const take = (k: string) => { const i = asks.findIndex(a => a.k === k); if (i < 0) throw new Error(`no ask ${k} in ${asks.map(a => a.k)}`); return asks.splice(i, 1)[0]; }; |
| 14 | + const server: Record<string, any[]> = { a: [{ id: 1, t: "W" }, { id: 2, t: "F" }], b: [{ id: 4, t: "D" }], c: [{ id: 5, t: "K" }] }; |
| 15 | + const answer = (k: string) => take(k).res(structuredClone(server[k])); |
| 16 | + let api: any; |
| 17 | + function App() { |
| 18 | + const L: Record<string, any> = {}; |
| 19 | + for (const k of ["a", "b", "c"]) { |
| 20 | + const [ans] = createSignal(async () => await ask(k), { loadingValue: null } as any); |
| 21 | + const [rows, setRows] = createOptimisticStore(() => ans() ?? [], [] as any[], { key: "id" } as any); |
| 22 | + L[k] = { ans, rows, setRows }; |
| 23 | + } |
| 24 | + // the detail finds #5: which list holds it, then the row in it |
| 25 | + const where = createMemo(() => ["a", "b", "c"].find(k => L[k].rows.some((r: any) => r.id === 5)) ?? ""); |
| 26 | + const sel = createMemo(() => where() ? L[where()].rows.find((r: any) => r.id === 5) : null); |
| 27 | + const reorder = action(function* () { // swap a's two rows; accepted, every list is re-asked |
| 28 | + L.a.setRows((rs: any[]) => [rs[1], rs[0]]); |
| 29 | + yield ask("reorder"); |
| 30 | + refresh(L.a.ans); refresh(L.b.ans); refresh(L.c.ans); |
| 31 | + }); |
| 32 | + const move = action(function* () { // #5 from c to the end of b; accepted, every list is re-asked |
| 33 | + const row = { ...L.c.rows[0] }; |
| 34 | + L.c.setRows((rs: any[]) => rs.filter(r => r.id !== 5)); L.b.setRows((rs: any[]) => [...rs, row]); |
| 35 | + yield ask("move"); |
| 36 | + refresh(L.a.ans); refresh(L.b.ans); refresh(L.c.ans); |
| 37 | + }); |
| 38 | + const rename = action(function* (t: string) { L.b.setRows((rs: any[]) => { rs[rs.length - 1].t = t; }); yield ask("rename"); refresh(L.b.ans); }); |
| 39 | + api = { reorder, move, rename }; |
| 40 | + const col = (k: string) => <Errored fallback={<b>{k} error</b>}><Show when={L[k].ans() != null} fallback={<i>loading</i>}><For each={L[k].rows}>{(r: any) => <span>#{r.id} {r.t} </span>}</For></Show></Errored>; |
| 41 | + return <section>{col("a")}|{col("b")}|{col("c")}|<Show when={sel()} fallback={<i>none</i>}>{s => <>detail #{s().id} {s().t}</>}</Show></section>; |
| 42 | + } |
| 43 | + const c = document.body.appendChild(document.createElement("div")); |
| 44 | + render(() => <App />, c); |
| 45 | + const screen = () => c.textContent!.replace(/\s+/g, " ").trim(); |
| 46 | + await tick(); |
| 47 | + answer("a"); answer("b"); answer("c"); await tick(); |
| 48 | + api.reorder(); await tick(); |
| 49 | + api.move(); await tick(); // #5 into b, the reorder still out |
| 50 | + server.a = [server.a[1], server.a[0]]; take("reorder").res(true); await tick(); // accepted: a, b, c re-asked |
| 51 | + if (V === "nofail") answer("b"); else take("b").rej(new Error("down")); await tick(); // b's re-ask fails; a's and c's are out |
| 52 | + server.b = [...server.b, { id: 5, t: "K" }]; server.c = []; take("move").res(true); await tick(); // accepted: re-asked again |
| 53 | + api.rename("A"); await tick(); // rename #5, last in b |
| 54 | + const s = screen(); |
| 55 | + // base: the row shows "#5 K", the detail "#5 A". nofail (b's re-ask answered): both "A" |
| 56 | + expect(s.match(/#5 (\w) \|/)?.[1]).toBe(s.match(/detail #5 (\w)/)?.[1]); |
| 57 | +}); |
0 commit comments