Skip to content

Commit b27247e

Browse files
committed
test(router): e2e and consumer coverage for the app version of location state (#994)
Both apps, dev and production, through shared bodies in tests/shared-e2e: - state another app version stored reads as no state after a reload, typed and plain, with a clean hydration; a reload under the same version keeps it. "Another build is running" is modelled by rewriting the version the entry records (or removing it, an entry from before the record existed). - back/forward to an entry another version wrote reads no state, without a document load. - a slot whose value validate rejects reads as no state (kept apart from the version cases so it goes with the option if that is removed). The grid fixture now has a typed reader, a plain-state reader and a validated one; its `VersionedGrid` definition is `GridState`, without options. Dev only, in the local loader-hmr suite: an RSC edit bumps the app version in the running page, the page keeps its location state, and the next document load drops it. cloudflare-basic's dogfood test covers the same through renderRoute and runMiddleware: seeds read back without a version, and a middleware's locationState carries none.
1 parent d564cf4 commit b27247e

11 files changed

Lines changed: 412 additions & 159 deletions

File tree

‎packages/rangojs-router/e2e/loader-hmr.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, test, type APIRequestContext } from "@playwright/test";
2+
import { waitForShellHydration } from "@shared/e2e";
23
import { useFixture } from "./fixture";
34
import fs from "node:fs/promises";
45
import path from "node:path";
@@ -162,4 +163,52 @@ export const HMRAnotherLoader = createLoader(
162163
);
163164
expect(text2).toContain("Fetched via GET!");
164165
});
166+
167+
// #994: location state is recorded under the app version its document
168+
// loaded with. An RSC edit bumps the dev version inside the running page
169+
// (rsc-router.tsx HMR handler -> bridge.updateVersion); the page must keep
170+
// its location state, and the next document load, now under the new
171+
// version, drops it.
172+
test("an RSC edit keeps the page's location state; the next document load drops it", async ({
173+
page,
174+
}) => {
175+
const grid = page.getByTestId("grid-value");
176+
const step = page.getByTestId("grid-step");
177+
178+
await page.goto(f.url("/location-state/grid-options"));
179+
await waitForShellHydration(page);
180+
await page.getByTestId("grid-write").click();
181+
await expect(grid).toHaveText("desc:3");
182+
183+
const versionBumped = page.waitForEvent("console", {
184+
predicate: (message) => message.text().includes("HMR: version changed"),
185+
timeout: 15_000,
186+
});
187+
const refetched = page.waitForEvent("console", {
188+
predicate: (message) =>
189+
message.text().includes("HMR: RSC stream complete"),
190+
timeout: 15_000,
191+
});
192+
await fs.writeFile(
193+
loadersPath,
194+
`${originalContent}\n// location-state HMR probe\n`,
195+
);
196+
await versionBumped;
197+
await refetched;
198+
199+
// The same document under a bumped version: the mounted reader, a
200+
// back/forward and a new write all still see the state.
201+
await expect(grid).toHaveText("desc:3");
202+
await page.getByTestId("grid-next").click();
203+
await expect(step).toHaveText("next");
204+
await expect(grid).toHaveText("none");
205+
await page.goBack();
206+
await expect(step).toHaveText("typed");
207+
await expect(grid).toHaveText("desc:3");
208+
209+
await page.reload();
210+
await waitForShellHydration(page);
211+
await expect(page.getByTestId("grid-mounted")).toHaveText("yes");
212+
await expect(grid).toHaveText("none");
213+
});
165214
});

‎packages/rangojs-router/e2e/location-state.test.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
expectClearOnReloadDropsCarriedState,
44
expectClearOnReloadDropsStateOnTraversalLoad,
55
expectLateSuspenseReaderHydratesClean,
6-
expectStaleLocationStateReadsAsNone,
6+
expectOtherVersionLocationStateDroppedOnLoad,
7+
expectOtherVersionLocationStateDroppedOnTraversal,
8+
expectRejectedLocationStateReadsAsNone,
79
returnToEvictedEntry,
810
} from "@shared/e2e";
911
import { useFixture, type Fixture } from "./fixture";
@@ -1471,7 +1473,8 @@ test.describe("location-state.late-suspense (production)", () => {
14711473
lateSuspenseSuite(f);
14721474
});
14731475

1474-
// #994: createLocationState({ clearOnReload }), ({ version }), ({ validate }).
1476+
// #994: createLocationState({ clearOnReload }), ({ validate }), and the app
1477+
// version every entry's location state is recorded under.
14751478
function optionsSuite(f: Fixture) {
14761479
test("clearOnReload state is carried by a client navigation and dropped by a reload", async ({
14771480
page,
@@ -1491,10 +1494,28 @@ function optionsSuite(f: Fixture) {
14911494
);
14921495
});
14931496

1494-
test("a versioned or validated slot another deploy stored reads as no state after a reload", async ({
1497+
test("state another app version stored reads as no state after a reload, its own is kept", async ({
14951498
page,
14961499
}) => {
1497-
await expectStaleLocationStateReadsAsNone(
1500+
await expectOtherVersionLocationStateDroppedOnLoad(
1501+
page,
1502+
f.url("/location-state/grid-options"),
1503+
);
1504+
});
1505+
1506+
test("back/forward to an entry another app version wrote reads no state", async ({
1507+
page,
1508+
}) => {
1509+
await expectOtherVersionLocationStateDroppedOnTraversal(
1510+
page,
1511+
f.url("/location-state/grid-options"),
1512+
);
1513+
});
1514+
1515+
test("a slot whose value validate rejects reads as no state after a reload", async ({
1516+
page,
1517+
}) => {
1518+
await expectRejectedLocationStateReadsAsNone(
14981519
page,
14991520
f.url("/location-state/grid-options"),
15001521
);

‎packages/rangojs-router/e2e/test-app/src/components/LocationStateOptions.tsx‎

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"use client";
22

3+
import { useEffect, useState } from "react";
34
import { Link, useLocationState } from "@rangojs/router/client";
45
import {
56
CarriedItems,
7+
GridState,
68
ListSort,
79
ServerPageStamp,
810
ValidatedGrid,
9-
VersionedGrid,
1011
} from "../location-states.js";
1112

1213
/**
@@ -64,22 +65,41 @@ export function LoadMoreList({
6465
}
6566

6667
/**
67-
* #994 version / validate. Each reader shows `order:page` or "none"; the keys
68-
* are rendered so the e2e can store what another deploy would have.
68+
* #994 state another app version stored, and `validate`. Each reader shows its
69+
* value or "none": a typed slot (`grid-value`), plain state (`plain-value`)
70+
* and a validated slot (`val-value`, with its key so the e2e can store a value
71+
* the guard rejects). `step` is the server-rendered `?step`, so the e2e can
72+
* tell which entry is committed.
73+
*
74+
* Nothing in location state survives a version change, so no slot can signal
75+
* that the client snapshots are applied: `grid-mounted` turns "yes" in an
76+
* effect, which runs after them.
6977
*/
70-
export function GridOptionsPanel({ basePath }: { basePath: string }) {
71-
const versioned = useLocationState(VersionedGrid);
78+
export function GridOptionsPanel({
79+
basePath,
80+
step,
81+
}: {
82+
basePath: string;
83+
step: string;
84+
}) {
85+
const grid = useLocationState(GridState);
86+
const plain = useLocationState<{ from?: string }>();
7287
const validated = useLocationState(ValidatedGrid);
73-
const control = useLocationState(ListSort);
74-
const grid = { order: "desc", page: 3 } as const;
88+
const [mounted, setMounted] = useState(false);
89+
useEffect(() => setMounted(true), []);
90+
const snapshot = { order: "desc", page: 3 } as const;
7591
return (
7692
<section>
7793
<p>
78-
versioned{" "}
79-
<span data-testid="vg-value">
80-
{versioned ? `${versioned.order}:${versioned.page}` : "none"}
81-
</span>{" "}
82-
<code data-testid="vg-key">{VersionedGrid.__rsc_ls_key}</code>
94+
step <span data-testid="grid-step">{step}</span>, mounted{" "}
95+
<span data-testid="grid-mounted">{mounted ? "yes" : "no"}</span>
96+
</p>
97+
<p>
98+
typed{" "}
99+
<span data-testid="grid-value">
100+
{grid ? `${grid.order}:${grid.page}` : "none"}
101+
</span>
102+
, plain <span data-testid="plain-value">{plain?.from ?? "none"}</span>
83103
</p>
84104
<p>
85105
validated{" "}
@@ -88,20 +108,22 @@ export function GridOptionsPanel({ basePath }: { basePath: string }) {
88108
</span>{" "}
89109
<code data-testid="val-key">{ValidatedGrid.__rsc_ls_key}</code>
90110
</p>
91-
<p>
92-
control{" "}
93-
<span data-testid="grid-control">{control?.order ?? "none"}</span>
94-
</p>
95111
<Link
96-
to={`${basePath}?written=1`}
97-
state={[
98-
VersionedGrid(grid),
99-
ValidatedGrid(grid),
100-
ListSort({ order: "control" }),
101-
]}
112+
to={`${basePath}?step=typed`}
113+
state={[GridState(snapshot), ValidatedGrid(snapshot)]}
102114
data-testid="grid-write"
103115
>
104-
Write grid state
116+
Write typed state
117+
</Link>{" "}
118+
<Link
119+
to={`${basePath}?step=plain`}
120+
state={{ from: "panel" }}
121+
data-testid="plain-write"
122+
>
123+
Write plain state
124+
</Link>{" "}
125+
<Link to={`${basePath}?step=next`} data-testid="grid-next">
126+
Next entry, no state
105127
</Link>
106128
</section>
107129
);

‎packages/rangojs-router/e2e/test-app/src/location-states.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ export interface GridSnapshot {
110110
page: number;
111111
}
112112

113-
// #994: the e2e stores what other deploys of this definition would have, under
114-
// version 1's key and under the key from before it had a version.
115-
export const VersionedGrid = createLocationState<GridSnapshot>({ version: 2 });
113+
// #994: no options. The e2e rewrites the app version its entry records, the
114+
// way an entry an older build wrote would carry another one.
115+
export const GridState = createLocationState<GridSnapshot>();
116116

117117
// Unguarded on purpose: a slot that is not an object makes it throw, which
118118
// must read as undefined instead of failing the render.

‎packages/rangojs-router/e2e/test-app/src/urls/location-state.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,16 @@ export const locationStatePatterns = urls(({ path, middleware }) => [
332332
{ name: "loadMore" },
333333
),
334334

335-
// #994 version / validate: readers of a versioned and a validated slot.
335+
// #994 app version / validate: readers of a typed slot, plain state and a
336+
// validated slot.
336337
path(
337338
"/grid-options",
338-
() => <GridOptionsPanel basePath="/location-state/grid-options" />,
339+
(ctx) => (
340+
<GridOptionsPanel
341+
basePath="/location-state/grid-options"
342+
step={ctx.searchParams.get("step") ?? "start"}
343+
/>
344+
),
339345
{ name: "gridOptions" },
340346
),
341347

‎tests/cloudflare-basic/e2e/location-state.test.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
expectClearOnReloadDropsCarriedState,
44
expectClearOnReloadDropsStateOnTraversalLoad,
55
expectLateSuspenseReaderHydratesClean,
6-
expectStaleLocationStateReadsAsNone,
6+
expectOtherVersionLocationStateDroppedOnLoad,
7+
expectOtherVersionLocationStateDroppedOnTraversal,
8+
expectRejectedLocationStateReadsAsNone,
79
returnToEvictedEntry,
810
} from "@shared/e2e";
911
import { useFixture } from "./fixture";
@@ -275,7 +277,8 @@ function describeLateSuspense(mode: "dev" | "build") {
275277
});
276278
}
277279

278-
// #994: createLocationState({ clearOnReload }), ({ version }), ({ validate }).
280+
// #994: createLocationState({ clearOnReload }), ({ validate }), and the app
281+
// version every entry's location state is recorded under.
279282
function describeOptions(mode: "dev" | "build") {
280283
const label = mode === "build" ? "production" : "dev";
281284
test.describe(`location-state.options (${label})`, () => {
@@ -299,10 +302,28 @@ function describeOptions(mode: "dev" | "build") {
299302
);
300303
});
301304

302-
test("a versioned or validated slot another deploy stored reads as no state after a reload", async ({
305+
test("state another app version stored reads as no state after a reload, its own is kept", async ({
303306
page,
304307
}) => {
305-
await expectStaleLocationStateReadsAsNone(
308+
await expectOtherVersionLocationStateDroppedOnLoad(
309+
page,
310+
f.url("/location-state-grid-options"),
311+
);
312+
});
313+
314+
test("back/forward to an entry another app version wrote reads no state", async ({
315+
page,
316+
}) => {
317+
await expectOtherVersionLocationStateDroppedOnTraversal(
318+
page,
319+
f.url("/location-state-grid-options"),
320+
);
321+
});
322+
323+
test("a slot whose value validate rejects reads as no state after a reload", async ({
324+
page,
325+
}) => {
326+
await expectRejectedLocationStateReadsAsNone(
306327
page,
307328
f.url("/location-state-grid-options"),
308329
);

‎tests/cloudflare-basic/src/components/LocationStateOptions.tsx‎

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"use client";
22

3+
import { useEffect, useState } from "react";
34
import { Link, useLocationState } from "@rangojs/router/client";
45
import {
56
CarriedItems,
7+
GridState,
68
ListSort,
79
ServerPageStamp,
810
ValidatedGrid,
9-
VersionedGrid,
1011
} from "../location-states.js";
1112

1213
/**
@@ -64,22 +65,41 @@ export function LoadMoreList({
6465
}
6566

6667
/**
67-
* #994 version / validate. Each reader shows `order:page` or "none"; the keys
68-
* are rendered so the e2e can store what another deploy would have.
68+
* #994 state another app version stored, and `validate`. Each reader shows its
69+
* value or "none": a typed slot (`grid-value`), plain state (`plain-value`)
70+
* and a validated slot (`val-value`, with its key so the e2e can store a value
71+
* the guard rejects). `step` is the server-rendered `?step`, so the e2e can
72+
* tell which entry is committed.
73+
*
74+
* Nothing in location state survives a version change, so no slot can signal
75+
* that the client snapshots are applied: `grid-mounted` turns "yes" in an
76+
* effect, which runs after them.
6977
*/
70-
export function GridOptionsPanel({ basePath }: { basePath: string }) {
71-
const versioned = useLocationState(VersionedGrid);
78+
export function GridOptionsPanel({
79+
basePath,
80+
step,
81+
}: {
82+
basePath: string;
83+
step: string;
84+
}) {
85+
const grid = useLocationState(GridState);
86+
const plain = useLocationState<{ from?: string }>();
7287
const validated = useLocationState(ValidatedGrid);
73-
const control = useLocationState(ListSort);
74-
const grid = { order: "desc", page: 3 } as const;
88+
const [mounted, setMounted] = useState(false);
89+
useEffect(() => setMounted(true), []);
90+
const snapshot = { order: "desc", page: 3 } as const;
7591
return (
7692
<section>
7793
<p>
78-
versioned{" "}
79-
<span data-testid="vg-value">
80-
{versioned ? `${versioned.order}:${versioned.page}` : "none"}
81-
</span>{" "}
82-
<code data-testid="vg-key">{VersionedGrid.__rsc_ls_key}</code>
94+
step <span data-testid="grid-step">{step}</span>, mounted{" "}
95+
<span data-testid="grid-mounted">{mounted ? "yes" : "no"}</span>
96+
</p>
97+
<p>
98+
typed{" "}
99+
<span data-testid="grid-value">
100+
{grid ? `${grid.order}:${grid.page}` : "none"}
101+
</span>
102+
, plain <span data-testid="plain-value">{plain?.from ?? "none"}</span>
83103
</p>
84104
<p>
85105
validated{" "}
@@ -88,20 +108,22 @@ export function GridOptionsPanel({ basePath }: { basePath: string }) {
88108
</span>{" "}
89109
<code data-testid="val-key">{ValidatedGrid.__rsc_ls_key}</code>
90110
</p>
91-
<p>
92-
control{" "}
93-
<span data-testid="grid-control">{control?.order ?? "none"}</span>
94-
</p>
95111
<Link
96-
to={`${basePath}?written=1`}
97-
state={[
98-
VersionedGrid(grid),
99-
ValidatedGrid(grid),
100-
ListSort({ order: "control" }),
101-
]}
112+
to={`${basePath}?step=typed`}
113+
state={[GridState(snapshot), ValidatedGrid(snapshot)]}
102114
data-testid="grid-write"
103115
>
104-
Write grid state
116+
Write typed state
117+
</Link>{" "}
118+
<Link
119+
to={`${basePath}?step=plain`}
120+
state={{ from: "panel" }}
121+
data-testid="plain-write"
122+
>
123+
Write plain state
124+
</Link>{" "}
125+
<Link to={`${basePath}?step=next`} data-testid="grid-next">
126+
Next entry, no state
105127
</Link>
106128
</section>
107129
);

0 commit comments

Comments
 (0)