Skip to content

Commit

Permalink
Add tests for SSR + route.lazy (#11323)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Mar 4, 2024
1 parent 8da1a23 commit 98e7f7b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
75 changes: 74 additions & 1 deletion packages/router/__tests__/ssr-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import {
} from "../utils";
import { deferredData, trackedPromise } from "./utils/custom-matchers";
import { createDeferred } from "./utils/data-router-setup";
import { createRequest, createSubmitRequest, invariant } from "./utils/utils";
import {
createRequest,
createSubmitRequest,
invariant,
sleep,
} from "./utils/utils";

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

it("should support route.lazy", async () => {
let { query } = createStaticHandler([
{
id: "root",
path: "/",
async lazy() {
await sleep(100);
return {
async loader() {
await sleep(100);
return "ROOT LOADER";
},
};
},
},
{
id: "parent",
path: "/parent",
async lazy() {
await sleep(100);
return {
async loader() {
await sleep(100);
return "PARENT LOADER";
},
};
},
children: [
{
id: "child",
path: "child",
async lazy() {
await sleep(100);
return {
async loader() {
await sleep(100);
return "CHILD LOADER";
},
};
},
},
],
},
]);

let context = await query(createRequest("/"));
expect(context).toMatchObject({
loaderData: {
root: "ROOT LOADER",
},
errors: null,
location: { pathname: "/" },
matches: [{ route: { id: "root" } }],
});

context = await query(createRequest("/parent/child"));
expect(context).toMatchObject({
actionData: null,
loaderData: {
parent: "PARENT LOADER",
child: "CHILD LOADER",
},
errors: null,
location: { pathname: "/parent/child" },
matches: [{ route: { id: "parent" } }, { route: { id: "child" } }],
});
});

it("should support document submit navigations", async () => {
let { query } = createStaticHandler(SSR_ROUTES);
let context = await query(createSubmitRequest("/parent/child"));
Expand Down
6 changes: 5 additions & 1 deletion packages/router/__tests__/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { AgnosticDataRouteObject } from "../../utils";

export async function sleep(n: number) {
await new Promise((r) => setTimeout(r, n));
}

export async function tick() {
await new Promise((r) => setTimeout(r, 0));
await sleep(0);
}

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

0 comments on commit 98e7f7b

Please sign in to comment.