|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + * |
| 5 | + * #4592: `<Errored>` recovering while another memo in its content is still |
| 6 | + * asking for the new input. `a` and `b` both ask by `s`; `b:0` rejects and |
| 7 | + * the error fallback shows. `s` changes and both re-ask. `b:1` landing while |
| 8 | + * `a:1` is out must not reveal `a`'s answer for `s = 0` beside `s = 1`: the |
| 9 | + * error stays the display until the frame that replaces it is whole (F-1, |
| 10 | + * D-39, D-41's mirror). |
| 11 | + */ |
| 12 | +import { render } from "@solidjs/web"; |
| 13 | +import { createMemo, createSignal, Errored, flush, Loading } from "solid-js"; |
| 14 | +import { expect, it } from "vitest"; |
| 15 | + |
| 16 | +const tick = async () => { |
| 17 | + for (let i = 0; i < 2; i++) { |
| 18 | + await new Promise(r => setTimeout(r, 0)); |
| 19 | + flush(); |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +async function run(boundary: "none" | "loading") { |
| 24 | + const flights = new Map<string, { resolve: (v: number) => void; reject: (e: Error) => void }>(); |
| 25 | + const ask = (key: string) => |
| 26 | + new Promise<number>((resolve, reject) => flights.set(key, { resolve, reject })); |
| 27 | + const [s, setS] = createSignal(0); |
| 28 | + const el = document.createElement("div"); |
| 29 | + const frames: string[] = []; |
| 30 | + const dispose = render(() => { |
| 31 | + const a = createMemo(() => ask("a:" + s())); |
| 32 | + const b = createMemo(() => ask("b:" + s())); |
| 33 | + const content = () => ( |
| 34 | + <> |
| 35 | + <p>s={s()}</p> |
| 36 | + <p>a={a()}</p> |
| 37 | + <p>b={b()}</p> |
| 38 | + </> |
| 39 | + ); |
| 40 | + return ( |
| 41 | + <Errored fallback={<p>error</p>}> |
| 42 | + {boundary === "loading" ? ( |
| 43 | + <Loading fallback={<p>loading</p>}>{content()}</Loading> |
| 44 | + ) : ( |
| 45 | + content() |
| 46 | + )} |
| 47 | + </Errored> |
| 48 | + ); |
| 49 | + }, el); |
| 50 | + await tick(); |
| 51 | + flights.get("a:0")!.resolve(10); |
| 52 | + await tick(); |
| 53 | + flights.get("b:0")!.reject(new Error("boom")); |
| 54 | + await tick(); |
| 55 | + frames.push(el.textContent!); |
| 56 | + setS(1); |
| 57 | + flush(); |
| 58 | + await tick(); |
| 59 | + frames.push(el.textContent!); // a:1 and b:1 asked |
| 60 | + flights.get("b:1")!.resolve(21); |
| 61 | + await tick(); |
| 62 | + frames.push(el.textContent!); // b recovers; a:1 is still out |
| 63 | + flights.get("a:1")!.resolve(11); |
| 64 | + await tick(); |
| 65 | + frames.push(el.textContent!); |
| 66 | + dispose(); |
| 67 | + return frames; |
| 68 | +} |
| 69 | + |
| 70 | +it("no boundary: <Errored> recovery does not show a pending memo's stale value beside its new input", async () => { |
| 71 | + expect(await run("none")).toEqual(["error", "error", "error", "s=1a=11b=21"]); |
| 72 | +}); |
| 73 | + |
| 74 | +it("inside <Loading>: <Errored> recovery does not show a pending memo's stale value beside its new input", async () => { |
| 75 | + expect(await run("loading")).toEqual(["error", "error", "error", "s=1a=11b=21"]); |
| 76 | +}); |
0 commit comments