@@ -40,6 +40,7 @@ import type {
40
40
} from "../utils" ;
41
41
import {
42
42
AbortedDeferredError ,
43
+ createMiddlewareStore ,
43
44
isRouteErrorResponse ,
44
45
stripBasename ,
45
46
} from "../utils" ;
@@ -12234,23 +12235,23 @@ describe("a router", () => {
12234
12235
await currentRouter . navigate ( "/parent?get" ) ;
12235
12236
expect ( currentRouter . state . errors ) . toMatchInlineSnapshot ( `
12236
12237
{
12237
- "parent": [Error: Middleware not enabled ( \`future.unstable_middleware\`)],
12238
+ "parent": [Error: Middleware must be enabled via the \`future.unstable_middleware\` flag )],
12238
12239
}
12239
12240
` ) ;
12240
12241
12241
12242
await currentRouter . navigate ( "/" ) ;
12242
12243
await currentRouter . navigate ( "/parent?set" ) ;
12243
12244
expect ( currentRouter . state . errors ) . toMatchInlineSnapshot ( `
12244
12245
{
12245
- "parent": [Error: Middleware not enabled ( \`future.unstable_middleware\`)],
12246
+ "parent": [Error: Middleware must be enabled via the \`future.unstable_middleware\` flag )],
12246
12247
}
12247
12248
` ) ;
12248
12249
12249
12250
await currentRouter . navigate ( "/" ) ;
12250
12251
await currentRouter . navigate ( "/parent?next" ) ;
12251
12252
expect ( currentRouter . state . errors ) . toMatchInlineSnapshot ( `
12252
12253
{
12253
- "parent": [Error: Middleware not enabled ( \`future.unstable_middleware\`)],
12254
+ "parent": [Error: Middleware must be enabled via the \`future.unstable_middleware\` flag )],
12254
12255
}
12255
12256
` ) ;
12256
12257
} ) ;
@@ -12382,6 +12383,70 @@ describe("a router", () => {
12382
12383
} ) ;
12383
12384
} ) ;
12384
12385
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
+
12385
12450
it ( "throws if no value is available via context.get()" , async ( ) => {
12386
12451
let theContext = createMiddlewareContext < number > ( ) ;
12387
12452
0 commit comments