From 1b079acab99a9463b6cbbe583beb76711b2fbf8b Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 4 Mar 2024 10:23:05 -0500 Subject: [PATCH] Add tests for SSR + route.lazy --- packages/router/__tests__/ssr-test.ts | 75 +++++++++++++++++++++++- packages/router/__tests__/utils/utils.ts | 6 +- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/packages/router/__tests__/ssr-test.ts b/packages/router/__tests__/ssr-test.ts index c69cfbf859..daea5eb978 100644 --- a/packages/router/__tests__/ssr-test.ts +++ b/packages/router/__tests__/ssr-test.ts @@ -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 { trackedPromise(data?: any, error?: any, aborted?: boolean): R; @@ -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")); diff --git a/packages/router/__tests__/utils/utils.ts b/packages/router/__tests__/utils/utils.ts index bc123e9819..ff7ce69859 100644 --- a/packages/router/__tests__/utils/utils.ts +++ b/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;