Skip to content

Commit

Permalink
Add requestContext to staticHandler query/queryRoute (#9696)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Dec 6, 2022
1 parent 5dba8ed commit a5e233d
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 53 deletions.
7 changes: 7 additions & 0 deletions .changeset/afraid-snakes-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@remix-run/router": patch
---

Add `requestContext` support to static handler `query`/`queryRoute`

- Note that the unstable API of `queryRoute(path, routeId)` has been changed to `queryRoute(path, { routeId, requestContext })`
168 changes: 132 additions & 36 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10932,6 +10932,42 @@ describe("a router", () => {
expect(childLoaderRequest.url).toBe("http://localhost/child");
});

it("should support a requestContext passed to loaders and actions", async () => {
let requestContext = { sessionId: "12345" };
let rootStub = jest.fn(() => "ROOT");
let childStub = jest.fn(() => "CHILD");
let actionStub = jest.fn(() => "CHILD ACTION");
let arg = (s) => s.mock.calls[0][0];
let { query } = createStaticHandler([
{
id: "root",
path: "/",
loader: rootStub,
children: [
{
id: "child",
path: "child",
action: actionStub,
loader: childStub,
},
],
},
]);

await query(createRequest("/child"), { requestContext });
expect(arg(rootStub).context.sessionId).toBe("12345");
expect(arg(childStub).context.sessionId).toBe("12345");

actionStub.mockClear();
rootStub.mockClear();
childStub.mockClear();

await query(createSubmitRequest("/child"), { requestContext });
expect(arg(actionStub).context.sessionId).toBe("12345");
expect(arg(rootStub).context.sessionId).toBe("12345");
expect(arg(childStub).context.sessionId).toBe("12345");
});

describe("statusCode", () => {
it("should expose a 200 status code by default", async () => {
let { query } = createStaticHandler([
Expand Down Expand Up @@ -11254,7 +11290,7 @@ describe("a router", () => {
isError ? Promise.reject(data) : Promise.resolve(data),
},
]);
return handler.queryRoute(req, routeId);
return handler.queryRoute(req, { routeId });
}

return {
Expand Down Expand Up @@ -11307,7 +11343,9 @@ describe("a router", () => {
data = await queryRoute(createRequest("/parent?index"));
expect(data).toBe("PARENT INDEX LOADER");

data = await queryRoute(createRequest("/parent/child"), "child");
data = await queryRoute(createRequest("/parent/child"), {
routeId: "child",
});
expect(data).toBe("CHILD LOADER");
});

Expand All @@ -11324,19 +11362,27 @@ describe("a router", () => {
let data;

// Layout route
data = await queryRoute(createRequest("/parent"), "parent");
data = await queryRoute(createRequest("/parent"), {
routeId: "parent",
});
expect(data).toBe("PARENT LOADER");

// Index route
data = await queryRoute(createRequest("/parent"), "parentIndex");
data = await queryRoute(createRequest("/parent"), {
routeId: "parentIndex",
});
expect(data).toBe("PARENT INDEX LOADER");

// Parent in nested route
data = await queryRoute(createRequest("/parent/child"), "parent");
data = await queryRoute(createRequest("/parent/child"), {
routeId: "parent",
});
expect(data).toBe("PARENT LOADER");

// Child in nested route
data = await queryRoute(createRequest("/parent/child"), "child");
data = await queryRoute(createRequest("/parent/child"), {
routeId: "child",
});
expect(data).toBe("CHILD LOADER");

// Non-undefined falsey values should count
Expand Down Expand Up @@ -11464,19 +11510,27 @@ describe("a router", () => {
let data;

// Layout route
data = await queryRoute(createRequest("/base/parent"), "parent");
data = await queryRoute(createRequest("/base/parent"), {
routeId: "parent",
});
expect(data).toBe("PARENT LOADER");

// Index route
data = await queryRoute(createRequest("/base/parent"), "parentIndex");
data = await queryRoute(createRequest("/base/parent"), {
routeId: "parentIndex",
});
expect(data).toBe("PARENT INDEX LOADER");

// Parent in nested route
data = await queryRoute(createRequest("/base/parent/child"), "parent");
data = await queryRoute(createRequest("/base/parent/child"), {
routeId: "parent",
});
expect(data).toBe("PARENT LOADER");

// Child in nested route
data = await queryRoute(createRequest("/base/parent/child"), "child");
data = await queryRoute(createRequest("/base/parent/child"), {
routeId: "child",
});
expect(data).toBe("CHILD LOADER");

// Non-undefined falsey values should count
Expand All @@ -11494,19 +11548,27 @@ describe("a router", () => {
let data;

// Layout route
data = await queryRoute(createSubmitRequest("/parent"), "parent");
data = await queryRoute(createSubmitRequest("/parent"), {
routeId: "parent",
});
expect(data).toBe("PARENT ACTION");

// Index route
data = await queryRoute(createSubmitRequest("/parent"), "parentIndex");
data = await queryRoute(createSubmitRequest("/parent"), {
routeId: "parentIndex",
});
expect(data).toBe("PARENT INDEX ACTION");

// Parent in nested route
data = await queryRoute(createSubmitRequest("/parent/child"), "parent");
data = await queryRoute(createSubmitRequest("/parent/child"), {
routeId: "parent",
});
expect(data).toBe("PARENT ACTION");

// Child in nested route
data = await queryRoute(createSubmitRequest("/parent/child"), "child");
data = await queryRoute(createSubmitRequest("/parent/child"), {
routeId: "child",
});
expect(data).toBe("CHILD ACTION");

// Non-undefined falsey values should count
Expand All @@ -11525,19 +11587,19 @@ describe("a router", () => {

data = await queryRoute(
createSubmitRequest("/parent", { method: "PUT" }),
"parent"
{ routeId: "parent" }
);
expect(data).toBe("PARENT ACTION");

data = await queryRoute(
createSubmitRequest("/parent", { method: "PATCH" }),
"parent"
{ routeId: "parent" }
);
expect(data).toBe("PARENT ACTION");

data = await queryRoute(
createSubmitRequest("/parent", { method: "DELETE" }),
"parent"
{ routeId: "parent" }
);
expect(data).toBe("PARENT ACTION");
});
Expand Down Expand Up @@ -11697,10 +11759,9 @@ describe("a router", () => {
],
},
]);
let response = await queryRoute(
createRequest("/parent/child"),
"child"
);
let response = await queryRoute(createRequest("/parent/child"), {
routeId: "child",
});
expect(response instanceof Response).toBe(true);
expect((response as Response).status).toBe(302);
expect((response as Response).headers.get("Location")).toBe("/parent");
Expand All @@ -11724,10 +11785,9 @@ describe("a router", () => {
],
},
]);
let response = await queryRoute(
createSubmitRequest("/parent/child"),
"child"
);
let response = await queryRoute(createSubmitRequest("/parent/child"), {
routeId: "child",
});
expect(response instanceof Response).toBe(true);
expect((response as Response).status).toBe(302);
expect((response as Response).headers.get("Location")).toBe("/parent");
Expand All @@ -11749,7 +11809,9 @@ describe("a router", () => {
loader: () => redirect(url),
},
]);
let response = await handler.queryRoute(createRequest("/"), "root");
let response = await handler.queryRoute(createRequest("/"), {
routeId: "root",
});
expect(response instanceof Response).toBe(true);
expect((response as Response).status).toBe(302);
expect((response as Response).headers.get("Location")).toBe(url);
Expand All @@ -11766,7 +11828,7 @@ describe("a router", () => {
},
]);
let request = createRequest("/");
let data = await queryRoute(request, "root");
let data = await queryRoute(request, { routeId: "root" });
expect(data instanceof Response).toBe(true);
expect(await data.json()).toEqual({ key: "value" });
});
Expand All @@ -11781,7 +11843,7 @@ describe("a router", () => {
},
]);
let request = createSubmitRequest("/");
let data = await queryRoute(request, "root");
let data = await queryRoute(request, { routeId: "root" });
expect(data instanceof Response).toBe(true);
expect(await data.json()).toEqual({ key: "value" });
});
Expand All @@ -11801,7 +11863,7 @@ describe("a router", () => {
});
let e;
try {
let statePromise = queryRoute(request, "root");
let statePromise = queryRoute(request, { routeId: "root" });
controller.abort();
// This should resolve even though we never resolved the loader
await statePromise;
Expand All @@ -11826,7 +11888,7 @@ describe("a router", () => {
});
let e;
try {
let statePromise = queryRoute(request, "root");
let statePromise = queryRoute(request, { routeId: "root" });
controller.abort();
// This should resolve even though we never resolved the loader
await statePromise;
Expand All @@ -11841,7 +11903,7 @@ describe("a router", () => {
let request = createRequest("/", { signal: undefined });
let e;
try {
await queryRoute(request, "index");
await queryRoute(request, { routeId: "index" });
} catch (_e) {
e = _e;
}
Expand All @@ -11850,6 +11912,38 @@ describe("a router", () => {
);
});

it("should support a requestContext passed to loaders and actions", async () => {
let requestContext = { sessionId: "12345" };
let childStub = jest.fn(() => "CHILD");
let actionStub = jest.fn(() => "CHILD ACTION");
let arg = (s) => s.mock.calls[0][0];
let { queryRoute } = createStaticHandler([
{
path: "/",
children: [
{
id: "child",
path: "child",
action: actionStub,
loader: childStub,
},
],
},
]);

await queryRoute(createRequest("/child"), {
routeId: "child",
requestContext,
});
expect(arg(childStub).context.sessionId).toBe("12345");

await queryRoute(createSubmitRequest("/child"), {
routeId: "child",
requestContext,
});
expect(arg(actionStub).context.sessionId).toBe("12345");
});

describe("Errors with Status Codes", () => {
/* eslint-disable jest/no-conditional-expect */
let { queryRoute } = createStaticHandler([
Expand Down Expand Up @@ -11887,7 +11981,7 @@ describe("a router", () => {

it("should handle not found routeIds with a 403 Response", async () => {
try {
await queryRoute(createRequest("/"), "junk");
await queryRoute(createRequest("/"), { routeId: "junk" });
expect(false).toBe(true);
} catch (data) {
expect(isRouteErrorResponse(data)).toBe(true);
Expand All @@ -11899,7 +11993,7 @@ describe("a router", () => {
}

try {
await queryRoute(createSubmitRequest("/"), "junk");
await queryRoute(createSubmitRequest("/"), { routeId: "junk" });
expect(false).toBe(true);
} catch (data) {
expect(isRouteErrorResponse(data)).toBe(true);
Expand All @@ -11913,7 +12007,7 @@ describe("a router", () => {

it("should handle missing loaders with a 400 Response", async () => {
try {
await queryRoute(createRequest("/"), "root");
await queryRoute(createRequest("/"), { routeId: "root" });
expect(false).toBe(true);
} catch (data) {
expect(isRouteErrorResponse(data)).toBe(true);
Expand All @@ -11930,7 +12024,7 @@ describe("a router", () => {

it("should handle missing actions with a 405 Response", async () => {
try {
await queryRoute(createSubmitRequest("/"), "root");
await queryRoute(createSubmitRequest("/"), { routeId: "root" });
expect(false).toBe(true);
} catch (data) {
expect(isRouteErrorResponse(data)).toBe(true);
Expand All @@ -11947,7 +12041,9 @@ describe("a router", () => {

it("should handle unsupported methods with a 405 Response", async () => {
try {
await queryRoute(createRequest("/", { method: "OPTIONS" }), "root");
await queryRoute(createRequest("/", { method: "OPTIONS" }), {
routeId: "root",
});
expect(false).toBe(true);
} catch (data) {
expect(isRouteErrorResponse(data)).toBe(true);
Expand Down
Loading

0 comments on commit a5e233d

Please sign in to comment.