|
| 1 | +/** |
| 2 | + * #3892 — `<Loading on={latest(id)}>` stays on its fallback after the async |
| 3 | + * source it shares with another boundary has settled. |
| 4 | + * |
| 5 | + * Compiled `{data()}` is an insert: the read lives in a child render effect, |
| 6 | + * and the boundary's value is that child (the same value once the fallback |
| 7 | + * ends). A parent render effect inserts each boundary's result. `on: |
| 8 | + * () => latest(id)` shows the fallback now, beside the frame the other |
| 9 | + * boundary holds. When the shared source settles, that parent must apply the |
| 10 | + * child again — a fresh read of the boundary is not enough. |
| 11 | + */ |
| 12 | +import { afterEach, expect, test } from "vitest"; |
| 13 | +import { |
| 14 | + createLoadingBoundary, |
| 15 | + createMemo, |
| 16 | + createRenderEffect, |
| 17 | + createRoot, |
| 18 | + createSignal, |
| 19 | + flush, |
| 20 | + latest |
| 21 | +} from "../src/index.js"; |
| 22 | + |
| 23 | +afterEach(() => flush()); |
| 24 | + |
| 25 | +async function drain() { |
| 26 | + for (let i = 0; i < 6; i++) { |
| 27 | + await Promise.resolve(); |
| 28 | + flush(); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +test("Loading on latest leaves its fallback once the shared source settles", async () => { |
| 33 | + let finish!: (value: number) => void; |
| 34 | + const replacement = new Promise<number>(resolve => { |
| 35 | + finish = resolve; |
| 36 | + }); |
| 37 | + const [id, setId] = createSignal(0); |
| 38 | + const texts: string[] = []; |
| 39 | + let appliedA: unknown; |
| 40 | + let appliedB: unknown; |
| 41 | + let dispose!: () => void; |
| 42 | + |
| 43 | + createRoot(d => { |
| 44 | + dispose = d; |
| 45 | + const data = createMemo(() => (id() ? replacement : Promise.resolve(1))); |
| 46 | + const slot = (fallback: string, on?: boolean) => { |
| 47 | + const i = texts.push("") - 1; |
| 48 | + return createLoadingBoundary( |
| 49 | + () => { |
| 50 | + createRenderEffect( |
| 51 | + () => data(), |
| 52 | + v => { |
| 53 | + texts[i] = String(v); |
| 54 | + } |
| 55 | + ); |
| 56 | + return i; |
| 57 | + }, |
| 58 | + () => fallback, |
| 59 | + on ? { on: () => latest(id) } : undefined |
| 60 | + ); |
| 61 | + }; |
| 62 | + const armed = slot("fallback", true); |
| 63 | + const plain = slot("kept"); |
| 64 | + createRenderEffect(armed, v => { |
| 65 | + appliedA = v; |
| 66 | + }); |
| 67 | + createRenderEffect(plain, v => { |
| 68 | + appliedB = v; |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + const screen = () => |
| 73 | + (typeof appliedA === "number" ? texts[appliedA] : appliedA) + |
| 74 | + (typeof appliedB === "number" ? texts[appliedB] : String(appliedB)); |
| 75 | + |
| 76 | + flush(); |
| 77 | + await drain(); |
| 78 | + expect(screen()).toBe("11"); |
| 79 | + |
| 80 | + setId(1); |
| 81 | + flush(); |
| 82 | + await drain(); |
| 83 | + // Display-ahead fallback beside the other boundary's previous content. |
| 84 | + expect(screen()).toBe("fallback1"); |
| 85 | + |
| 86 | + finish(2); |
| 87 | + await drain(); |
| 88 | + expect(screen()).toBe("22"); |
| 89 | + dispose(); |
| 90 | +}); |
0 commit comments