Skip to content

Commit 98e7f7b

Browse files
authored
Add tests for SSR + route.lazy (#11323)
1 parent 8da1a23 commit 98e7f7b

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

packages/router/__tests__/ssr-test.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import {
1717
} from "../utils";
1818
import { deferredData, trackedPromise } from "./utils/custom-matchers";
1919
import { createDeferred } from "./utils/data-router-setup";
20-
import { createRequest, createSubmitRequest, invariant } from "./utils/utils";
20+
import {
21+
createRequest,
22+
createSubmitRequest,
23+
invariant,
24+
sleep,
25+
} from "./utils/utils";
2126

2227
interface CustomMatchers<R = jest.Expect> {
2328
trackedPromise(data?: any, error?: any, aborted?: boolean): R;
@@ -293,6 +298,74 @@ describe("ssr", () => {
293298
});
294299
});
295300

301+
it("should support route.lazy", async () => {
302+
let { query } = createStaticHandler([
303+
{
304+
id: "root",
305+
path: "/",
306+
async lazy() {
307+
await sleep(100);
308+
return {
309+
async loader() {
310+
await sleep(100);
311+
return "ROOT LOADER";
312+
},
313+
};
314+
},
315+
},
316+
{
317+
id: "parent",
318+
path: "/parent",
319+
async lazy() {
320+
await sleep(100);
321+
return {
322+
async loader() {
323+
await sleep(100);
324+
return "PARENT LOADER";
325+
},
326+
};
327+
},
328+
children: [
329+
{
330+
id: "child",
331+
path: "child",
332+
async lazy() {
333+
await sleep(100);
334+
return {
335+
async loader() {
336+
await sleep(100);
337+
return "CHILD LOADER";
338+
},
339+
};
340+
},
341+
},
342+
],
343+
},
344+
]);
345+
346+
let context = await query(createRequest("/"));
347+
expect(context).toMatchObject({
348+
loaderData: {
349+
root: "ROOT LOADER",
350+
},
351+
errors: null,
352+
location: { pathname: "/" },
353+
matches: [{ route: { id: "root" } }],
354+
});
355+
356+
context = await query(createRequest("/parent/child"));
357+
expect(context).toMatchObject({
358+
actionData: null,
359+
loaderData: {
360+
parent: "PARENT LOADER",
361+
child: "CHILD LOADER",
362+
},
363+
errors: null,
364+
location: { pathname: "/parent/child" },
365+
matches: [{ route: { id: "parent" } }, { route: { id: "child" } }],
366+
});
367+
});
368+
296369
it("should support document submit navigations", async () => {
297370
let { query } = createStaticHandler(SSR_ROUTES);
298371
let context = await query(createSubmitRequest("/parent/child"));

packages/router/__tests__/utils/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { AgnosticDataRouteObject } from "../../utils";
22

3+
export async function sleep(n: number) {
4+
await new Promise((r) => setTimeout(r, n));
5+
}
6+
37
export async function tick() {
4-
await new Promise((r) => setTimeout(r, 0));
8+
await sleep(0);
59
}
610

711
export function invariant(value: boolean, message?: string): asserts value;

0 commit comments

Comments
 (0)