|
| 1 | +/** |
| 2 | + * #4639 follow-up to #4638 — an error boundary built in the component body |
| 3 | + * (outside `<Loading on={sel()}>`) passes its source's pending by (A6). Its |
| 4 | + * output is on screen only through the render effects it reaches (Vocabulary |
| 5 | + * "Reader": a memo counts only through the render effects that read it, |
| 6 | + * wherever it was created, and a pending value no render effect reaches holds |
| 7 | + * nothing; F-3: "where the memo was created does not matter"). Reached only |
| 8 | + * by content behind the re-armed fallback, it holds nothing outside it (F-4, |
| 9 | + * D-14): the fallback shows with the change's frame, beside the sibling's. |
| 10 | + * |
| 11 | + * - memo: a memo built outside the Loading between the output and the content; |
| 12 | + * - nested: an error boundary over an error boundary, both built outside; |
| 13 | + * - unread: an error boundary nobody reads, beside the content's plain read; |
| 14 | + * - header: the output also rendered outside — a reader that is the display |
| 15 | + * (D-16): the frame waits for `detail`, and DEV reports |
| 16 | + * LOADING_ON_OUTSIDE_HOLD. Only then: with the fallback on screen the |
| 17 | + * report would be false (D-16, "the fallback can never be seen"). |
| 18 | + * - ueff: the output also read by a user effect outside — never a reader |
| 19 | + * (Vocabulary "Reader"), so it holds nothing, as a plain read of `detail`; |
| 20 | + * - lattice: a diamond memo lattice 2 wide × 30 deep below the output, read |
| 21 | + * by nothing on screen — each node is walked once, not once per path; |
| 22 | + * - header-tree: a header render effect inside an error boundary built |
| 23 | + * outside, the boundary's output mounted untracked — the header is the |
| 24 | + * display (D-16): the frame waits and DEV reports, as `header`. |
| 25 | + */ |
| 26 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 27 | +import { |
| 28 | + createEffect, |
| 29 | + createErrorBoundary, |
| 30 | + createLoadingBoundary, |
| 31 | + createMemo, |
| 32 | + createRenderEffect, |
| 33 | + createRoot, |
| 34 | + createSignal, |
| 35 | + flush, |
| 36 | + untrack, |
| 37 | + OBSERVE |
| 38 | +} from "../src/index.js"; |
| 39 | + |
| 40 | +beforeEach(() => vi.useFakeTimers()); |
| 41 | +afterEach(() => { |
| 42 | + vi.useRealTimers(); |
| 43 | + vi.restoreAllMocks(); |
| 44 | +}); |
| 45 | + |
| 46 | +type Mode = "direct" | "memo" | "nested" | "unread" | "ueff" | "lattice" | "header" | "header-tree"; |
| 47 | + |
| 48 | +function part(read: () => string) { |
| 49 | + const el = { text: "-" }; |
| 50 | + createRenderEffect(read, v => { |
| 51 | + el.text = v; |
| 52 | + }); |
| 53 | + return el; |
| 54 | +} |
| 55 | + |
| 56 | +function page(mode: Mode) { |
| 57 | + const asks: { key: string; res: (v: any) => void; rej: (e: any) => void }[] = []; |
| 58 | + const ask = (key: string) => new Promise<any>((res, rej) => asks.push({ key, res, rej })); |
| 59 | + const view: { |
| 60 | + header: string; |
| 61 | + headerEl?: { text: string }; |
| 62 | + detail: { text: string } | null; |
| 63 | + comments: { text: string } | null; |
| 64 | + } = { header: "", detail: null, comments: null }; |
| 65 | + const [sel, setSel] = createSignal(3); |
| 66 | + let dispose!: () => void; |
| 67 | + createRoot(d => { |
| 68 | + dispose = d; |
| 69 | + const detail = createMemo(() => ask(`detail:${sel()}`), { name: "detail" }); |
| 70 | + const eb = (src: () => any): (() => any) => |
| 71 | + untrack(() => |
| 72 | + createErrorBoundary( |
| 73 | + () => src(), |
| 74 | + () => ({ title: "ERR" }) |
| 75 | + ) |
| 76 | + ); |
| 77 | + let shown: () => any = mode === "unread" ? detail : eb(detail); |
| 78 | + if (mode === "unread") eb(detail); |
| 79 | + if (mode === "nested") shown = eb(shown); |
| 80 | + let title = () => shown()?.title as string; |
| 81 | + if (mode === "memo") title = createMemo(title, { name: "title" }); |
| 82 | + if (mode === "ueff") |
| 83 | + createEffect( |
| 84 | + () => shown()?.title, |
| 85 | + () => {} |
| 86 | + ); |
| 87 | + if (mode === "lattice") { |
| 88 | + let layer = [shown, shown]; |
| 89 | + for (let i = 0; i < 30; i++) { |
| 90 | + const [l, r] = layer; |
| 91 | + layer = [createMemo(() => [l(), r()]), createMemo(() => [r(), l()])]; |
| 92 | + } |
| 93 | + } |
| 94 | + if (mode === "header-tree") { |
| 95 | + const out = untrack(() => |
| 96 | + createErrorBoundary( |
| 97 | + () => part(() => detail().title), |
| 98 | + () => ({ text: "ERR" }) |
| 99 | + ) |
| 100 | + ); |
| 101 | + const el = untrack(out) as { text: string }; |
| 102 | + view.headerEl = el; |
| 103 | + } |
| 104 | + if (mode === "header") |
| 105 | + createRenderEffect( |
| 106 | + () => shown()?.title, |
| 107 | + v => { |
| 108 | + view.header = `${v} / `; |
| 109 | + } |
| 110 | + ); |
| 111 | + const comments = createMemo(() => ask(`comments:${sel()}`), { name: "comments" }); |
| 112 | + const a = untrack(() => |
| 113 | + createLoadingBoundary( |
| 114 | + () => part(title), |
| 115 | + () => part(() => `loading #${sel()}`), |
| 116 | + { on: () => sel() } |
| 117 | + ) |
| 118 | + ); |
| 119 | + createRenderEffect(a, v => { |
| 120 | + view.detail = v; |
| 121 | + }); |
| 122 | + const b = untrack(() => |
| 123 | + createLoadingBoundary( |
| 124 | + () => part(() => `comments ${(comments() as any[]).length}`), |
| 125 | + () => part(() => "loading comments"), |
| 126 | + { on: () => sel() } |
| 127 | + ) |
| 128 | + ); |
| 129 | + createRenderEffect(b, v => { |
| 130 | + view.comments = v; |
| 131 | + }); |
| 132 | + }); |
| 133 | + const settle = async (key: string, v?: any, reject?: boolean) => { |
| 134 | + const i = asks.findIndex(a => a.key === key); |
| 135 | + if (i >= 0) { |
| 136 | + const x = asks.splice(i, 1)[0]; |
| 137 | + reject ? x.rej(new Error("boom")) : x.res(v); |
| 138 | + } |
| 139 | + for (let k = 0; k < 4; k++) { |
| 140 | + await vi.advanceTimersByTimeAsync(0); |
| 141 | + flush(); |
| 142 | + } |
| 143 | + }; |
| 144 | + const t = (el: { text: string } | null) => (el ? el.text : "-"); |
| 145 | + const screen = () => |
| 146 | + `${view.headerEl ? `${view.headerEl.text} / ` : view.header}${t(view.detail)} | ${t(view.comments)}`; |
| 147 | + return { setSel, settle, screen, dispose }; |
| 148 | +} |
| 149 | + |
| 150 | +function captureWarnings() { |
| 151 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 152 | + const capture = OBSERVE!.diagnostics.capture(); |
| 153 | + return { |
| 154 | + codes: () => capture.events.map(e => e.code).filter(c => c === "LOADING_ON_OUTSIDE_HOLD"), |
| 155 | + stop: () => capture.stop() |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +describe("#4639 an error boundary output reached only behind fallbacks holds nothing", () => { |
| 160 | + for (const mode of ["direct", "memo", "nested", "unread", "ueff", "lattice"] as Mode[]) |
| 161 | + for (const reject of mode === "unread" || mode === "lattice" ? [false] : [false, true]) |
| 162 | + test(`${mode}${reject ? ", detail rejects" : ""}: both fallbacks show now, no LOADING_ON_OUTSIDE_HOLD`, async () => { |
| 163 | + const d = captureWarnings(); |
| 164 | + const p = page(mode); |
| 165 | + flush(); |
| 166 | + await p.settle("detail:3", { title: "Crash" }); |
| 167 | + await p.settle("comments:3", [1]); |
| 168 | + expect(p.screen()).toBe("Crash | comments 1"); |
| 169 | + p.setSel(1); |
| 170 | + flush(); |
| 171 | + await p.settle(""); |
| 172 | + expect(p.screen()).toBe("loading #1 | loading comments"); |
| 173 | + await p.settle("detail:1", { title: "One" }, reject); |
| 174 | + const shown = reject ? "ERR" : "One"; |
| 175 | + expect(p.screen()).toBe(`${shown} | loading comments`); |
| 176 | + await p.settle("comments:1", [1, 2]); |
| 177 | + expect(p.screen()).toBe(`${shown} | comments 2`); |
| 178 | + expect(d.codes()).toEqual([]); |
| 179 | + d.stop(); |
| 180 | + p.dispose(); |
| 181 | + }); |
| 182 | + |
| 183 | + for (const mode of ["header", "header-tree"] as Mode[]) |
| 184 | + test(`${mode} (D-16): the output rendered outside holds the frame; LOADING_ON_OUTSIDE_HOLD once`, async () => { |
| 185 | + const d = captureWarnings(); |
| 186 | + const p = page(mode); |
| 187 | + flush(); |
| 188 | + await p.settle("detail:3", { title: "Crash" }); |
| 189 | + await p.settle("comments:3", [1]); |
| 190 | + expect(p.screen()).toBe("Crash / Crash | comments 1"); |
| 191 | + p.setSel(1); |
| 192 | + flush(); |
| 193 | + await p.settle(""); |
| 194 | + expect(p.screen()).toBe("Crash / Crash | comments 1"); |
| 195 | + expect(d.codes()).toEqual(["LOADING_ON_OUTSIDE_HOLD"]); |
| 196 | + await p.settle("detail:1", { title: "One" }); |
| 197 | + expect(p.screen()).toBe("One / One | loading comments"); |
| 198 | + await p.settle("comments:1", [1, 2]); |
| 199 | + expect(p.screen()).toBe("One / One | comments 2"); |
| 200 | + d.stop(); |
| 201 | + p.dispose(); |
| 202 | + }); |
| 203 | +}); |
0 commit comments