Skip to content

Commit d4f11ae

Browse files
committed
Add future flag
1 parent 667ec51 commit d4f11ae

File tree

2 files changed

+175
-17
lines changed

2 files changed

+175
-17
lines changed

packages/router/__tests__/router-test.ts

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11626,6 +11626,7 @@ describe("a router", () => {
1162611626
currentRouter = createRouter({
1162711627
routes: MIDDLEWARE_ORDERING_ROUTES,
1162811628
history: createMemoryHistory(),
11629+
future: { unstable_middleware: true },
1162911630
}).initialize();
1163011631

1163111632
await currentRouter.navigate("/parent");
@@ -11648,6 +11649,7 @@ describe("a router", () => {
1164811649
currentRouter = createRouter({
1164911650
routes: MIDDLEWARE_ORDERING_ROUTES,
1165011651
history: createMemoryHistory(),
11652+
future: { unstable_middleware: true },
1165111653
}).initialize();
1165211654

1165311655
await currentRouter.navigate("/parent", {
@@ -11680,6 +11682,7 @@ describe("a router", () => {
1168011682
currentRouter = createRouter({
1168111683
routes: MIDDLEWARE_ORDERING_ROUTES,
1168211684
history: createMemoryHistory(),
11685+
future: { unstable_middleware: true },
1168311686
}).initialize();
1168411687

1168511688
await currentRouter.navigate("/parent/child/grandchild");
@@ -11727,6 +11730,7 @@ describe("a router", () => {
1172711730
currentRouter = createRouter({
1172811731
routes: MIDDLEWARE_ORDERING_ROUTES,
1172911732
history: createMemoryHistory(),
11733+
future: { unstable_middleware: true },
1173011734
}).initialize();
1173111735

1173211736
await currentRouter.navigate("/parent/child/grandchild", {
@@ -11791,6 +11795,7 @@ describe("a router", () => {
1179111795
currentRouter = createRouter({
1179211796
routes: MIDDLEWARE_ORDERING_ROUTES,
1179311797
history: createMemoryHistory(),
11798+
future: { unstable_middleware: true },
1179411799
}).initialize();
1179511800

1179611801
await currentRouter.fetch(
@@ -11817,6 +11822,7 @@ describe("a router", () => {
1181711822
currentRouter = createRouter({
1181811823
routes: MIDDLEWARE_ORDERING_ROUTES,
1181911824
history: createMemoryHistory(),
11825+
future: { unstable_middleware: true },
1182011826
}).initialize();
1182111827

1182211828
await currentRouter.fetch(
@@ -11847,6 +11853,7 @@ describe("a router", () => {
1184711853
currentRouter = createRouter({
1184811854
routes: MIDDLEWARE_ORDERING_ROUTES,
1184911855
history: createMemoryHistory(),
11856+
future: { unstable_middleware: true },
1185011857
}).initialize();
1185111858

1185211859
await currentRouter.navigate("/parent/child/grandchild");
@@ -11901,6 +11908,7 @@ describe("a router", () => {
1190111908
currentRouter = createRouter({
1190211909
routes: MIDDLEWARE_ORDERING_ROUTES,
1190311910
history: createMemoryHistory(),
11911+
future: { unstable_middleware: true },
1190411912
}).initialize();
1190511913

1190611914
await currentRouter.navigate("/parent");
@@ -11941,7 +11949,9 @@ describe("a router", () => {
1194111949
});
1194211950

1194311951
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+
});
1194511955

1194611956
let context = await query(createRequest("/parent/child/grandchild"));
1194711957

@@ -11981,7 +11991,9 @@ describe("a router", () => {
1198111991
});
1198211992

1198311993
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+
});
1198511997

1198611998
let result = await queryRoute(
1198711999
createRequest("/parent/child/grandchild")
@@ -12035,6 +12047,7 @@ describe("a router", () => {
1203512047
},
1203612048
],
1203712049
history: createMemoryHistory(),
12050+
future: { unstable_middleware: true },
1203812051
}).initialize();
1203912052

1204012053
await currentRouter.navigate("/parent/child");
@@ -12074,6 +12087,7 @@ describe("a router", () => {
1207412087
},
1207512088
],
1207612089
history: createMemoryHistory(),
12090+
future: { unstable_middleware: true },
1207712091
}).initialize();
1207812092

1207912093
await currentRouter?.navigate("/parent");
@@ -12105,6 +12119,7 @@ describe("a router", () => {
1210512119
},
1210612120
],
1210712121
history: createMemoryHistory(),
12122+
future: { unstable_middleware: true },
1210812123
}).initialize();
1210912124

1211012125
await currentRouter?.navigate("/parent");
@@ -12139,6 +12154,7 @@ describe("a router", () => {
1213912154
},
1214012155
],
1214112156
history: createMemoryHistory(),
12157+
future: { unstable_middleware: true },
1214212158
}).initialize();
1214312159

1214412160
await currentRouter?.navigate("/parent", {
@@ -12152,6 +12168,92 @@ describe("a router", () => {
1215212168
),
1215312169
});
1215412170
});
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+
});
1215512257
});
1215612258

1215712259
describe("middleware context", () => {
@@ -12213,6 +12315,7 @@ describe("a router", () => {
1221312315
currentRouter = createRouter({
1221412316
routes: MIDDLEWARE_CONTEXT_ROUTES,
1221512317
history: createMemoryHistory(),
12318+
future: { unstable_middleware: true },
1221612319
}).initialize();
1221712320

1221812321
await currentRouter.navigate("/parent/child/grandchild");
@@ -12231,6 +12334,7 @@ describe("a router", () => {
1223112334
currentRouter = createRouter({
1223212335
routes: MIDDLEWARE_CONTEXT_ROUTES,
1223312336
history: createMemoryHistory(),
12337+
future: { unstable_middleware: true },
1223412338
}).initialize();
1223512339

1223612340
await currentRouter.navigate("/parent/child/grandchild", {
@@ -12255,6 +12359,7 @@ describe("a router", () => {
1225512359
currentRouter = createRouter({
1225612360
routes: MIDDLEWARE_CONTEXT_ROUTES,
1225712361
history: createMemoryHistory(),
12362+
future: { unstable_middleware: true },
1225812363
}).initialize();
1225912364

1226012365
await currentRouter.fetch("key", "root", "/parent/child/grandchild");
@@ -12269,6 +12374,7 @@ describe("a router", () => {
1226912374
currentRouter = createRouter({
1227012375
routes: MIDDLEWARE_CONTEXT_ROUTES,
1227112376
history: createMemoryHistory(),
12377+
future: { unstable_middleware: true },
1227212378
}).initialize();
1227312379

1227412380
await currentRouter.fetch("key", "root", "/parent/child/grandchild", {
@@ -12299,6 +12405,7 @@ describe("a router", () => {
1229912405
},
1230012406
],
1230112407
history: createMemoryHistory(),
12408+
future: { unstable_middleware: true },
1230212409
}).initialize();
1230312410

1230412411
await currentRouter.navigate("/broken");
@@ -12331,6 +12438,7 @@ describe("a router", () => {
1233112438
},
1233212439
],
1233312440
history: createMemoryHistory(),
12441+
future: { unstable_middleware: true },
1233412442
}).initialize();
1233512443

1233612444
await currentRouter.navigate("/broken");
@@ -12374,6 +12482,7 @@ describe("a router", () => {
1237412482
},
1237512483
],
1237612484
history: createMemoryHistory(),
12485+
future: { unstable_middleware: true },
1237712486
}).initialize();
1237812487

1237912488
await currentRouter.navigate("/works");

0 commit comments

Comments
 (0)