// @vitest-environment jsdom /** * #649: a router action's busy state, settled hooks and submission record * release when the action's transition COMMITS — not when its body returns. * The body's final slice can start reads (the default revalidation's refetch, * a redirect target's data) that hold the transition with the old UI still on * screen; until that commits, the form stays `aria-busy`, hooks haven't run * and the submission hasn't rendered. Busy state is keyed by the form's action * URL, so a morph that strips the attribute or a re-render that replaces the * form element gets it back on (re-)claim. */ import { action, createMemo, createRoot, createSignal, For, Loading, Show } from "solid-js"; import { vi } from "vitest"; import { claimElementTree, redirect, render } from "@solidjs/web"; import { formAction as routerAction, createRouter, memoryHistory, query, useAction, useNavigate, useSubmissions } from "../src/index.js"; import type { Submission } from "../src/index.js"; type Deferred = { promise: Promise; resolve: (v: T) => void; reject: (e: unknown) => void; }; const deferred = (): Deferred => { let resolve!: (v: T) => void; let reject!: (e: unknown) => void; const promise = new Promise((res, rej) => ((resolve = res), (reject = rej))); return { promise, resolve, reject }; }; const wait = (ms: number) => new Promise(r => setTimeout(r, ms)); async function until(fn: () => boolean, timeout = 1000) { const start = Date.now(); while (!fn()) { if (Date.now() - start > timeout) throw new Error("timed out"); await wait(2); } } let seq = 0; /** * A list fed by a query, and a form posting to an action whose mutation is * gated per submission. The action returns a value without flight data, so * the default revalidation refetches the list inside the action's * transition; `holdRefetch()` gates that refetch, holding the commit. */ function setup( options: { mutation?: (text: string, gate: Promise) => Promise; formAttrs?: Record; onSubmit?: () => void; } = {} ) { const id = ++seq; const db = ["a"]; let refetchGate: Deferred | undefined; const mutationGates: Deferred[] = []; const getItems = query(async () => { if (refetchGate) await refetchGate.promise; return [...db]; }, `items-649-${id}`); const add = routerAction(async (form: FormData) => { const gate = deferred(); mutationGates.push(gate); const text = String(form.get("text")); if (options.mutation) return options.mutation(text, gate.promise); const result = await gate.promise; db.push(text); return result; }, `add-649-${id}`); const log: string[] = []; const settled: Submission[] = []; const root = document.createElement("div"); document.body.appendChild(root); const rows = () => root.querySelectorAll("li").length; const form = () => root.querySelector("form")!; const busy = (el: HTMLFormElement = form()) => el.getAttribute("aria-busy"); const [replaced, setReplaced] = createSignal(false); const List = () => { const items = createMemo(() => getItems()); const subs = useSubmissions(add); options.onSubmit && add.onSubmit(options.onSubmit); add.onSettled(s => { settled.push(s); log.push(`settled result=${s.result} rows=${rows()}`); }); const Form = (props: { gen: string }) => (
); return ( <>
    {item =>
  • {item}
  • }
{subs.length} }>
); }; const Router = createRouter({ routes: [{ path: "/", component: List }] as const, history: memoryHistory("/") }); const unmount = render( () => {props => {props.children}}, root ); let disposed = false; const dispose = () => { if (disposed) return; disposed = true; unmount(); root.remove(); }; fixtures.push(dispose); return { add, log, settled, root, rows, form, busy, replaceForm: () => setReplaced(true), submissionsOnScreen: () => root.querySelector("output")!.textContent, mutation: (i = 0) => mutationGates[i], mutationStarted: (n = 1) => until(() => mutationGates.length >= n), holdRefetch: () => (refetchGate = deferred()), releaseRefetch: () => refetchGate!.resolve(), dispose }; } // a failed assertion must not leave a router delegating submits to the next test const fixtures: (() => void)[] = []; afterEach(() => fixtures.splice(0).forEach(dispose => dispose())); describe("#649 router actions release at the transition's commit", () => { test("a held revalidation keeps the form busy, the hooks waiting and the record off screen", async () => { const t = setup(); await until(() => t.rows() === 1); t.holdRefetch(); t.form().requestSubmit(); expect(t.busy()).toBe("true"); await t.mutationStarted(); t.mutation().resolve("ok"); // the body has returned; its revalidation refetch holds the transition await wait(30); expect(t.rows()).toBe(1); expect(t.busy()).toBe("true"); expect(t.log).toEqual([]); expect(t.submissionsOnScreen()).toBe("0"); t.releaseRefetch(); await until(() => t.rows() === 2); await until(() => t.log.length > 0); expect(t.busy()).toBeNull(); // the hook observes the committed UI expect(t.log).toEqual(["settled result=ok rows=2"]); await until(() => t.submissionsOnScreen() === "1"); t.dispose(); }); test("an unheld transition still releases once the new data is on screen", async () => { const t = setup(); await until(() => t.rows() === 1); t.form().requestSubmit(); await t.mutationStarted(); t.mutation().resolve("ok"); await until(() => t.log.length > 0); expect(t.log).toEqual(["settled result=ok rows=2"]); expect(t.busy()).toBeNull(); t.dispose(); }); test("a morph that strips aria-busy is undone by the re-claim while busy, not after", async () => { const t = setup(); await until(() => t.rows() === 1); t.form().requestSubmit(); expect(t.busy()).toBe("true"); await t.mutationStarted(); // a server-component morph resets attributes to the server HTML (which // never carries aria-busy), then re-claims the element t.form().removeAttribute("aria-busy"); claimElementTree(t.form()); expect(t.busy()).toBe("true"); t.mutation().resolve("ok"); await until(() => t.log.length > 0); expect(t.busy()).toBeNull(); claimElementTree(t.form()); expect(t.busy()).toBeNull(); t.dispose(); }); test("a replacement form element for the same action URL is busy, and released", async () => { const t = setup(); await until(() => t.rows() === 1); const first = t.form(); first.requestSubmit(); await t.mutationStarted(); t.replaceForm(); await until(() => t.form().dataset.gen === "second"); const second = t.form(); expect(second).not.toBe(first); expect(t.busy(second)).toBe("true"); t.mutation().resolve("ok"); await until(() => t.log.length > 0); expect(t.busy(second)).toBeNull(); expect(t.busy(first)).toBeNull(); t.dispose(); }); test("a rejected mutation releases at commit and records the error", async () => { const t = setup({ mutation: async (_, gate) => { await gate; throw new Error("nope"); } }); await until(() => t.rows() === 1); t.form().requestSubmit(); await t.mutationStarted(); expect(t.busy()).toBe("true"); t.mutation().resolve(undefined); await until(() => t.settled.length > 0); expect(t.busy()).toBeNull(); expect((t.settled[0].error as Error).message).toBe("nope"); await until(() => t.submissionsOnScreen() === "1"); t.dispose(); }); test("a response that fails to decode (a throw before the final slice) releases and records it", async () => { const t = setup({ mutation: async (_, gate) => { await gate; // handed over whole with a body the router decodes itself: declared // JSON (the runtime's body-format header), but not JSON return new Response("{not json", { headers: { "X-Server-Function-Format": "8" } }); } }); await until(() => t.rows() === 1); t.form().requestSubmit(); await t.mutationStarted(); expect(t.busy()).toBe("true"); t.mutation().resolve(undefined); await until(() => t.settled.length > 0); expect(t.busy()).toBeNull(); expect(t.settled[0].error).toBeInstanceOf(SyntaxError); await until(() => t.submissionsOnScreen() === "1"); t.dispose(); }); test("a throwing submit hook (before the mutation runs) releases and records it", async () => { const t = setup({ onSubmit: () => { throw new Error("hook"); } }); await until(() => t.rows() === 1); t.form().requestSubmit(); await until(() => t.settled.length > 0); expect(t.busy()).toBeNull(); expect((t.settled[0].error as Error).message).toBe("hook"); await until(() => t.submissionsOnScreen() === "1"); t.dispose(); }); test("overlapping submissions keep the form busy until the last one commits", async () => { const t = setup(); await until(() => t.rows() === 1); // separate tasks: two submits in one task would share a transition t.form().requestSubmit(); await wait(0); t.form().requestSubmit(); await t.mutationStarted(2); expect(t.busy()).toBe("true"); t.mutation(0).resolve("one"); await until(() => t.settled.length === 1); expect(t.busy()).toBe("true"); t.mutation(1).resolve("two"); await until(() => t.settled.length === 2); expect(t.busy()).toBeNull(); t.dispose(); }); test("an authored aria-busy is left alone", async () => { const t = setup({ formAttrs: { "aria-busy": "false" } }); await until(() => t.rows() === 1); t.form().requestSubmit(); await t.mutationStarted(); expect(t.busy()).toBe("false"); claimElementTree(t.form()); expect(t.busy()).toBe("false"); t.mutation().resolve("ok"); await until(() => t.settled.length > 0); expect(t.busy()).toBe("false"); t.dispose(); }); test("a hook registered after the body finished doesn't run for that submission", async () => { const t = setup(); await until(() => t.rows() === 1); t.holdRefetch(); t.form().requestSubmit(); await t.mutationStarted(); t.mutation().resolve("first"); await wait(30); expect(t.log).toEqual([]); // body finished, commit held: a hook registered now is too late for it const late: unknown[] = []; const disposeLate = createRoot(dispose => { t.add.onSettled(s => void late.push(s.result)); return dispose; }); try { t.releaseRefetch(); await until(() => t.settled.length === 1); await wait(20); expect(late).toEqual([]); // ...but it is live for the next one t.form().requestSubmit(); await t.mutationStarted(2); t.mutation(1).resolve("second"); await until(() => t.settled.length === 2); expect(late).toEqual(["second"]); } finally { disposeLate(); } }); test("a hook whose owner unmounted before the submit doesn't run", async () => { const t = setup(); await until(() => t.rows() === 1); const gone: unknown[] = []; createRoot(dispose => { t.add.onSettled(s => void gone.push(s.result)); dispose(); }); t.form().requestSubmit(); await t.mutationStarted(); t.mutation().resolve("ok"); await until(() => t.settled.length === 1); await wait(20); expect(gone).toEqual([]); }); test("a throwing settled hook is reported and the hooks after it still run", async () => { const t = setup(); await until(() => t.rows() === 1); const after: unknown[] = []; const disposeHooks = createRoot(dispose => { t.add.onSettled(() => { throw new Error("hook threw"); }); t.add.onSettled(s => void after.push(s.result)); return dispose; }); // the error is rethrown on its own microtask; catch it there const reported: unknown[] = []; const original = globalThis.queueMicrotask; const spy = vi.spyOn(globalThis, "queueMicrotask").mockImplementation(callback => original(() => { try { callback(); } catch (e) { reported.push(e); } }) ); try { t.form().requestSubmit(); await t.mutationStarted(); t.mutation().resolve("ok"); await until(() => after.length > 0); await until(() => reported.length > 0); expect(after).toEqual(["ok"]); expect(t.log).toEqual(["settled result=ok rows=2"]); expect((reported[0] as Error).message).toBe("hook threw"); } finally { spy.mockRestore(); disposeHooks(); } }); test("an aria-busy the author rewrites mid-flight is theirs from then on", async () => { const t = setup(); await until(() => t.rows() === 1); t.form().requestSubmit(); await t.mutationStarted(); expect(t.busy()).toBe("true"); t.form().setAttribute("aria-busy", "false"); claimElementTree(t.form()); expect(t.busy()).toBe("false"); t.mutation().resolve("ok"); await until(() => t.settled.length > 0); expect(t.busy()).toBe("false"); t.dispose(); }); }); describe("#649 redirects and composition", () => { test("a redirect releases at the commit that puts its target on screen", async () => { const targetGate = deferred(); const getTarget = query(async () => { await targetGate.promise; return "other"; }, "target-649-redirect"); const mutationGate = deferred(); const go = routerAction(async () => { await mutationGate.promise; return redirect("/other"); }, "go-649-redirect"); const log: string[] = []; const root = document.createElement("div"); document.body.appendChild(root); let form!: HTMLFormElement; const Home = () => { // owned by the page the redirect leaves: the commit disposes it before // the settle fires, but it was registered when the body finished go.onSettled(s => void log.push(`page hook result=${s.result} text=${root.textContent}`)); return (
); }; const Other = () => { const target = createMemo(() => getTarget()); return {target()}; }; const Router = createRouter({ routes: [ { path: "/", component: Home }, { path: "/other", component: Other } ] as const, history: memoryHistory("/") }); const dispose = render( () => ( {props => { go.onSettled(() => void log.push(`settled text=${root.textContent}`)); return {props.children}; }} ), root ); try { await until(() => root.textContent === "home"); form.requestSubmit(); expect(form.getAttribute("aria-busy")).toBe("true"); mutationGate.resolve(); // navigated inside the transition; the target's data holds the commit await wait(30); expect(root.textContent).toBe("home"); expect(form.getAttribute("aria-busy")).toBe("true"); expect(log).toEqual([]); targetGate.resolve(); await until(() => log.length > 1); await wait(20); // each exactly once, both at the commit showing the target expect([...log].sort()).toEqual([ "page hook result=undefined text=other", "settled text=other" ]); expect(form.getAttribute("aria-busy")).toBeNull(); } finally { dispose(); root.remove(); } }); test("a failed submission's hook runs at the commit that unmounts its owner", async () => { const targetGate = deferred(); const getTarget = query(async () => { await targetGate.promise; return "other"; }, "target-649-failed"); const fail = routerAction(async () => { await wait(5); throw new Error("nope"); }, "fail-649-unmount"); const log: string[] = []; const root = document.createElement("div"); let leave!: () => Promise; const Home = () => { fail.onSettled( s => void log.push(`page hook error=${(s.error as Error).message} text=${root.textContent}`) ); const call = useAction(fail); const navigate = useNavigate(); // the failure is the inner submission's; the outer action moves on and // navigates inside the shared transition, held by the target's data leave = action(function* () { try { yield call(); } catch {} navigate("/other"); }); return home; }; const Other = () => { const target = createMemo(() => getTarget()); return {target()}; }; const Router = createRouter({ routes: [ { path: "/", component: Home }, { path: "/other", component: Other } ] as const, history: memoryHistory("/") }); const dispose = render( () => {props => {props.children}}, root ); try { await until(() => root.textContent === "home"); const done = leave(); await done; await wait(30); expect(root.textContent).toBe("home"); expect(log).toEqual([]); targetGate.resolve(); await until(() => log.length > 0); await wait(20); expect(log).toEqual(["page hook error=nope text=other"]); } finally { dispose(); } }); test("a nested `yield call()` settles at the outer action's commit", async () => { const log: string[] = []; const inner = routerAction(async () => { await wait(5); return "inner"; }, "inner-649-nested"); inner.onSettled(s => void log.push(`settled ${s.result}`)); let outer!: () => Promise; const outerGate = deferred(); const Home = () => { const subs = useSubmissions(inner); const call = useAction(inner); outer = action(function* () { const v = yield call(); log.push(`outer got ${v}`); // the outer body keeps the shared transition open yield outerGate.promise; return "outer"; }); return {subs.length}; }; const Router = createRouter({ routes: [{ path: "/", component: Home }] as const, history: memoryHistory("/") }); const root = document.createElement("div"); const dispose = render( () => {props => {props.children}}, root ); try { await until(() => root.textContent === "0"); const done = outer(); await until(() => log.length > 0); await wait(20); // the inner promise resolved (the outer body moved on); the inner's // settle waits for the outer commit expect(log).toEqual(["outer got inner"]); expect(root.textContent).toBe("0"); outerGate.resolve(); await done; await until(() => log.length > 1); expect(log).toEqual(["outer got inner", "settled inner"]); await until(() => root.textContent === "1"); } finally { dispose(); } }); });