@@ -11626,6 +11626,7 @@ describe("a router", () => {
11626
11626
currentRouter = createRouter ( {
11627
11627
routes : MIDDLEWARE_ORDERING_ROUTES ,
11628
11628
history : createMemoryHistory ( ) ,
11629
+ future : { unstable_middleware : true } ,
11629
11630
} ) . initialize ( ) ;
11630
11631
11631
11632
await currentRouter . navigate ( "/parent" ) ;
@@ -11648,6 +11649,7 @@ describe("a router", () => {
11648
11649
currentRouter = createRouter ( {
11649
11650
routes : MIDDLEWARE_ORDERING_ROUTES ,
11650
11651
history : createMemoryHistory ( ) ,
11652
+ future : { unstable_middleware : true } ,
11651
11653
} ) . initialize ( ) ;
11652
11654
11653
11655
await currentRouter . navigate ( "/parent" , {
@@ -11680,6 +11682,7 @@ describe("a router", () => {
11680
11682
currentRouter = createRouter ( {
11681
11683
routes : MIDDLEWARE_ORDERING_ROUTES ,
11682
11684
history : createMemoryHistory ( ) ,
11685
+ future : { unstable_middleware : true } ,
11683
11686
} ) . initialize ( ) ;
11684
11687
11685
11688
await currentRouter . navigate ( "/parent/child/grandchild" ) ;
@@ -11727,6 +11730,7 @@ describe("a router", () => {
11727
11730
currentRouter = createRouter ( {
11728
11731
routes : MIDDLEWARE_ORDERING_ROUTES ,
11729
11732
history : createMemoryHistory ( ) ,
11733
+ future : { unstable_middleware : true } ,
11730
11734
} ) . initialize ( ) ;
11731
11735
11732
11736
await currentRouter . navigate ( "/parent/child/grandchild" , {
@@ -11791,6 +11795,7 @@ describe("a router", () => {
11791
11795
currentRouter = createRouter ( {
11792
11796
routes : MIDDLEWARE_ORDERING_ROUTES ,
11793
11797
history : createMemoryHistory ( ) ,
11798
+ future : { unstable_middleware : true } ,
11794
11799
} ) . initialize ( ) ;
11795
11800
11796
11801
await currentRouter . fetch (
@@ -11817,6 +11822,7 @@ describe("a router", () => {
11817
11822
currentRouter = createRouter ( {
11818
11823
routes : MIDDLEWARE_ORDERING_ROUTES ,
11819
11824
history : createMemoryHistory ( ) ,
11825
+ future : { unstable_middleware : true } ,
11820
11826
} ) . initialize ( ) ;
11821
11827
11822
11828
await currentRouter . fetch (
@@ -11847,6 +11853,7 @@ describe("a router", () => {
11847
11853
currentRouter = createRouter ( {
11848
11854
routes : MIDDLEWARE_ORDERING_ROUTES ,
11849
11855
history : createMemoryHistory ( ) ,
11856
+ future : { unstable_middleware : true } ,
11850
11857
} ) . initialize ( ) ;
11851
11858
11852
11859
await currentRouter . navigate ( "/parent/child/grandchild" ) ;
@@ -11901,6 +11908,7 @@ describe("a router", () => {
11901
11908
currentRouter = createRouter ( {
11902
11909
routes : MIDDLEWARE_ORDERING_ROUTES ,
11903
11910
history : createMemoryHistory ( ) ,
11911
+ future : { unstable_middleware : true } ,
11904
11912
} ) . initialize ( ) ;
11905
11913
11906
11914
await currentRouter . navigate ( "/parent" ) ;
@@ -11941,7 +11949,9 @@ describe("a router", () => {
11941
11949
} ) ;
11942
11950
11943
11951
it ( "runs middleware before staticHandler.query" , async ( ) => {
11944
- let { query } = createStaticHandler ( MIDDLEWARE_ORDERING_ROUTES ) ;
11952
+ let { query } = createStaticHandler ( MIDDLEWARE_ORDERING_ROUTES , {
11953
+ future : { unstable_middleware : true } ,
11954
+ } ) ;
11945
11955
11946
11956
let context = await query ( createRequest ( "/parent/child/grandchild" ) ) ;
11947
11957
@@ -11981,7 +11991,9 @@ describe("a router", () => {
11981
11991
} ) ;
11982
11992
11983
11993
it ( "runs middleware before staticHandler.queryRoute" , async ( ) => {
11984
- let { queryRoute } = createStaticHandler ( MIDDLEWARE_ORDERING_ROUTES ) ;
11994
+ let { queryRoute } = createStaticHandler ( MIDDLEWARE_ORDERING_ROUTES , {
11995
+ future : { unstable_middleware : true } ,
11996
+ } ) ;
11985
11997
11986
11998
let result = await queryRoute (
11987
11999
createRequest ( "/parent/child/grandchild" )
@@ -12035,6 +12047,7 @@ describe("a router", () => {
12035
12047
} ,
12036
12048
] ,
12037
12049
history : createMemoryHistory ( ) ,
12050
+ future : { unstable_middleware : true } ,
12038
12051
} ) . initialize ( ) ;
12039
12052
12040
12053
await currentRouter . navigate ( "/parent/child" ) ;
@@ -12074,6 +12087,7 @@ describe("a router", () => {
12074
12087
} ,
12075
12088
] ,
12076
12089
history : createMemoryHistory ( ) ,
12090
+ future : { unstable_middleware : true } ,
12077
12091
} ) . initialize ( ) ;
12078
12092
12079
12093
await currentRouter ?. navigate ( "/parent" ) ;
@@ -12105,6 +12119,7 @@ describe("a router", () => {
12105
12119
} ,
12106
12120
] ,
12107
12121
history : createMemoryHistory ( ) ,
12122
+ future : { unstable_middleware : true } ,
12108
12123
} ) . initialize ( ) ;
12109
12124
12110
12125
await currentRouter ?. navigate ( "/parent" ) ;
@@ -12139,6 +12154,7 @@ describe("a router", () => {
12139
12154
} ,
12140
12155
] ,
12141
12156
history : createMemoryHistory ( ) ,
12157
+ future : { unstable_middleware : true } ,
12142
12158
} ) . initialize ( ) ;
12143
12159
12144
12160
await currentRouter ?. navigate ( "/parent" , {
@@ -12152,6 +12168,92 @@ describe("a router", () => {
12152
12168
) ,
12153
12169
} ) ;
12154
12170
} ) ;
12171
+
12172
+ it ( "does not run middleware if flag is not enabled" , async ( ) => {
12173
+ currentRouter = createRouter ( {
12174
+ routes : [
12175
+ {
12176
+ id : "root" ,
12177
+ path : "/" ,
12178
+ } ,
12179
+ {
12180
+ id : "parent" ,
12181
+ path : "/parent" ,
12182
+ middleware ( ) {
12183
+ throw new Error ( "Nope!" ) ;
12184
+ } ,
12185
+ loader ( ) {
12186
+ calls . push ( "parent loader" ) ;
12187
+ return "PARENT LOADER" ;
12188
+ } ,
12189
+ } ,
12190
+ ] ,
12191
+ history : createMemoryHistory ( ) ,
12192
+ } ) . initialize ( ) ;
12193
+
12194
+ await currentRouter . navigate ( "/parent" ) ;
12195
+
12196
+ expect ( currentRouter . state . location . pathname ) . toBe ( "/parent" ) ;
12197
+ expect ( currentRouter . state . loaderData ) . toEqual ( {
12198
+ parent : "PARENT LOADER" ,
12199
+ } ) ;
12200
+ expect ( calls ) . toMatchInlineSnapshot ( `
12201
+ [
12202
+ "parent loader",
12203
+ ]
12204
+ ` ) ;
12205
+ } ) ;
12206
+
12207
+ it ( "throws if middleware get methods are called when flag is not enabled" , async ( ) => {
12208
+ currentRouter = createRouter ( {
12209
+ routes : [
12210
+ {
12211
+ id : "root" ,
12212
+ path : "/" ,
12213
+ } ,
12214
+ {
12215
+ id : "parent" ,
12216
+ path : "/parent" ,
12217
+ loader ( { request, middleware } ) {
12218
+ let sp = new URL ( request . url ) . searchParams ;
12219
+ if ( sp . has ( "get" ) ) {
12220
+ middleware . get ( createMiddlewareContext ( 0 ) ) ;
12221
+ } else if ( sp . has ( "set" ) ) {
12222
+ middleware . set ( createMiddlewareContext ( 0 ) , 1 ) ;
12223
+ } else if ( sp . has ( "next" ) ) {
12224
+ middleware . next ( ) ;
12225
+ }
12226
+
12227
+ return "PARENT LOADER" ;
12228
+ } ,
12229
+ } ,
12230
+ ] ,
12231
+ history : createMemoryHistory ( ) ,
12232
+ } ) . initialize ( ) ;
12233
+
12234
+ await currentRouter . navigate ( "/parent?get" ) ;
12235
+ expect ( currentRouter . state . errors ) . toMatchInlineSnapshot ( `
12236
+ {
12237
+ "parent": [Error: Middleware not enabled (\`future.unstable_middleware\`)],
12238
+ }
12239
+ ` ) ;
12240
+
12241
+ await currentRouter . navigate ( "/" ) ;
12242
+ await currentRouter . navigate ( "/parent?set" ) ;
12243
+ expect ( currentRouter . state . errors ) . toMatchInlineSnapshot ( `
12244
+ {
12245
+ "parent": [Error: Middleware not enabled (\`future.unstable_middleware\`)],
12246
+ }
12247
+ ` ) ;
12248
+
12249
+ await currentRouter . navigate ( "/" ) ;
12250
+ await currentRouter . navigate ( "/parent?next" ) ;
12251
+ expect ( currentRouter . state . errors ) . toMatchInlineSnapshot ( `
12252
+ {
12253
+ "parent": [Error: Middleware not enabled (\`future.unstable_middleware\`)],
12254
+ }
12255
+ ` ) ;
12256
+ } ) ;
12155
12257
} ) ;
12156
12258
12157
12259
describe ( "middleware context" , ( ) => {
@@ -12213,6 +12315,7 @@ describe("a router", () => {
12213
12315
currentRouter = createRouter ( {
12214
12316
routes : MIDDLEWARE_CONTEXT_ROUTES ,
12215
12317
history : createMemoryHistory ( ) ,
12318
+ future : { unstable_middleware : true } ,
12216
12319
} ) . initialize ( ) ;
12217
12320
12218
12321
await currentRouter . navigate ( "/parent/child/grandchild" ) ;
@@ -12231,6 +12334,7 @@ describe("a router", () => {
12231
12334
currentRouter = createRouter ( {
12232
12335
routes : MIDDLEWARE_CONTEXT_ROUTES ,
12233
12336
history : createMemoryHistory ( ) ,
12337
+ future : { unstable_middleware : true } ,
12234
12338
} ) . initialize ( ) ;
12235
12339
12236
12340
await currentRouter . navigate ( "/parent/child/grandchild" , {
@@ -12255,6 +12359,7 @@ describe("a router", () => {
12255
12359
currentRouter = createRouter ( {
12256
12360
routes : MIDDLEWARE_CONTEXT_ROUTES ,
12257
12361
history : createMemoryHistory ( ) ,
12362
+ future : { unstable_middleware : true } ,
12258
12363
} ) . initialize ( ) ;
12259
12364
12260
12365
await currentRouter . fetch ( "key" , "root" , "/parent/child/grandchild" ) ;
@@ -12269,6 +12374,7 @@ describe("a router", () => {
12269
12374
currentRouter = createRouter ( {
12270
12375
routes : MIDDLEWARE_CONTEXT_ROUTES ,
12271
12376
history : createMemoryHistory ( ) ,
12377
+ future : { unstable_middleware : true } ,
12272
12378
} ) . initialize ( ) ;
12273
12379
12274
12380
await currentRouter . fetch ( "key" , "root" , "/parent/child/grandchild" , {
@@ -12299,6 +12405,7 @@ describe("a router", () => {
12299
12405
} ,
12300
12406
] ,
12301
12407
history : createMemoryHistory ( ) ,
12408
+ future : { unstable_middleware : true } ,
12302
12409
} ) . initialize ( ) ;
12303
12410
12304
12411
await currentRouter . navigate ( "/broken" ) ;
@@ -12331,6 +12438,7 @@ describe("a router", () => {
12331
12438
} ,
12332
12439
] ,
12333
12440
history : createMemoryHistory ( ) ,
12441
+ future : { unstable_middleware : true } ,
12334
12442
} ) . initialize ( ) ;
12335
12443
12336
12444
await currentRouter . navigate ( "/broken" ) ;
@@ -12374,6 +12482,7 @@ describe("a router", () => {
12374
12482
} ,
12375
12483
] ,
12376
12484
history : createMemoryHistory ( ) ,
12485
+ future : { unstable_middleware : true } ,
12377
12486
} ) . initialize ( ) ;
12378
12487
12379
12488
await currentRouter . navigate ( "/works" ) ;
0 commit comments