Skip to content

Commit dd8a6ad

Browse files
committed
Fix up E2E tests
1 parent af2063e commit dd8a6ad

File tree

8 files changed

+47
-46
lines changed

8 files changed

+47
-46
lines changed

.changeset/client-context.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Add `context` support to client side data routers (unstable)
6+
7+
- Library mode
8+
- `createBrowserRouter(routes, { unstable_context })`
9+
- Framework mode
10+
- `<HydratedRouter unstable_context>`

.changeset/middleware.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
"react-router": minor
2+
"react-router": patch
33
---
44

5-
Support `middleware` on routes
5+
Support `middleware` on routes (unstable)
66

77
Routes can now define a `middleware` property accepting an array of functions that will run sequentially before route loader run in parallel. These functions accept the same arguments as `loader`/`action` and an additional `next` function to run the remaining data pipeline. This allows middlewares to perform logic before and after loaders/actions execute.
88

integration/error-data-request-test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,7 @@ test.describe("ErrorBoundary", () => {
112112
);
113113
expect(status).toBe(200);
114114
expect(headers.has("X-Remix-Error")).toBe(false);
115-
expect(data).toEqual({
116-
root: {
117-
data: null,
118-
},
119-
"routes/_index": {
120-
data: null,
121-
},
122-
});
115+
expect(data).toEqual({});
123116
});
124117

125118
test("returns a 405 on a data fetch POST to a path with no action", async () => {

integration/error-sanitization-test.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,6 @@ test.describe("Error Sanitization", () => {
232232
test("returns data without errors", async () => {
233233
let { data } = await fixture.requestSingleFetchData("/_root.data");
234234
expect(data).toEqual({
235-
root: {
236-
data: null,
237-
},
238235
"routes/_index": {
239236
data: "LOADER",
240237
},
@@ -244,9 +241,6 @@ test.describe("Error Sanitization", () => {
244241
test("sanitizes loader errors in data requests", async () => {
245242
let { data } = await fixture.requestSingleFetchData("/_root.data?loader");
246243
expect(data).toEqual({
247-
root: {
248-
data: null,
249-
},
250244
"routes/_index": {
251245
error: new Error("Unexpected Server Error"),
252246
},
@@ -392,9 +386,6 @@ test.describe("Error Sanitization", () => {
392386
test("returns data without errors", async () => {
393387
let { data } = await fixture.requestSingleFetchData("/_root.data");
394388
expect(data).toEqual({
395-
root: {
396-
data: null,
397-
},
398389
"routes/_index": {
399390
data: "LOADER",
400391
},
@@ -404,9 +395,6 @@ test.describe("Error Sanitization", () => {
404395
test("does not sanitize loader errors in data requests", async () => {
405396
let { data } = await fixture.requestSingleFetchData("/_root.data?loader");
406397
expect(data).toEqual({
407-
root: {
408-
data: null,
409-
},
410398
"routes/_index": {
411399
error: new Error("Loader Error"),
412400
},
@@ -640,9 +628,6 @@ test.describe("Error Sanitization", () => {
640628
test("returns data without errors", async () => {
641629
let { data } = await fixture.requestSingleFetchData("/_root.data");
642630
expect(data).toEqual({
643-
root: {
644-
data: null,
645-
},
646631
"routes/_index": {
647632
data: "LOADER",
648633
},
@@ -652,8 +637,9 @@ test.describe("Error Sanitization", () => {
652637
test("sanitizes loader errors in data requests", async () => {
653638
let { data } = await fixture.requestSingleFetchData("/_root.data?loader");
654639
expect(data).toEqual({
655-
root: { data: null },
656-
"routes/_index": { error: new Error("Unexpected Server Error") },
640+
"routes/_index": {
641+
error: new Error("Unexpected Server Error"),
642+
},
657643
});
658644
expect(errorLogs[0][0]).toEqual("App Specific Error Logging:");
659645
expect(errorLogs[1][0]).toEqual(

integration/vite-dot-server-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ test.describe("Vite / route / server-only module referenced by client", () => {
142142
` '${specifier}' imported by route 'app/routes/_index.tsx'`,
143143

144144
" React Router automatically removes server-code from these exports:",
145-
" `loader`, `action`, `headers`",
145+
" `loader`, `action`, `middleware`, `headers`",
146146

147147
` But other route exports in 'app/routes/_index.tsx' depend on '${specifier}'.`,
148148

integration/vite-prerender-test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,6 @@ test.describe("Prerendering", () => {
414414

415415
let dataRes = await fixture.requestSingleFetchData("/json.json.data");
416416
expect(dataRes.data).toEqual({
417-
root: {
418-
data: null,
419-
},
420417
"routes/json[.json]": {
421418
data: {
422419
hello: "world",
@@ -429,9 +426,6 @@ test.describe("Prerendering", () => {
429426

430427
dataRes = await fixture.requestSingleFetchData("/text.txt.data");
431428
expect(dataRes.data).toEqual({
432-
root: {
433-
data: null,
434-
},
435429
"routes/text[.txt]": {
436430
data: "Hello, world",
437431
},

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,17 @@ async function handleResourceRequest(
498498
return errorResponseToJson(error, serverMode);
499499
}
500500

501+
if (
502+
error instanceof Error &&
503+
error.message === "Expected a response from queryRoute"
504+
) {
505+
let newError = new Error(
506+
"Expected a Response to be returned from resource route handler"
507+
);
508+
handleError(newError);
509+
return returnLastResortErrorResponse(newError, serverMode);
510+
}
511+
501512
handleError(error);
502513
return returnLastResortErrorResponse(error, serverMode);
503514
}

packages/react-router/lib/server-runtime/single-fetch.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export async function singleFetchAction(
114114
},
115115
});
116116

117-
invariant(isResponse(result), " Expected a Response from query()");
117+
invariant(isResponse(result), "Expected a Response from query()");
118118

119119
// Unlike `handleDataRequest`, when singleFetch is enabled, query does
120120
// let non-Response return values through
@@ -197,19 +197,26 @@ export async function singleFetchLoaders(
197197
// Aggregate results based on the matches we intended to load since we get
198198
// `null` values back in `context.loaderData` for routes we didn't load
199199
let results: SingleFetchResults = {};
200-
let loadedMatches = context.matches.filter(
201-
(m) =>
202-
m.route.loader && (!loadRouteIds || loadRouteIds.has(m.route.id))
200+
let loadedMatches = new Set(
201+
context.matches
202+
.filter((m) =>
203+
loadRouteIds
204+
? loadRouteIds.has(m.route.id)
205+
: m.route.loader != null
206+
)
207+
.map((m) => m.route.id)
203208
);
204209

205-
loadedMatches.forEach((m) => {
206-
let { id } = m.route;
207-
if (context.errors && context.errors.hasOwnProperty(id)) {
208-
results[id] = { error: context.errors[id] };
209-
} else if (context.loaderData.hasOwnProperty(id)) {
210-
results[id] = { data: context.loaderData[id] };
210+
if (context.errors) {
211+
for (let [id, error] of Object.entries(context.errors)) {
212+
results[id] = { error };
211213
}
212-
});
214+
}
215+
for (let [id, data] of Object.entries(context.loaderData)) {
216+
if (!(id in results) && loadedMatches.has(id)) {
217+
results[id] = { data };
218+
}
219+
}
213220

214221
return generateSingleFetchResponse(request, build, serverMode, {
215222
result: results,
@@ -219,7 +226,7 @@ export async function singleFetchLoaders(
219226
},
220227
});
221228

222-
invariant(isResponse(result), " Expected a Response from query()");
229+
invariant(isResponse(result), "Expected a Response from query()");
223230

224231
if (isRedirectResponse(result)) {
225232
return generateSingleFetchResponse(request, build, serverMode, {

0 commit comments

Comments
 (0)