@sigil-dev/grimoire 0.9.6 → 0.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,366 +1,366 @@
1
- import { beforeEach, describe, expect, test, vi } from "bun:test";
2
- import type { Document, History, Location, Window } from "happy-dom";
3
- import {
4
- clearDeps,
5
- depends,
6
- hasDep,
7
- setDeps,
8
- startCollecting,
9
- stopCollecting,
10
- } from "../src/client/deps.ts";
11
-
12
- // test/preload.ts installs happy-dom globals at runtime; this package's
13
- // compiler options have no DOM lib, so declare the shapes we rely on.
14
- declare global {
15
- // biome-ignore-all lint/style/noVar: ambient global declarations require var
16
- var window: Window;
17
- var document: Document;
18
- var location: Location;
19
- var history: History;
20
- var MouseEvent: Window["MouseEvent"];
21
- var Event: Window["Event"];
22
- var requestAnimationFrame: Window["requestAnimationFrame"];
23
- }
24
-
25
- // router.ts reads `location` at module load — DOM globals must exist first.
26
- // Dynamic import is required because the module under test captures location
27
- // during evaluation, which static-hoisted imports would race. The default
28
- // about:blank URL has origin "null", which breaks new URL(href, origin) —
29
- // point the window at a real origin first.
30
- window.location.href = "http://localhost/";
31
- const globalEnv = globalThis as typeof globalThis & {
32
- location: Location;
33
- history: History;
34
- requestAnimationFrame: Window["requestAnimationFrame"];
35
- };
36
- globalEnv.location = window.location;
37
- globalEnv.history = window.history;
38
- globalEnv.requestAnimationFrame = window.requestAnimationFrame.bind(window);
39
- const router = await import("../src/client/router.ts");
40
-
41
- function jsonResponse(pattern: string): Response {
42
- return new Response(
43
- JSON.stringify({
44
- data: {},
45
- layoutData: [],
46
- params: {},
47
- pattern,
48
- head: "",
49
- }),
50
- { headers: { "content-type": "application/json" } },
51
- );
52
- }
53
-
54
- function captureFetch(): {
55
- calls: Array<{ input: string; init?: RequestInit }>;
56
- restore: () => void;
57
- } {
58
- const calls: Array<{ input: string; init?: RequestInit }> = [];
59
- const original = globalThis.fetch;
60
- // test shim over the global — precise fetch typing is impossible here.
61
- // Echoes the requested pathname as the route pattern, mirroring renderRoute.
62
- globalThis.fetch = (async (
63
- input: string,
64
- init?: RequestInit,
65
- ): Promise<Response> => {
66
- calls.push({ input, init });
67
- const pattern = new URL(input, "http://localhost").pathname;
68
- return jsonResponse(pattern);
69
- }) as unknown as typeof fetch;
70
- return {
71
- calls,
72
- restore: () => {
73
- globalThis.fetch = original;
74
- },
75
- };
76
- }
77
-
78
- describe("depends", () => {
79
- test("no-op outside collection", () => {
80
- depends("posts");
81
- expect(hasDep("/", "posts")).toBe(false);
82
- });
83
-
84
- test("registers key during collection", () => {
85
- startCollecting();
86
- depends("posts");
87
- stopCollecting("/");
88
- expect(hasDep("/", "posts")).toBe(true);
89
- });
90
-
91
- test("multiple keys", () => {
92
- startCollecting();
93
- depends("posts");
94
- depends("tags");
95
- stopCollecting("/");
96
- expect(hasDep("/", "posts")).toBe(true);
97
- expect(hasDep("/", "tags")).toBe(true);
98
- });
99
-
100
- test("cleared on next collection", () => {
101
- startCollecting();
102
- depends("posts");
103
- stopCollecting("/");
104
- startCollecting();
105
- depends("comments");
106
- stopCollecting("/");
107
- expect(hasDep("/", "posts")).toBe(false);
108
- expect(hasDep("/", "comments")).toBe(true);
109
- });
110
- });
111
- describe("navigate — view transitions", () => {
112
- test("falls back to direct swap when startViewTransition is missing", async () => {
113
- const root = document.createElement("div");
114
- root.id = "grimoire-root";
115
- document.body.appendChild(root);
116
- const fetchShim = captureFetch();
117
- try {
118
- router.initRouter({ "/": () => document.createElement("main") }, []);
119
- await router.navigate("/");
120
- expect(root.childNodes.length).toBeGreaterThan(0);
121
- } finally {
122
- fetchShim.restore();
123
- root.remove();
124
- }
125
- });
126
-
127
- test("routes the swap through startViewTransition when available", async () => {
128
- const root = document.createElement("div");
129
- root.id = "grimoire-root";
130
- document.body.appendChild(root);
131
- let vtCalled = false;
132
- const docEnv = document as Document & {
133
- startViewTransition?: (cb: () => void) => unknown;
134
- };
135
- docEnv.startViewTransition = (cb: () => void) => {
136
- vtCalled = true;
137
- cb();
138
- return undefined;
139
- };
140
- const fetchShim = captureFetch();
141
- try {
142
- router.initRouter({ "/vt": () => document.createElement("main") }, []);
143
- await router.navigate("/vt");
144
- expect(vtCalled).toBe(true);
145
- expect(root.childNodes.length).toBeGreaterThan(0);
146
- } finally {
147
- delete docEnv.startViewTransition;
148
- fetchShim.restore();
149
- root.remove();
150
- }
151
- });
152
- });
153
-
154
- describe("hover prefetch", () => {
155
- // The 80ms debounce is real timer logic under test — fake timers advance
156
- // it deterministically instead of sleeping past it.
157
- test("plain internal link prefetches route data; preload=off skips", () => {
158
- vi.useFakeTimers();
159
- router.initRouter(
160
- {
161
- "/": () => document.createElement("div"),
162
- "/off": () => document.createElement("div"),
163
- },
164
- [],
165
- );
166
- const fetchShim = captureFetch();
167
- try {
168
- const plain = document.createElement("a");
169
- plain.setAttribute("href", "/");
170
- document.body.appendChild(plain);
171
- plain.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
172
- vi.advanceTimersByTime(80);
173
- expect(fetchShim.calls.length).toBe(1);
174
- expect(fetchShim.calls[0]?.init?.headers).toEqual({
175
- "x-grimoire-navigate": "1",
176
- });
177
-
178
- const optedOut = document.createElement("a");
179
- optedOut.setAttribute("href", "/off");
180
- optedOut.setAttribute("data-sigil-preload", "off");
181
- document.body.appendChild(optedOut);
182
- optedOut.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
183
- vi.advanceTimersByTime(80);
184
- expect(fetchShim.calls.length).toBe(1); // unchanged
185
- } finally {
186
- vi.useRealTimers();
187
- fetchShim.restore();
188
- }
189
- });
190
-
191
- test("external links are never prefetched", () => {
192
- vi.useFakeTimers();
193
- const fetchShim = captureFetch();
194
- try {
195
- const ext = document.createElement("a");
196
- ext.setAttribute("href", "https://example.com/page");
197
- document.body.appendChild(ext);
198
- ext.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
199
- vi.advanceTimersByTime(80);
200
- expect(fetchShim.calls.length).toBe(0);
201
- } finally {
202
- vi.useRealTimers();
203
- fetchShim.restore();
204
- }
205
- });
206
- });
207
-
208
- describe("popstate — query string", () => {
209
- // popstate fires navigate() from an event listener, so there is no promise
210
- // to await at the call site. afterNavigate is the router's own completion
211
- // signal; a fixed sleep would be both slower and racy.
212
- function onNavigated(): Promise<void> {
213
- const { promise, resolve } = Promise.withResolvers<void>();
214
- router.afterNavigate(() => resolve());
215
- return promise;
216
- }
217
-
218
- // Back used to refetch the bare pathname, so a query-driven page rendered
219
- // unfiltered data while the address bar still showed the query.
220
- test("back to a query URL refetches with the query intact", async () => {
221
- const root = document.createElement("div");
222
- root.id = "grimoire-root";
223
- document.body.appendChild(root);
224
- window.history.pushState(null, "", "/");
225
- router.initRouter(
226
- {
227
- "/list": () => document.createElement("div"),
228
- "/other": () => document.createElement("div"),
229
- },
230
- [],
231
- );
232
- const shim = captureFetch();
233
- try {
234
- await router.navigate("/list?q=fireball&page=2");
235
- await router.navigate("/other");
236
- shim.calls.length = 0;
237
-
238
- // the browser moves history itself, then fires popstate
239
- const navigated = onNavigated();
240
- window.history.pushState(null, "", "/list?q=fireball&page=2");
241
- window.dispatchEvent(new Event("popstate"));
242
- await navigated;
243
-
244
- expect(shim.calls.map((c) => c.input)).toEqual([
245
- "/list?q=fireball&page=2",
246
- ]);
247
- } finally {
248
- router.afterNavigate(() => {});
249
- shim.restore();
250
- root.remove();
251
- }
252
- });
253
-
254
- test("a popstate that only changes the query still refetches", async () => {
255
- const root = document.createElement("div");
256
- root.id = "grimoire-root";
257
- document.body.appendChild(root);
258
- window.history.pushState(null, "", "/");
259
- router.initRouter({ "/list": () => document.createElement("div") }, []);
260
- const shim = captureFetch();
261
- try {
262
- await router.navigate("/list?page=2");
263
- shim.calls.length = 0;
264
-
265
- const navigated = onNavigated();
266
- window.history.pushState(null, "", "/list?page=1");
267
- window.dispatchEvent(new Event("popstate"));
268
- await navigated;
269
-
270
- expect(shim.calls.map((c) => c.input)).toEqual(["/list?page=1"]);
271
- } finally {
272
- router.afterNavigate(() => {});
273
- shim.restore();
274
- root.remove();
275
- }
276
- });
277
- });
278
-
279
- describe("enhance — named action URLs", () => {
280
- test("action starting with ?/ submits to the current pathname", async () => {
281
- // earlier navigate() tests moved history — reset to a clean path
282
- window.history.pushState(null, "", "/");
283
- const { enhance } = await import("../src/client/enhance.ts");
284
- const calls: string[] = [];
285
- const original = globalThis.fetch;
286
- globalThis.fetch = (async (input: string) => {
287
- calls.push(input);
288
- return new Response(null, { status: 303, headers: { location: "/" } });
289
- }) as unknown as typeof fetch;
290
-
291
- const form = document.createElement("form");
292
- document.body.appendChild(form);
293
- const cleanup = enhance(form, { action: "?/create" });
294
- try {
295
- form.dispatchEvent(
296
- new Event("submit", { bubbles: true, cancelable: true }),
297
- );
298
- await new Promise((r) => setTimeout(r, 0));
299
- expect(calls).toEqual([`${window.location.pathname}?/create`]);
300
- } finally {
301
- cleanup();
302
- form.remove();
303
- globalThis.fetch = original;
304
- }
305
- });
306
- });
307
-
308
- describe("Page.load — parent()", () => {
309
- test("parent() merges layoutData entries outermost-first", async () => {
310
- let seenParent: Record<string, unknown> | undefined;
311
- const PageWithLoad = Object.assign(() => document.createElement("div"), {
312
- load: async (ctx: { parent: () => Promise<Record<string, unknown>> }) => {
313
- seenParent = await ctx.parent();
314
- return {};
315
- },
316
- });
317
-
318
- const original = globalThis.fetch;
319
- globalThis.fetch = (async (input: string) => {
320
- const pattern = new URL(input, "http://localhost").pathname;
321
- return new Response(
322
- JSON.stringify({
323
- data: {},
324
- layoutData: [{ a: 1 }, { b: 2 }],
325
- params: {},
326
- pattern,
327
- head: "",
328
- }),
329
- { headers: { "content-type": "application/json" } },
330
- );
331
- }) as unknown as typeof fetch;
332
-
333
- try {
334
- router.initRouter({ "/pl": PageWithLoad }, []);
335
- await router.navigate("/pl");
336
- expect(seenParent).toEqual({ a: 1, b: 2 });
337
- } finally {
338
- globalThis.fetch = original;
339
- }
340
- });
341
- });
342
-
343
- describe("invalidate", () => {
344
- test("refetches only on key intersection; empty deps always refetch", async () => {
345
- router.initRouter({ "/": () => document.createElement("div") }, []);
346
- const shim = captureFetch();
347
- try {
348
- await router.navigate("/"); // establishes lastState.pattern = "/"
349
-
350
- setDeps("/", ["user:1"]);
351
- shim.calls.length = 0;
352
- await router.invalidate("other");
353
- expect(shim.calls.length).toBe(0); // no intersection → skip
354
-
355
- await router.invalidate("user:1");
356
- expect(shim.calls.length).toBe(1); // intersect → refetch
357
-
358
- clearDeps("/");
359
- await router.invalidate("anything");
360
- expect(shim.calls.length).toBe(2); // no deps → conservative refetch
361
- } finally {
362
- shim.restore();
363
- clearDeps("/");
364
- }
365
- });
366
- });
1
+ import { beforeEach, describe, expect, test, vi } from "bun:test";
2
+ import type { Document, History, Location, Window } from "happy-dom";
3
+ import {
4
+ clearDeps,
5
+ depends,
6
+ hasDep,
7
+ setDeps,
8
+ startCollecting,
9
+ stopCollecting,
10
+ } from "../src/client/deps.ts";
11
+
12
+ // test/preload.ts installs happy-dom globals at runtime; this package's
13
+ // compiler options have no DOM lib, so declare the shapes we rely on.
14
+ declare global {
15
+ // biome-ignore-all lint/style/noVar: ambient global declarations require var
16
+ var window: Window;
17
+ var document: Document;
18
+ var location: Location;
19
+ var history: History;
20
+ var MouseEvent: Window["MouseEvent"];
21
+ var Event: Window["Event"];
22
+ var requestAnimationFrame: Window["requestAnimationFrame"];
23
+ }
24
+
25
+ // router.ts reads `location` at module load — DOM globals must exist first.
26
+ // Dynamic import is required because the module under test captures location
27
+ // during evaluation, which static-hoisted imports would race. The default
28
+ // about:blank URL has origin "null", which breaks new URL(href, origin) —
29
+ // point the window at a real origin first.
30
+ window.location.href = "http://localhost/";
31
+ const globalEnv = globalThis as typeof globalThis & {
32
+ location: Location;
33
+ history: History;
34
+ requestAnimationFrame: Window["requestAnimationFrame"];
35
+ };
36
+ globalEnv.location = window.location;
37
+ globalEnv.history = window.history;
38
+ globalEnv.requestAnimationFrame = window.requestAnimationFrame.bind(window);
39
+ const router = await import("../src/client/router.ts");
40
+
41
+ function jsonResponse(pattern: string): Response {
42
+ return new Response(
43
+ JSON.stringify({
44
+ data: {},
45
+ layoutData: [],
46
+ params: {},
47
+ pattern,
48
+ head: "",
49
+ }),
50
+ { headers: { "content-type": "application/json" } },
51
+ );
52
+ }
53
+
54
+ function captureFetch(): {
55
+ calls: Array<{ input: string; init?: RequestInit }>;
56
+ restore: () => void;
57
+ } {
58
+ const calls: Array<{ input: string; init?: RequestInit }> = [];
59
+ const original = globalThis.fetch;
60
+ // test shim over the global — precise fetch typing is impossible here.
61
+ // Echoes the requested pathname as the route pattern, mirroring renderRoute.
62
+ globalThis.fetch = (async (
63
+ input: string,
64
+ init?: RequestInit,
65
+ ): Promise<Response> => {
66
+ calls.push({ input, init });
67
+ const pattern = new URL(input, "http://localhost").pathname;
68
+ return jsonResponse(pattern);
69
+ }) as unknown as typeof fetch;
70
+ return {
71
+ calls,
72
+ restore: () => {
73
+ globalThis.fetch = original;
74
+ },
75
+ };
76
+ }
77
+
78
+ describe("depends", () => {
79
+ test("no-op outside collection", () => {
80
+ depends("posts");
81
+ expect(hasDep("/", "posts")).toBe(false);
82
+ });
83
+
84
+ test("registers key during collection", () => {
85
+ startCollecting();
86
+ depends("posts");
87
+ stopCollecting("/");
88
+ expect(hasDep("/", "posts")).toBe(true);
89
+ });
90
+
91
+ test("multiple keys", () => {
92
+ startCollecting();
93
+ depends("posts");
94
+ depends("tags");
95
+ stopCollecting("/");
96
+ expect(hasDep("/", "posts")).toBe(true);
97
+ expect(hasDep("/", "tags")).toBe(true);
98
+ });
99
+
100
+ test("cleared on next collection", () => {
101
+ startCollecting();
102
+ depends("posts");
103
+ stopCollecting("/");
104
+ startCollecting();
105
+ depends("comments");
106
+ stopCollecting("/");
107
+ expect(hasDep("/", "posts")).toBe(false);
108
+ expect(hasDep("/", "comments")).toBe(true);
109
+ });
110
+ });
111
+ describe("navigate — view transitions", () => {
112
+ test("falls back to direct swap when startViewTransition is missing", async () => {
113
+ const root = document.createElement("div");
114
+ root.id = "grimoire-root";
115
+ document.body.appendChild(root);
116
+ const fetchShim = captureFetch();
117
+ try {
118
+ router.initRouter({ "/": () => document.createElement("main") }, []);
119
+ await router.navigate("/");
120
+ expect(root.childNodes.length).toBeGreaterThan(0);
121
+ } finally {
122
+ fetchShim.restore();
123
+ root.remove();
124
+ }
125
+ });
126
+
127
+ test("routes the swap through startViewTransition when available", async () => {
128
+ const root = document.createElement("div");
129
+ root.id = "grimoire-root";
130
+ document.body.appendChild(root);
131
+ let vtCalled = false;
132
+ const docEnv = document as Document & {
133
+ startViewTransition?: (cb: () => void) => unknown;
134
+ };
135
+ docEnv.startViewTransition = (cb: () => void) => {
136
+ vtCalled = true;
137
+ cb();
138
+ return undefined;
139
+ };
140
+ const fetchShim = captureFetch();
141
+ try {
142
+ router.initRouter({ "/vt": () => document.createElement("main") }, []);
143
+ await router.navigate("/vt");
144
+ expect(vtCalled).toBe(true);
145
+ expect(root.childNodes.length).toBeGreaterThan(0);
146
+ } finally {
147
+ delete docEnv.startViewTransition;
148
+ fetchShim.restore();
149
+ root.remove();
150
+ }
151
+ });
152
+ });
153
+
154
+ describe("hover prefetch", () => {
155
+ // The 80ms debounce is real timer logic under test — fake timers advance
156
+ // it deterministically instead of sleeping past it.
157
+ test("plain internal link prefetches route data; preload=off skips", () => {
158
+ vi.useFakeTimers();
159
+ router.initRouter(
160
+ {
161
+ "/": () => document.createElement("div"),
162
+ "/off": () => document.createElement("div"),
163
+ },
164
+ [],
165
+ );
166
+ const fetchShim = captureFetch();
167
+ try {
168
+ const plain = document.createElement("a");
169
+ plain.setAttribute("href", "/");
170
+ document.body.appendChild(plain);
171
+ plain.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
172
+ vi.advanceTimersByTime(80);
173
+ expect(fetchShim.calls.length).toBe(1);
174
+ expect(fetchShim.calls[0]?.init?.headers).toEqual({
175
+ "x-grimoire-navigate": "1",
176
+ });
177
+
178
+ const optedOut = document.createElement("a");
179
+ optedOut.setAttribute("href", "/off");
180
+ optedOut.setAttribute("data-sigil-preload", "off");
181
+ document.body.appendChild(optedOut);
182
+ optedOut.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
183
+ vi.advanceTimersByTime(80);
184
+ expect(fetchShim.calls.length).toBe(1); // unchanged
185
+ } finally {
186
+ vi.useRealTimers();
187
+ fetchShim.restore();
188
+ }
189
+ });
190
+
191
+ test("external links are never prefetched", () => {
192
+ vi.useFakeTimers();
193
+ const fetchShim = captureFetch();
194
+ try {
195
+ const ext = document.createElement("a");
196
+ ext.setAttribute("href", "https://example.com/page");
197
+ document.body.appendChild(ext);
198
+ ext.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
199
+ vi.advanceTimersByTime(80);
200
+ expect(fetchShim.calls.length).toBe(0);
201
+ } finally {
202
+ vi.useRealTimers();
203
+ fetchShim.restore();
204
+ }
205
+ });
206
+ });
207
+
208
+ describe("popstate — query string", () => {
209
+ // popstate fires navigate() from an event listener, so there is no promise
210
+ // to await at the call site. afterNavigate is the router's own completion
211
+ // signal; a fixed sleep would be both slower and racy.
212
+ function onNavigated(): Promise<void> {
213
+ const { promise, resolve } = Promise.withResolvers<void>();
214
+ router.afterNavigate(() => resolve());
215
+ return promise;
216
+ }
217
+
218
+ // Back used to refetch the bare pathname, so a query-driven page rendered
219
+ // unfiltered data while the address bar still showed the query.
220
+ test("back to a query URL refetches with the query intact", async () => {
221
+ const root = document.createElement("div");
222
+ root.id = "grimoire-root";
223
+ document.body.appendChild(root);
224
+ window.history.pushState(null, "", "/");
225
+ router.initRouter(
226
+ {
227
+ "/list": () => document.createElement("div"),
228
+ "/other": () => document.createElement("div"),
229
+ },
230
+ [],
231
+ );
232
+ const shim = captureFetch();
233
+ try {
234
+ await router.navigate("/list?q=fireball&page=2");
235
+ await router.navigate("/other");
236
+ shim.calls.length = 0;
237
+
238
+ // the browser moves history itself, then fires popstate
239
+ const navigated = onNavigated();
240
+ window.history.pushState(null, "", "/list?q=fireball&page=2");
241
+ window.dispatchEvent(new Event("popstate"));
242
+ await navigated;
243
+
244
+ expect(shim.calls.map((c) => c.input)).toEqual([
245
+ "/list?q=fireball&page=2",
246
+ ]);
247
+ } finally {
248
+ router.afterNavigate(() => {});
249
+ shim.restore();
250
+ root.remove();
251
+ }
252
+ });
253
+
254
+ test("a popstate that only changes the query still refetches", async () => {
255
+ const root = document.createElement("div");
256
+ root.id = "grimoire-root";
257
+ document.body.appendChild(root);
258
+ window.history.pushState(null, "", "/");
259
+ router.initRouter({ "/list": () => document.createElement("div") }, []);
260
+ const shim = captureFetch();
261
+ try {
262
+ await router.navigate("/list?page=2");
263
+ shim.calls.length = 0;
264
+
265
+ const navigated = onNavigated();
266
+ window.history.pushState(null, "", "/list?page=1");
267
+ window.dispatchEvent(new Event("popstate"));
268
+ await navigated;
269
+
270
+ expect(shim.calls.map((c) => c.input)).toEqual(["/list?page=1"]);
271
+ } finally {
272
+ router.afterNavigate(() => {});
273
+ shim.restore();
274
+ root.remove();
275
+ }
276
+ });
277
+ });
278
+
279
+ describe("enhance — named action URLs", () => {
280
+ test("action starting with ?/ submits to the current pathname", async () => {
281
+ // earlier navigate() tests moved history — reset to a clean path
282
+ window.history.pushState(null, "", "/");
283
+ const { enhance } = await import("../src/client/enhance.ts");
284
+ const calls: string[] = [];
285
+ const original = globalThis.fetch;
286
+ globalThis.fetch = (async (input: string) => {
287
+ calls.push(input);
288
+ return new Response(null, { status: 303, headers: { location: "/" } });
289
+ }) as unknown as typeof fetch;
290
+
291
+ const form = document.createElement("form");
292
+ document.body.appendChild(form);
293
+ const cleanup = enhance(form, { action: "?/create" });
294
+ try {
295
+ form.dispatchEvent(
296
+ new Event("submit", { bubbles: true, cancelable: true }),
297
+ );
298
+ await new Promise((r) => setTimeout(r, 0));
299
+ expect(calls).toEqual([`${window.location.pathname}?/create`]);
300
+ } finally {
301
+ cleanup();
302
+ form.remove();
303
+ globalThis.fetch = original;
304
+ }
305
+ });
306
+ });
307
+
308
+ describe("Page.load — parent()", () => {
309
+ test("parent() merges layoutData entries outermost-first", async () => {
310
+ let seenParent: Record<string, unknown> | undefined;
311
+ const PageWithLoad = Object.assign(() => document.createElement("div"), {
312
+ load: async (ctx: { parent: () => Promise<Record<string, unknown>> }) => {
313
+ seenParent = await ctx.parent();
314
+ return {};
315
+ },
316
+ });
317
+
318
+ const original = globalThis.fetch;
319
+ globalThis.fetch = (async (input: string) => {
320
+ const pattern = new URL(input, "http://localhost").pathname;
321
+ return new Response(
322
+ JSON.stringify({
323
+ data: {},
324
+ layoutData: [{ a: 1 }, { b: 2 }],
325
+ params: {},
326
+ pattern,
327
+ head: "",
328
+ }),
329
+ { headers: { "content-type": "application/json" } },
330
+ );
331
+ }) as unknown as typeof fetch;
332
+
333
+ try {
334
+ router.initRouter({ "/pl": PageWithLoad }, []);
335
+ await router.navigate("/pl");
336
+ expect(seenParent).toEqual({ a: 1, b: 2 });
337
+ } finally {
338
+ globalThis.fetch = original;
339
+ }
340
+ });
341
+ });
342
+
343
+ describe("invalidate", () => {
344
+ test("refetches only on key intersection; empty deps always refetch", async () => {
345
+ router.initRouter({ "/": () => document.createElement("div") }, []);
346
+ const shim = captureFetch();
347
+ try {
348
+ await router.navigate("/"); // establishes lastState.pattern = "/"
349
+
350
+ setDeps("/", ["user:1"]);
351
+ shim.calls.length = 0;
352
+ await router.invalidate("other");
353
+ expect(shim.calls.length).toBe(0); // no intersection → skip
354
+
355
+ await router.invalidate("user:1");
356
+ expect(shim.calls.length).toBe(1); // intersect → refetch
357
+
358
+ clearDeps("/");
359
+ await router.invalidate("anything");
360
+ expect(shim.calls.length).toBe(2); // no deps → conservative refetch
361
+ } finally {
362
+ shim.restore();
363
+ clearDeps("/");
364
+ }
365
+ });
366
+ });