|
| 1 | +/** |
| 2 | +// solidjs/solid#5104 |
| 3 | + * @jsxImportSource @solidjs/web |
| 4 | + * @vitest-environment jsdom |
| 5 | + */ |
| 6 | +import { test, expect } from "vitest"; |
| 7 | +import { For, Show, Errored, Loading, action, refresh, createSignal, createOptimisticStore, flush } from "solid-js"; |
| 8 | +import { render } from "../src/index.js"; |
| 9 | +const tick = async () => { for (let i = 0; i < 4; i++) { await new Promise(r => setTimeout(r, 0)); flush(); } }; |
| 10 | + |
| 11 | +// Two lists, each an optimistic store over its own answer. One action re-asks both; one answer lands; then a guessed |
| 12 | +// move takes a row from that list to the other, still re-asking. The other list's heading, which reads its answer |
| 13 | +// (`ans() != null`) and its store's length, misses the guess its own <For> shows. |
| 14 | +for (const W of ["one re-ask has landed", "neither re-ask has landed"]) test(`a heading counts the guess its list shows (${W})`, async () => { |
| 15 | + const asks: { k: string; res: (v: any) => void }[] = []; |
| 16 | + const ask = (k: string) => new Promise<any>(res => asks.push({ k, res })); |
| 17 | + const answer = (k: string, v: any) => { const i = asks.findIndex(a => a.k === k); asks.splice(i, 1)[0].res(v); }; |
| 18 | + let api: any; |
| 19 | + function App() { |
| 20 | + const [doingAns] = createSignal(async () => await ask("doing"), { loadingValue: null } as any); |
| 21 | + const [doing, setDoing] = createOptimisticStore(() => doingAns() ?? [], [] as any[], { key: "id" } as any); |
| 22 | + const [doneAns] = createSignal(async () => await ask("done"), { loadingValue: null } as any); |
| 23 | + const [done, setDone] = createOptimisticStore(() => doneAns() ?? [], [] as any[], { key: "id" } as any); |
| 24 | + const reask = action(function* () { refresh(doneAns); refresh(doingAns); }); // an accepted save elsewhere re-asks both |
| 25 | + const move = action(function* () { // #5 from done to doing, guessed |
| 26 | + setDone((rs: any[]) => rs.filter(r => r.id !== 5)); setDoing((rs: any[]) => [...rs, { id: 5 }]); |
| 27 | + }); |
| 28 | + api = { reask, move }; |
| 29 | + return <section> |
| 30 | + <h3>{doingAns() != null ? `Doing (${doing.length})` : "Doing"}</h3> |
| 31 | + <Loading fallback={<i>loading</i>}><Errored fallback={<b>error</b>}> |
| 32 | + <Show when={doingAns() != null}><For each={doing}>{(r: any) => <span>#{r.id} </span>}</For></Show> |
| 33 | + </Errored></Loading> |
| 34 | + | done: <For each={done}>{(r: any) => <span>#{r.id} </span>}</For> |
| 35 | + </section>; |
| 36 | + } |
| 37 | + const c = document.body.appendChild(document.createElement("div")); |
| 38 | + render(() => <App />, c); |
| 39 | + await tick(); |
| 40 | + answer("doing", [{ id: 3 }, { id: 4 }]); answer("done", [{ id: 5 }]); await tick(); |
| 41 | + api.reask(); await tick(); |
| 42 | + if (W === "one re-ask has landed") { answer("done", [{ id: 5 }]); await tick(); } |
| 43 | + api.move(); await tick(); |
| 44 | + const s = c.textContent!.replace(/\s+/g, " ").trim(); |
| 45 | + const counted = Number(s.match(/Doing \((\d+)\)/)?.[1]); const shown = (s.split("|")[0].match(/#\d+ /g) || []).length; |
| 46 | + // one re-ask landed (main 99db9bce4): "Doing (2)#3 #4 #5 | done:" — the list shows the guess, its heading doesn't |
| 47 | + expect({ counted, shown, s }).toEqual({ counted: shown, shown, s }); |
| 48 | +}); |
0 commit comments