Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for SSR + route.lazy #11323

Merged
merged 1 commit into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 74 additions & 1 deletion packages/router/__tests__/ssr-test.ts
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
@@ -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