Skip to content

Commit 480955d

Browse files
committed
Add unit tests and changeset
1 parent e404d4a commit 480955d

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Fix an issue in `queryRoute` that was not always identifying thrown `Response` instances

packages/router/__tests__/router-test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16230,6 +16230,46 @@ describe("a router", () => {
1623016230
expect(await data.json()).toEqual({ key: "value" });
1623116231
});
1623216232

16233+
it("should not unwrap responses thrown from loaders", async () => {
16234+
let response = json({ key: "value" });
16235+
let { queryRoute } = createStaticHandler([
16236+
{
16237+
id: "root",
16238+
path: "/",
16239+
loader: () => Promise.reject(response),
16240+
},
16241+
]);
16242+
let request = createRequest("/");
16243+
let data;
16244+
try {
16245+
await queryRoute(request, { routeId: "root" });
16246+
} catch (e) {
16247+
data = e;
16248+
}
16249+
expect(data instanceof Response).toBe(true);
16250+
expect(await data.json()).toEqual({ key: "value" });
16251+
});
16252+
16253+
it("should not unwrap responses thrown from actions", async () => {
16254+
let response = json({ key: "value" });
16255+
let { queryRoute } = createStaticHandler([
16256+
{
16257+
id: "root",
16258+
path: "/",
16259+
action: () => Promise.reject(response),
16260+
},
16261+
]);
16262+
let request = createSubmitRequest("/");
16263+
let data;
16264+
try {
16265+
await queryRoute(request, { routeId: "root" });
16266+
} catch (e) {
16267+
data = e;
16268+
}
16269+
expect(data instanceof Response).toBe(true);
16270+
expect(await data.json()).toEqual({ key: "value" });
16271+
});
16272+
1623316273
it("should handle aborted load requests", async () => {
1623416274
let dfd = createDeferred();
1623516275
let controller = new AbortController();

packages/router/router.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,7 +2789,7 @@ export function createStaticHandler(
27892789
// it to bail out and then return or throw here based on whether the user
27902790
// returned or threw
27912791
if (isQueryRouteResponse(e)) {
2792-
if (e.type === ResultType.error && !isRedirectResponse(e.response)) {
2792+
if (e.type === ResultType.error) {
27932793
throw e.response;
27942794
}
27952795
return e.response;
@@ -3741,11 +3741,11 @@ async function callLoaderOrAction(
37413741
// without unwrapping. We do this with the QueryRouteResponse wrapper
37423742
// interface so we can know whether it was returned or thrown
37433743
if (opts.isRouteRequest) {
3744-
// eslint-disable-next-line no-throw-literal
3745-
throw {
3744+
let queryRouteResponse: QueryRouteResponse = {
37463745
type: resultType || ResultType.data,
37473746
response: result,
37483747
};
3748+
throw queryRouteResponse;
37493749
}
37503750

37513751
let data: any;

0 commit comments

Comments
 (0)