@@ -41,6 +41,7 @@ import type {
41
41
import {
42
42
AbortedDeferredError ,
43
43
createMiddlewareStore ,
44
+ getRouteAwareMiddlewareContext ,
44
45
isRouteErrorResponse ,
45
46
stripBasename ,
46
47
} from "../utils" ;
@@ -11954,13 +11955,21 @@ describe("a router", () => {
11954
11955
future : { unstable_middleware : true } ,
11955
11956
} ) ;
11956
11957
11957
- let context = await query ( createRequest ( "/parent/child/grandchild" ) ) ;
11958
+ let context = await query ( createRequest ( "/parent/child/grandchild" ) , {
11959
+ render : ( context ) => {
11960
+ invariant (
11961
+ ! ( context instanceof Response ) ,
11962
+ "Expected StaticHandlerContext"
11963
+ ) ;
11964
+ return Promise . resolve ( json ( context . loaderData ) ) ;
11965
+ } ,
11966
+ } ) ;
11958
11967
11959
11968
invariant (
11960
- ! ( context instanceof Response ) ,
11961
- "Expected StaticHandlerContext "
11969
+ context instanceof Response ,
11970
+ "Expected Response from query() with render() "
11962
11971
) ;
11963
- expect ( context . loaderData ) . toMatchInlineSnapshot ( `
11972
+ expect ( await context . json ( ) ) . toMatchInlineSnapshot ( `
11964
11973
{
11965
11974
"child": "CHILD LOADER",
11966
11975
"grandchild": "GRANDCHILD LOADER",
@@ -11970,19 +11979,13 @@ describe("a router", () => {
11970
11979
expect ( calls ) . toMatchInlineSnapshot ( `
11971
11980
[
11972
11981
"parent loader middleware start",
11973
- "parent loader middleware start",
11974
- "parent loader middleware start",
11975
- " parent loader start",
11976
11982
" child loader middleware start",
11977
- " child loader middleware start",
11978
- " parent loader end",
11979
- "parent loader middleware end",
11980
- " child loader start",
11981
11983
" grandchild loader middleware start",
11982
- " child loader end",
11983
- " child loader middleware end",
11984
- "parent loader middleware end",
11984
+ " parent loader start",
11985
+ " child loader start",
11985
11986
" grandchild loader start",
11987
+ " parent loader end",
11988
+ " child loader end",
11986
11989
" grandchild loader end",
11987
11990
" grandchild loader middleware end",
11988
11991
" child loader middleware end",
@@ -12094,9 +12097,7 @@ describe("a router", () => {
12094
12097
await currentRouter ?. navigate ( "/parent" ) ;
12095
12098
expect ( currentRouter . state . location . pathname ) . toBe ( "/parent" ) ;
12096
12099
expect ( currentRouter . state . errors ) . toEqual ( {
12097
- parent : new Error (
12098
- "You may only call `next()` once per middleware and you may not call it in an action or loader"
12099
- ) ,
12100
+ parent : new Error ( "You may only call `next()` once per middleware" ) ,
12100
12101
} ) ;
12101
12102
} ) ;
12102
12103
@@ -12127,7 +12128,7 @@ describe("a router", () => {
12127
12128
expect ( currentRouter . state . location . pathname ) . toBe ( "/parent" ) ;
12128
12129
expect ( currentRouter . state . errors ) . toEqual ( {
12129
12130
parent : new Error (
12130
- "You may only call ` next()` once per middleware and you may not call it in an action or loader "
12131
+ "You can not call context. next() in a loader or action "
12131
12132
) ,
12132
12133
} ) ;
12133
12134
} ) ;
@@ -12165,7 +12166,7 @@ describe("a router", () => {
12165
12166
expect ( currentRouter . state . location . pathname ) . toBe ( "/parent" ) ;
12166
12167
expect ( currentRouter . state . errors ) . toEqual ( {
12167
12168
parent : new Error (
12168
- "You may only call ` next()` once per middleware and you may not call it in an action or loader "
12169
+ "You can not call context. next() in a loader or action "
12169
12170
) ,
12170
12171
} ) ;
12171
12172
} ) ;
@@ -12388,14 +12389,17 @@ describe("a router", () => {
12388
12389
future : { unstable_middleware : true } ,
12389
12390
} ) ;
12390
12391
12391
- let ctx = await query ( createRequest ( "/parent/child/grandchild" ) ) ;
12392
+ let ctx = await query ( createRequest ( "/parent/child/grandchild" ) , {
12393
+ render : ( context ) => {
12394
+ return Promise . resolve (
12395
+ json ( ( context as StaticHandlerContext ) . loaderData )
12396
+ ) ;
12397
+ } ,
12398
+ } ) ;
12392
12399
12393
- if ( ctx instanceof Response ) {
12394
- throw new Error ( "Unexpected Response" ) ;
12395
- }
12400
+ invariant ( ctx instanceof Response , "Expected Response" ) ;
12396
12401
12397
- expect ( ctx . location . pathname ) . toBe ( "/parent/child/grandchild" ) ;
12398
- expect ( ctx . loaderData ) . toEqual ( {
12402
+ expect ( await ctx . json ( ) ) . toEqual ( {
12399
12403
parent : 1 ,
12400
12404
child : 2 ,
12401
12405
grandchild : 3 ,
@@ -12417,17 +12421,24 @@ describe("a router", () => {
12417
12421
} ) ;
12418
12422
12419
12423
let middlewareContext = createMiddlewareStore ( ) ;
12420
- middlewareContext . set ( loaderCountContext , 50 ) ;
12424
+ let routeMiddlewareContext = getRouteAwareMiddlewareContext (
12425
+ middlewareContext ,
12426
+ - 1 ,
12427
+ ( ) => { }
12428
+ ) ;
12429
+ routeMiddlewareContext . set ( loaderCountContext , 50 ) ;
12421
12430
let ctx = await query ( createRequest ( "/parent/child/grandchild" ) , {
12422
12431
middlewareContext,
12432
+ render : ( context ) => {
12433
+ return Promise . resolve (
12434
+ json ( ( context as StaticHandlerContext ) . loaderData )
12435
+ ) ;
12436
+ } ,
12423
12437
} ) ;
12424
12438
12425
- if ( ctx instanceof Response ) {
12426
- throw new Error ( "Unexpected Response" ) ;
12427
- }
12439
+ invariant ( ctx instanceof Response , "Expected Response" ) ;
12428
12440
12429
- expect ( ctx . location . pathname ) . toBe ( "/parent/child/grandchild" ) ;
12430
- expect ( ctx . loaderData ) . toEqual ( {
12441
+ expect ( await ctx . json ( ) ) . toEqual ( {
12431
12442
parent : 51 ,
12432
12443
child : 52 ,
12433
12444
grandchild : 53 ,
@@ -12440,7 +12451,12 @@ describe("a router", () => {
12440
12451
} ) ;
12441
12452
12442
12453
let middlewareContext = createMiddlewareStore ( ) ;
12443
- middlewareContext . set ( loaderCountContext , 50 ) ;
12454
+ let routeMiddlewareContext = getRouteAwareMiddlewareContext (
12455
+ middlewareContext ,
12456
+ - 1 ,
12457
+ ( ) => { }
12458
+ ) ;
12459
+ routeMiddlewareContext . set ( loaderCountContext , 50 ) ;
12444
12460
let res = await queryRoute ( createRequest ( "/parent/child/grandchild" ) , {
12445
12461
middlewareContext,
12446
12462
} ) ;
0 commit comments