Skip to content

Commit ad5429d

Browse files
committed
allow pre-populated context for static handler
1 parent 5bac175 commit ad5429d

File tree

5 files changed

+185
-74
lines changed

5 files changed

+185
-74
lines changed

packages/router/__tests__/navigation-blocking-test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ const routes = [
1414

1515
describe("navigation blocking", () => {
1616
let router: Router;
17+
let warnSpy;
18+
19+
beforeEach(() => {
20+
warnSpy = jest.spyOn(console, "warn");
21+
});
22+
23+
afterEach(() => {
24+
warnSpy.mockReset();
25+
});
26+
1727
it("initializes an 'unblocked' blocker", () => {
1828
router = createRouter({
1929
history: createMemoryHistory({

packages/router/__tests__/router-test.ts

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import type {
4040
} from "../utils";
4141
import {
4242
AbortedDeferredError,
43+
createMiddlewareStore,
4344
isRouteErrorResponse,
4445
stripBasename,
4546
} from "../utils";
@@ -12234,23 +12235,23 @@ describe("a router", () => {
1223412235
await currentRouter.navigate("/parent?get");
1223512236
expect(currentRouter.state.errors).toMatchInlineSnapshot(`
1223612237
{
12237-
"parent": [Error: Middleware not enabled (\`future.unstable_middleware\`)],
12238+
"parent": [Error: Middleware must be enabled via the \`future.unstable_middleware\` flag)],
1223812239
}
1223912240
`);
1224012241

1224112242
await currentRouter.navigate("/");
1224212243
await currentRouter.navigate("/parent?set");
1224312244
expect(currentRouter.state.errors).toMatchInlineSnapshot(`
1224412245
{
12245-
"parent": [Error: Middleware not enabled (\`future.unstable_middleware\`)],
12246+
"parent": [Error: Middleware must be enabled via the \`future.unstable_middleware\` flag)],
1224612247
}
1224712248
`);
1224812249

1224912250
await currentRouter.navigate("/");
1225012251
await currentRouter.navigate("/parent?next");
1225112252
expect(currentRouter.state.errors).toMatchInlineSnapshot(`
1225212253
{
12253-
"parent": [Error: Middleware not enabled (\`future.unstable_middleware\`)],
12254+
"parent": [Error: Middleware must be enabled via the \`future.unstable_middleware\` flag)],
1225412255
}
1225512256
`);
1225612257
});
@@ -12382,6 +12383,70 @@ describe("a router", () => {
1238212383
});
1238312384
});
1238412385

12386+
it("passes context into staticHandler.query", async () => {
12387+
let { query } = createStaticHandler(MIDDLEWARE_CONTEXT_ROUTES, {
12388+
future: { unstable_middleware: true },
12389+
});
12390+
12391+
let ctx = await query(createRequest("/parent/child/grandchild"));
12392+
12393+
if (ctx instanceof Response) {
12394+
throw new Error("Unexpected Response");
12395+
}
12396+
12397+
expect(ctx.location.pathname).toBe("/parent/child/grandchild");
12398+
expect(ctx.loaderData).toEqual({
12399+
parent: 1,
12400+
child: 2,
12401+
grandchild: 3,
12402+
});
12403+
});
12404+
12405+
it("passes context into staticHandler.queryRoute", async () => {
12406+
let { queryRoute } = createStaticHandler(MIDDLEWARE_CONTEXT_ROUTES, {
12407+
future: { unstable_middleware: true },
12408+
});
12409+
12410+
let res = await queryRoute(createRequest("/parent/child/grandchild"));
12411+
expect(res).toBe(3);
12412+
});
12413+
12414+
it("prefills context in staticHandler.query", async () => {
12415+
let { query } = createStaticHandler(MIDDLEWARE_CONTEXT_ROUTES, {
12416+
future: { unstable_middleware: true },
12417+
});
12418+
12419+
let middlewareContext = createMiddlewareStore();
12420+
middlewareContext.set(loaderCountContext, 50);
12421+
let ctx = await query(createRequest("/parent/child/grandchild"), {
12422+
middlewareContext,
12423+
});
12424+
12425+
if (ctx instanceof Response) {
12426+
throw new Error("Unexpected Response");
12427+
}
12428+
12429+
expect(ctx.location.pathname).toBe("/parent/child/grandchild");
12430+
expect(ctx.loaderData).toEqual({
12431+
parent: 51,
12432+
child: 52,
12433+
grandchild: 53,
12434+
});
12435+
});
12436+
12437+
it("prefills context in staticHandler.queryRoute", async () => {
12438+
let { queryRoute } = createStaticHandler(MIDDLEWARE_CONTEXT_ROUTES, {
12439+
future: { unstable_middleware: true },
12440+
});
12441+
12442+
let middlewareContext = createMiddlewareStore();
12443+
middlewareContext.set(loaderCountContext, 50);
12444+
let res = await queryRoute(createRequest("/parent/child/grandchild"), {
12445+
middlewareContext,
12446+
});
12447+
expect(res).toBe(53);
12448+
});
12449+
1238512450
it("throws if no value is available via context.get()", async () => {
1238612451
let theContext = createMiddlewareContext<number>();
1238712452

packages/router/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@ export {
8989
DeferredData as UNSAFE_DeferredData,
9090
convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes,
9191
getPathContributingMatches as UNSAFE_getPathContributingMatches,
92+
createMiddlewareStore as UNSAFE_createMiddlewareStore,
9293
} from "./utils";

0 commit comments

Comments
 (0)