Skip to content

Commit 1c3c549

Browse files
committed
More tests and cleanup
1 parent fbbfb71 commit 1c3c549

File tree

2 files changed

+104
-8
lines changed

2 files changed

+104
-8
lines changed

packages/react-router/__tests__/router/router-test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,42 @@ describe("a router", () => {
936936
});
937937
});
938938

939+
it("does not run middlewares when complete hydrationData exists", async () => {
940+
let middlewareSpy = jest.fn();
941+
let loaderSpy = jest.fn();
942+
let router = createRouter({
943+
history: createMemoryHistory(),
944+
routes: [
945+
{
946+
id: "index",
947+
path: "/",
948+
middleware: [middlewareSpy],
949+
loader: loaderSpy,
950+
},
951+
],
952+
hydrationData: {
953+
loaderData: {
954+
index: "INDEX DATA",
955+
},
956+
},
957+
});
958+
router.initialize();
959+
960+
expect(router.state).toMatchObject({
961+
historyAction: "POP",
962+
location: {
963+
pathname: "/",
964+
},
965+
initialized: true,
966+
navigation: IDLE_NAVIGATION,
967+
loaderData: {
968+
index: "INDEX DATA",
969+
},
970+
});
971+
expect(middlewareSpy).not.toHaveBeenCalled();
972+
expect(loaderSpy).not.toHaveBeenCalled();
973+
});
974+
939975
it("kicks off initial data load if no hydration data is provided", async () => {
940976
let parentDfd = createDeferred();
941977
let parentSpy = jest.fn(() => parentDfd.promise);
@@ -993,6 +1029,61 @@ describe("a router", () => {
9931029
router.dispose();
9941030
});
9951031

1032+
it("run middlewares without loaders on initial load if no hydration data is provided", async () => {
1033+
let parentDfd = createDeferred();
1034+
let parentSpy = jest.fn(() => parentDfd.promise);
1035+
let childDfd = createDeferred();
1036+
let childSpy = jest.fn(() => childDfd.promise);
1037+
let router = createRouter({
1038+
history: createMemoryHistory(),
1039+
routes: [
1040+
{
1041+
path: "/",
1042+
middleware: [parentSpy],
1043+
children: [
1044+
{
1045+
index: true,
1046+
middleware: [childSpy],
1047+
},
1048+
],
1049+
},
1050+
],
1051+
});
1052+
router.initialize();
1053+
await tick();
1054+
1055+
expect(console.warn).not.toHaveBeenCalled();
1056+
expect(parentSpy.mock.calls.length).toBe(1);
1057+
expect(childSpy.mock.calls.length).toBe(0);
1058+
expect(router.state).toMatchObject({
1059+
historyAction: "POP",
1060+
location: expect.objectContaining({ pathname: "/" }),
1061+
initialized: false,
1062+
navigation: IDLE_NAVIGATION,
1063+
});
1064+
expect(router.state.loaderData).toEqual({});
1065+
1066+
await parentDfd.resolve(undefined);
1067+
expect(router.state).toMatchObject({
1068+
historyAction: "POP",
1069+
location: expect.objectContaining({ pathname: "/" }),
1070+
initialized: false,
1071+
navigation: IDLE_NAVIGATION,
1072+
});
1073+
expect(router.state.loaderData).toEqual({});
1074+
1075+
await childDfd.resolve(undefined);
1076+
expect(router.state).toMatchObject({
1077+
historyAction: "POP",
1078+
location: expect.objectContaining({ pathname: "/" }),
1079+
initialized: true,
1080+
navigation: IDLE_NAVIGATION,
1081+
loaderData: {},
1082+
});
1083+
1084+
router.dispose();
1085+
});
1086+
9961087
it("allows routes to be initialized with undefined loaderData", async () => {
9971088
let t = setup({
9981089
routes: [

packages/react-router/lib/router/router.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,7 @@ export function createRouter(init: RouterInit): Router {
960960
// functions around still then we'll need to run them in initialize()
961961
initialized = false;
962962
} else if (
963-
!initialMatches.some(
964-
(m) =>
965-
m.route.loader ||
966-
(m.route.middleware && m.route.middleware.length > 0),
967-
)
963+
!initialMatches.some((m) => routeHasLoaderOrMiddleware(m.route))
968964
) {
969965
// If we've got no loaders or middleware to run, then we're good to go
970966
initialized = true;
@@ -2098,7 +2094,9 @@ export function createRouter(init: RouterInit): Router {
20982094
if (
20992095
!init.dataStrategy &&
21002096
!dsMatches.some((m) => m.shouldLoad) &&
2101-
!dsMatches.some((m) => m.route.middleware) &&
2097+
!dsMatches.some(
2098+
(m) => m.route.middleware && m.route.middleware.length > 0,
2099+
) &&
21022100
revalidatingFetchers.length === 0
21032101
) {
21042102
let updatedFetchers = markFetchRedirectsDone();
@@ -4769,7 +4767,7 @@ function getMatchesToLoad(
47694767
} else if (route.lazy) {
47704768
// We haven't loaded this route yet so we don't know if it's got a loader!
47714769
forceShouldLoad = true;
4772-
} else if (route.loader == null) {
4770+
} else if (!routeHasLoaderOrMiddleware(route)) {
47734771
// Nothing to load!
47744772
forceShouldLoad = false;
47754773
} else if (initialHydration) {
@@ -4956,6 +4954,13 @@ function getMatchesToLoad(
49564954
return { dsMatches, revalidatingFetchers };
49574955
}
49584956

4957+
function routeHasLoaderOrMiddleware(route: RouteObject) {
4958+
return (
4959+
route.loader != null ||
4960+
(route.middleware != null && route.middleware.length > 0)
4961+
);
4962+
}
4963+
49594964
function shouldLoadRouteOnHydration(
49604965
route: AgnosticDataRouteObject,
49614966
loaderData: RouteData | null | undefined,
@@ -4967,7 +4972,7 @@ function shouldLoadRouteOnHydration(
49674972
}
49684973

49694974
// No loader or middleware, nothing to run
4970-
if (!route.loader && (!route.middleware || route.middleware.length === 0)) {
4975+
if (!routeHasLoaderOrMiddleware(route)) {
49714976
return false;
49724977
}
49734978

0 commit comments

Comments
 (0)