|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + */ |
| 5 | + |
| 6 | +import { expect, test } from "vitest"; |
| 7 | +import { createSignal, flush, latest } from "solid-js"; |
| 8 | +import { render } from "../src/index.js"; |
| 9 | + |
| 10 | +function deferred<T>() { |
| 11 | + let resolve!: (value: T) => void; |
| 12 | + const promise = new Promise<T>(r => (resolve = r)); |
| 13 | + return { promise, resolve }; |
| 14 | +} |
| 15 | + |
| 16 | +async function settle() { |
| 17 | + await Promise.resolve(); |
| 18 | + await Promise.resolve(); |
| 19 | + flush(); |
| 20 | +} |
| 21 | + |
| 22 | +test("latest() condition does not reveal an unready async branch (#3276)", async () => { |
| 23 | + const div = document.createElement("div"); |
| 24 | + const answer = deferred<string>(); |
| 25 | + const trace: Array<Record<string, unknown>> = []; |
| 26 | + const log = (event: string, details: Record<string, unknown> = {}) => { |
| 27 | + trace.push({ event, ...details }); |
| 28 | + }; |
| 29 | + let toggle!: () => void; |
| 30 | + |
| 31 | + const dispose = render(() => { |
| 32 | + const [entangle, setEntangle] = createSignal(false); |
| 33 | + const [asyncValue] = createSignal(async () => { |
| 34 | + log("async.compute.start"); |
| 35 | + const value = await answer.promise; |
| 36 | + log("async.compute.resolve", { value }); |
| 37 | + return value; |
| 38 | + }); |
| 39 | + const a = () => { |
| 40 | + const condition = latest(entangle); |
| 41 | + log("branch.a", { condition, value: condition ? asyncValue() : "BAD" }); |
| 42 | + return condition ? asyncValue() : "BAD"; |
| 43 | + }; |
| 44 | + const b = () => { |
| 45 | + const condition = entangle(); |
| 46 | + log("branch.b", { condition, value: condition ? asyncValue() : "OK" }); |
| 47 | + return condition ? asyncValue() : "OK"; |
| 48 | + }; |
| 49 | + toggle = () => { |
| 50 | + log("toggle.before", { entangle: entangle() }); |
| 51 | + setEntangle(value => !value); |
| 52 | + log("toggle.after", { entangle: entangle(), latest: latest(entangle) }); |
| 53 | + }; |
| 54 | + |
| 55 | + return ( |
| 56 | + <main> |
| 57 | + <button onClick={toggle}>toggle</button> |
| 58 | + <p id="a">{a()}</p> |
| 59 | + <p id="b">{b()}</p> |
| 60 | + </main> |
| 61 | + ); |
| 62 | + }, div); |
| 63 | + |
| 64 | + expect(div.querySelector("#a")!.textContent).toBe("BAD"); |
| 65 | + expect(div.querySelector("#b")!.textContent).toBe("OK"); |
| 66 | + |
| 67 | + toggle(); |
| 68 | + flush(); |
| 69 | + if (process.env.SOLID_ISSUE_3276_TRACE) |
| 70 | + process.stderr.write(`ISSUE-3276 TRACE ${JSON.stringify(trace)}\n`); |
| 71 | + |
| 72 | + // A must obey the same async gate as B: neither branch may expose an |
| 73 | + // uninitialized value before the async computation resolves. |
| 74 | + expect(div.querySelector("#a")!.textContent).toBe("BAD"); |
| 75 | + expect(div.querySelector("#b")!.textContent).toBe("OK"); |
| 76 | + |
| 77 | + answer.resolve("ready"); |
| 78 | + await settle(); |
| 79 | + expect(div.querySelector("#a")!.textContent).toBe("ready"); |
| 80 | + expect(div.querySelector("#b")!.textContent).toBe("ready"); |
| 81 | + dispose(); |
| 82 | +}); |
0 commit comments