Skip to content

Commit

Permalink
Updates to createStaticHandler for Remix consumption (#9482)
Browse files Browse the repository at this point in the history
* Updates to createStaticHandler for Remix consumption

* unit tests

* add changeset

* one more test for HEAD in queryRoute

* add tests
  • Loading branch information
brophdawg11 committed Oct 21, 2022
1 parent 80d5844 commit 3d0d28c
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 66 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-shirts-admire.md
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Changes to statis handler for incorporating into Remix"
204 changes: 159 additions & 45 deletions packages/router/__tests__/router-test.ts
Expand Up @@ -2144,6 +2144,31 @@ describe("a router", () => {
]);
});

it("matches root pathless route", () => {
let t = setup({
routes: [{ id: "root", children: [{ path: "foo" }] }],
});

t.navigate("/not-found");
expect(t.router.state.errors).toEqual({
root: {
status: 404,
statusText: "Not Found",
data: null,
},
});
expect(t.router.state.matches).toMatchObject([
{
params: {},
pathname: "",
route: {
id: "root",
children: expect.any(Array),
},
},
]);
});

it("clears prior loader/action data", async () => {
let t = initializeTmTest();
expect(t.router.state.loaderData).toEqual({
Expand Down Expand Up @@ -3128,11 +3153,7 @@ describe("a router", () => {
formData: createFormData({ gosh: "dang" }),
});
expect(t.router.state.errors).toEqual({
child: new ErrorResponse(
405,
"Method Not Allowed",
"No action found for [/child]"
),
child: new ErrorResponse(405, "Method Not Allowed", ""),
});
expect(console.warn).toHaveBeenCalled();
spy.mockReset();
Expand Down Expand Up @@ -3185,11 +3206,7 @@ describe("a router", () => {
});
expect(t.router.state.actionData).toBe(null);
expect(t.router.state.errors).toEqual({
grandchild: new ErrorResponse(
405,
"Method Not Allowed",
"No action found for [/child/grandchild]"
),
grandchild: new ErrorResponse(405, "Method Not Allowed", ""),
});
});
});
Expand Down Expand Up @@ -6667,11 +6684,7 @@ describe("a router", () => {
});
expect(A.fetcher).toBe(IDLE_FETCHER);
expect(t.router.state.errors).toEqual({
root: new ErrorResponse(
405,
"Method Not Allowed",
"No action found for [/]"
),
root: new ErrorResponse(405, "Method Not Allowed", ""),
});
});

Expand Down Expand Up @@ -9906,6 +9919,23 @@ describe("a router", () => {
});
});

it("should support document load navigations with HEAD requests", async () => {
let { query } = createStaticHandler(SSR_ROUTES);
let context = await query(
createRequest("/parent/child", { method: "HEAD" })
);
expect(context).toMatchObject({
actionData: null,
loaderData: {
parent: "PARENT LOADER",
child: "CHILD LOADER",
},
errors: null,
location: { pathname: "/parent/child" },
matches: [{ route: { id: "parent" } }, { route: { id: "child" } }],
});
});

it("should support document load navigations returning responses", async () => {
let { query } = createStaticHandler(SSR_ROUTES);
let context = await query(createRequest("/parent/json"));
Expand Down Expand Up @@ -9962,6 +9992,39 @@ describe("a router", () => {
});
});

it("should support alternative submission methods", async () => {
let { query } = createStaticHandler(SSR_ROUTES);
let context;

let expected = {
actionData: {
child: "CHILD ACTION",
},
loaderData: {
parent: "PARENT LOADER",
child: "CHILD LOADER",
},
errors: null,
location: { pathname: "/parent/child" },
matches: [{ route: { id: "parent" } }, { route: { id: "child" } }],
};

context = await query(
createSubmitRequest("/parent/child", { method: "PUT" })
);
expect(context).toMatchObject(expected);

context = await query(
createSubmitRequest("/parent/child", { method: "PATCH" })
);
expect(context).toMatchObject(expected);

context = await query(
createSubmitRequest("/parent/child", { method: "DELETE" })
);
expect(context).toMatchObject(expected);
});

it("should support document submit navigations returning responses", async () => {
let { query } = createStaticHandler(SSR_ROUTES);
let context = await query(createSubmitRequest("/parent/json"));
Expand Down Expand Up @@ -10202,20 +10265,6 @@ describe("a router", () => {
expect(e).toMatchInlineSnapshot(`[Error: query() call aborted]`);
});

it("should not support HEAD requests", async () => {
let { query } = createStaticHandler(SSR_ROUTES);
let request = createRequest("/", { method: "head" });
let e;
try {
await query(request);
} catch (_e) {
e = _e;
}
expect(e).toMatchInlineSnapshot(
`[Error: query()/queryRoute() do not support HEAD requests]`
);
});

it("should require a signal on the request", async () => {
let { query } = createStaticHandler(SSR_ROUTES);
let request = createRequest("/", { signal: undefined });
Expand Down Expand Up @@ -10246,7 +10295,30 @@ describe("a router", () => {
root: {
status: 405,
statusText: "Method Not Allowed",
data: "No action found for [/]",
data: "",
},
},
matches: [{ route: { id: "root" } }],
});
});

it("should handle unsupported methods with a 405 error", async () => {
let { query } = createStaticHandler([
{
id: "root",
path: "/",
},
]);
let request = createRequest("/", { method: "OPTIONS" });
let context = await query(request);
expect(context).toMatchObject({
actionData: null,
loaderData: {},
errors: {
root: {
status: 405,
statusText: "Method Not Allowed",
data: null,
},
},
matches: [{ route: { id: "root" } }],
Expand Down Expand Up @@ -10632,6 +10704,14 @@ describe("a router", () => {
expect(data).toBe("CHILD LOADER");
});

it("should support HEAD requests", async () => {
let { queryRoute } = createStaticHandler(SSR_ROUTES);
let data = await queryRoute(
createRequest("/parent", { method: "HEAD" })
);
expect(data).toBe("PARENT LOADER");
});

it("should support singular route load navigations (primitives)", async () => {
let { queryRoute } = createStaticHandler(SSR_ROUTES);
let data;
Expand Down Expand Up @@ -10800,6 +10880,29 @@ describe("a router", () => {
expect(data).toBe("");
});

it("should support alternative submission methods", async () => {
let { queryRoute } = createStaticHandler(SSR_ROUTES);
let data;

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

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

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

it("should support singular route submit navigations (Responses)", async () => {
/* eslint-disable jest/no-conditional-expect */
let T = setupFlexRouteTest();
Expand Down Expand Up @@ -11042,20 +11145,6 @@ describe("a router", () => {
expect(e).toMatchInlineSnapshot(`[Error: queryRoute() call aborted]`);
});

it("should not support HEAD requests", async () => {
let { queryRoute } = createStaticHandler(SSR_ROUTES);
let request = createRequest("/", { method: "head" });
let e;
try {
await queryRoute(request, "index");
} catch (_e) {
e = _e;
}
expect(e).toMatchInlineSnapshot(
`[Error: query()/queryRoute() do not support HEAD requests]`
);
});

it("should require a signal on the request", async () => {
let { queryRoute } = createStaticHandler(SSR_ROUTES);
let request = createRequest("/", { signal: undefined });
Expand Down Expand Up @@ -11139,7 +11228,32 @@ describe("a router", () => {
expect(data.status).toBe(405);
expect(data.statusText).toBe("Method Not Allowed");
expect(data.headers.get("X-Remix-Router-Error")).toBe("yes");
expect(await data.text()).toBe("No action found for [/]");
expect(await data.text()).toBe("");
}
/* eslint-enable jest/no-conditional-expect */
});

it("should handle unsupported methods with a 405 Response", async () => {
/* eslint-disable jest/no-conditional-expect */
let { queryRoute } = createStaticHandler([
{
id: "root",
path: "/",
},
]);

try {
await queryRoute(
createSubmitRequest("/", { method: "OPTIONS" }),
"root"
);
expect(false).toBe(true);
} catch (data) {
expect(data instanceof Response).toBe(true);
expect(data.status).toBe(405);
expect(data.statusText).toBe("Method Not Allowed");
expect(data.headers.get("X-Remix-Router-Error")).toBe("yes");
expect(await data.text()).toBe("");
}
/* eslint-enable jest/no-conditional-expect */
});
Expand Down

0 comments on commit 3d0d28c

Please sign in to comment.