|
| 1 | +/** |
| 2 | + * #4271: a projection over a memo follows the memo after it has shown a |
| 3 | + * value — the memo going pending again reports pending through the |
| 4 | + * projection, and a later throw reaches the error boundary. |
| 5 | + */ |
| 6 | +import { expect, it } from "vitest"; |
| 7 | +import { |
| 8 | + createErrorBoundary, |
| 9 | + createMemo, |
| 10 | + createProjection, |
| 11 | + createRenderEffect, |
| 12 | + createRoot, |
| 13 | + createSignal, |
| 14 | + flush, |
| 15 | + isPending, |
| 16 | + untrack |
| 17 | +} from "../src/index.js"; |
| 18 | + |
| 19 | +it("#4271: a projection over a memo reports its later pending and errors", async () => { |
| 20 | + const [q, setQ] = createSignal("a"); |
| 21 | + let resolveB!: (v: { id: number; n: string }[]) => void; |
| 22 | + let screen = ""; |
| 23 | + const dispose = createRoot(d => { |
| 24 | + const src = createMemo(() => { |
| 25 | + const v = q(); |
| 26 | + if (v === "a") throw new Error("too short"); |
| 27 | + if (v === "b") return new Promise<{ id: number; n: string }[]>(r => (resolveB = r)); |
| 28 | + return [{ id: 1, n: v }]; |
| 29 | + }); |
| 30 | + const proj = createProjection(() => src(), [], { key: "id" }); |
| 31 | + const out = untrack(() => |
| 32 | + createErrorBoundary( |
| 33 | + () => `${proj.length} ${isPending(() => proj.length) ? "pending" : "-"}`, |
| 34 | + err => "error:" + (err() as Error).message |
| 35 | + ) |
| 36 | + ); |
| 37 | + createRenderEffect( |
| 38 | + () => out(), |
| 39 | + v => { |
| 40 | + screen = v; |
| 41 | + } |
| 42 | + ); |
| 43 | + return d; |
| 44 | + }); |
| 45 | + const tick = async () => { |
| 46 | + await new Promise(r => setTimeout(r, 0)); |
| 47 | + flush(); |
| 48 | + }; |
| 49 | + flush(); |
| 50 | + await tick(); |
| 51 | + expect(screen).toBe("error:too short"); |
| 52 | + setQ("c"); |
| 53 | + flush(); |
| 54 | + await tick(); |
| 55 | + expect(screen).toBe("1 -"); |
| 56 | + setQ("b"); |
| 57 | + flush(); |
| 58 | + await tick(); |
| 59 | + expect(screen).toBe("1 pending"); |
| 60 | + resolveB([ |
| 61 | + { id: 2, n: "b" }, |
| 62 | + { id: 3, n: "b" } |
| 63 | + ]); |
| 64 | + await tick(); |
| 65 | + expect(screen).toBe("2 -"); |
| 66 | + setQ("a"); |
| 67 | + flush(); |
| 68 | + await tick(); |
| 69 | + expect(screen).toBe("error:too short"); |
| 70 | + dispose(); |
| 71 | +}); |
0 commit comments