Skip to content

Commit 7226afe

Browse files
committed
add tests
1 parent 1276208 commit 7226afe

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

packages/router/__tests__/router-test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,31 @@ describe("a router", () => {
21392139
]);
21402140
});
21412141

2142+
it("matches root pathless route", () => {
2143+
let t = setup({
2144+
routes: [{ id: "root", children: [{ path: "foo" }] }],
2145+
});
2146+
2147+
t.navigate("/not-found");
2148+
expect(t.router.state.errors).toEqual({
2149+
root: {
2150+
status: 404,
2151+
statusText: "Not Found",
2152+
data: null,
2153+
},
2154+
});
2155+
expect(t.router.state.matches).toMatchObject([
2156+
{
2157+
params: {},
2158+
pathname: "",
2159+
route: {
2160+
id: "root",
2161+
children: expect.any(Array),
2162+
},
2163+
},
2164+
]);
2165+
});
2166+
21422167
it("clears prior loader/action data", async () => {
21432168
let t = initializeTmTest();
21442169
expect(t.router.state.loaderData).toEqual({
@@ -9681,6 +9706,39 @@ describe("a router", () => {
96819706
});
96829707
});
96839708

9709+
it("should support alternative submission methods", async () => {
9710+
let { query } = createStaticHandler(SSR_ROUTES);
9711+
let context;
9712+
9713+
let expected = {
9714+
actionData: {
9715+
child: "CHILD ACTION",
9716+
},
9717+
loaderData: {
9718+
parent: "PARENT LOADER",
9719+
child: "CHILD LOADER",
9720+
},
9721+
errors: null,
9722+
location: { pathname: "/parent/child" },
9723+
matches: [{ route: { id: "parent" } }, { route: { id: "child" } }],
9724+
};
9725+
9726+
context = await query(
9727+
createSubmitRequest("/parent/child", { method: "PUT" })
9728+
);
9729+
expect(context).toMatchObject(expected);
9730+
9731+
context = await query(
9732+
createSubmitRequest("/parent/child", { method: "PATCH" })
9733+
);
9734+
expect(context).toMatchObject(expected);
9735+
9736+
context = await query(
9737+
createSubmitRequest("/parent/child", { method: "DELETE" })
9738+
);
9739+
expect(context).toMatchObject(expected);
9740+
});
9741+
96849742
it("should support document submit navigations returning responses", async () => {
96859743
let { query } = createStaticHandler(SSR_ROUTES);
96869744
let context = await query(createSubmitRequest("/parent/json"));
@@ -9912,6 +9970,29 @@ describe("a router", () => {
99129970
});
99139971
});
99149972

9973+
it("should handle unsupported methods with a 405 error", async () => {
9974+
let { query } = createStaticHandler([
9975+
{
9976+
id: "root",
9977+
path: "/",
9978+
},
9979+
]);
9980+
let request = createRequest("/", { method: "OPTIONS" });
9981+
let context = await query(request);
9982+
expect(context).toMatchObject({
9983+
actionData: null,
9984+
loaderData: {},
9985+
errors: {
9986+
root: {
9987+
status: 405,
9988+
statusText: "Method Not Allowed",
9989+
data: null,
9990+
},
9991+
},
9992+
matches: [{ route: { id: "root" } }],
9993+
});
9994+
});
9995+
99159996
describe("statusCode", () => {
99169997
it("should expose a 200 status code by default", async () => {
99179998
let { query } = createStaticHandler([
@@ -10467,6 +10548,29 @@ describe("a router", () => {
1046710548
expect(data).toBe("");
1046810549
});
1046910550

10551+
it("should support alternative submission methods", async () => {
10552+
let { queryRoute } = createStaticHandler(SSR_ROUTES);
10553+
let data;
10554+
10555+
data = await queryRoute(
10556+
createSubmitRequest("/parent", { method: "PUT" }),
10557+
"parent"
10558+
);
10559+
expect(data).toBe("PARENT ACTION");
10560+
10561+
data = await queryRoute(
10562+
createSubmitRequest("/parent", { method: "PATCH" }),
10563+
"parent"
10564+
);
10565+
expect(data).toBe("PARENT ACTION");
10566+
10567+
data = await queryRoute(
10568+
createSubmitRequest("/parent", { method: "DELETE" }),
10569+
"parent"
10570+
);
10571+
expect(data).toBe("PARENT ACTION");
10572+
});
10573+
1047010574
it("should support singular route submit navigations (Responses)", async () => {
1047110575
/* eslint-disable jest/no-conditional-expect */
1047210576
let T = setupFlexRouteTest();
@@ -10742,6 +10846,31 @@ describe("a router", () => {
1074210846
}
1074310847
/* eslint-enable jest/no-conditional-expect */
1074410848
});
10849+
10850+
it("should handle unsupported methods with a 405 Response", async () => {
10851+
/* eslint-disable jest/no-conditional-expect */
10852+
let { queryRoute } = createStaticHandler([
10853+
{
10854+
id: "root",
10855+
path: "/",
10856+
},
10857+
]);
10858+
10859+
try {
10860+
await queryRoute(
10861+
createSubmitRequest("/", { method: "OPTIONS" }),
10862+
"root"
10863+
);
10864+
expect(false).toBe(true);
10865+
} catch (data) {
10866+
expect(data instanceof Response).toBe(true);
10867+
expect(data.status).toBe(405);
10868+
expect(data.statusText).toBe("Method Not Allowed");
10869+
expect(data.headers.get("X-Remix-Router-Error")).toBe("yes");
10870+
expect(await data.text()).toBe("");
10871+
}
10872+
/* eslint-enable jest/no-conditional-expect */
10873+
});
1074510874
});
1074610875
});
1074710876
});

0 commit comments

Comments
 (0)