|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + * |
| 5 | + * #4474 through `Loading` + `insert` (the signals pin is |
| 6 | + * `signals/tests/latest-unread-identity-memo-4474.test.ts`): laziness is |
| 7 | + * never observable (D-22, ruled 2026-10-30). |
| 8 | + * |
| 9 | + * `m` is an async memo over `s`; `id = createMemo(() => m())` is an identity |
| 10 | + * memo created at module level that nothing reads. `m`'s first answer (2) |
| 11 | + * lands in its own flush — the unread `id` is released without running |
| 12 | + * (#2934). A write re-asks `m`, and in that same flush a `Loading` boundary |
| 13 | + * mounts content reading `latest(id)` and `isPending(id)`: `l:2 p:true`, then |
| 14 | + * `l:3 p:false` at the landing — the same screen as with a prior reader of |
| 15 | + * `id`, in either read order with a plain `{id()}` beside it. |
| 16 | + * |
| 17 | + * Deviation today (D-22 Status; the memo is unowned, not lazy, and the |
| 18 | + * settle walk's release at `m`'s landing deletes the retry it just queued — |
| 19 | + * fix in progress at the release): the mount reads `l:undefined p:true` — |
| 20 | + * the verdict-only row is pinned `test.fails` at the ruled screen. The two |
| 21 | + * rows with a plain `id()` beside the verdict reads |
| 22 | + * already show the ruled screen (the born-held reader keeps the mount on |
| 23 | + * `off` until the landing, F-5) and are the control. |
| 24 | + */ |
| 25 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 26 | +import { |
| 27 | + Loading, |
| 28 | + Show, |
| 29 | + createEffect, |
| 30 | + createMemo, |
| 31 | + createRoot, |
| 32 | + createSignal, |
| 33 | + flush, |
| 34 | + isPending, |
| 35 | + latest |
| 36 | +} from "solid-js"; |
| 37 | +import { render } from "../src/index.js"; |
| 38 | + |
| 39 | +beforeEach(() => vi.useFakeTimers()); |
| 40 | +afterEach(() => vi.useRealTimers()); |
| 41 | + |
| 42 | +const tick = async () => { |
| 43 | + await vi.advanceTimersByTimeAsync(0); |
| 44 | + flush(); |
| 45 | +}; |
| 46 | + |
| 47 | +type Plain = "none" | "before" | "after"; |
| 48 | + |
| 49 | +async function run(plain: Plain, prior: boolean) { |
| 50 | + const asks: { v: number; resolve: (v: number) => void }[] = []; |
| 51 | + const [s, setS] = createSignal(1); |
| 52 | + const m = createMemo(() => { |
| 53 | + const v = 1 + s(); |
| 54 | + return new Promise<number>(resolve => asks.push({ v, resolve })); |
| 55 | + }); |
| 56 | + const id = createMemo(() => m()); |
| 57 | + let disposePrior = () => {}; |
| 58 | + if (prior) |
| 59 | + disposePrior = createRoot(dispose => { |
| 60 | + createEffect( |
| 61 | + () => id(), |
| 62 | + () => {} |
| 63 | + ); |
| 64 | + return dispose; |
| 65 | + }); |
| 66 | + const div = document.createElement("div"); |
| 67 | + const dispose = render( |
| 68 | + () => ( |
| 69 | + <Loading fallback={<u>fallback</u>}> |
| 70 | + <Show when={s() > 1} fallback={<u>off</u>}> |
| 71 | + {plain === "before" && <b>p:{id()}</b>} |
| 72 | + <i> |
| 73 | + l:{latest(id)} p:{String(isPending(id))} |
| 74 | + </i> |
| 75 | + {plain === "after" && <b>p:{id()}</b>} |
| 76 | + </Show> |
| 77 | + </Loading> |
| 78 | + ), |
| 79 | + div |
| 80 | + ); |
| 81 | + const screen = () => div.textContent; |
| 82 | + flush(); |
| 83 | + await tick(); |
| 84 | + const screens = [screen()]; |
| 85 | + const first = asks.shift()!; |
| 86 | + first.resolve(first.v); |
| 87 | + await tick(); |
| 88 | + await tick(); // m's answer (2) lands on its own |
| 89 | + screens.push(screen()); |
| 90 | + setS(2); |
| 91 | + await tick(); |
| 92 | + await tick(); // re-ask m and mount the content |
| 93 | + screens.push(screen()); |
| 94 | + const second = asks.shift()!; |
| 95 | + second.resolve(second.v); |
| 96 | + await tick(); |
| 97 | + await tick(); |
| 98 | + screens.push(screen()); |
| 99 | + dispose(); |
| 100 | + disposePrior(); |
| 101 | + return screens; |
| 102 | +} |
| 103 | + |
| 104 | +describe("#4474 (web): latest() of an unread identity memo first read in the flush that re-asks its source", () => { |
| 105 | + // primed (off) | m's 2 landed | re-asked + mounted | landed |
| 106 | + test.fails( |
| 107 | + "verdict reads only: l:2 p:true at the mount, l:3 p:false at the landing (#4474, ruled; today l:undefined p:true)", |
| 108 | + async () => { |
| 109 | + const lazy = await run("none", false); |
| 110 | + expect(lazy).toEqual(["off", "off", "l:2 p:true", "l:3 p:false"]); |
| 111 | + expect(lazy).toEqual(await run("none", true)); |
| 112 | + } |
| 113 | + ); |
| 114 | + |
| 115 | + // A plain `id()` in the same mount is not display-ahead (D-27): its reader |
| 116 | + // is born held (D-2) and the mount — the boundary is disarmed, it showed |
| 117 | + // `off` — stays on `off` until the landing (F-5), in either read order. |
| 118 | + for (const plain of ["before", "after"] as const) |
| 119 | + test(`a plain id() ${plain} the verdict reads: the mount waits for the landing, as with a prior reader`, async () => { |
| 120 | + const lazy = await run(plain, false); |
| 121 | + expect(lazy).toEqual([ |
| 122 | + "off", |
| 123 | + "off", |
| 124 | + "off", |
| 125 | + plain === "before" ? "p:3l:3 p:false" : "l:3 p:falsep:3" |
| 126 | + ]); |
| 127 | + expect(lazy).toEqual(await run(plain, true)); |
| 128 | + }); |
| 129 | +}); |
0 commit comments