Skip to content

Commit

Permalink
Match splat after something other than /
Browse files Browse the repository at this point in the history
Also, remove `pathnameStart` property from `match` objects.
  • Loading branch information
mjackson committed Sep 20, 2021
1 parent 1082b45 commit 4bf20d7
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 236 deletions.
229 changes: 151 additions & 78 deletions packages/react-router/__tests__/matchPath-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,131 +2,204 @@ import { matchPath } from "react-router";

describe("matchPath", () => {
it("matches the root / URL", () => {
let match = matchPath("/", "/")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/",
pathnameStart: "/"
});
expect(matchPath("/", "/")).toMatchObject({ pathname: "/" });
});

it("matches a pathname", () => {
let match = matchPath("users", "/users")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/users",
pathnameStart: "/users"
describe("when the pattern has no leading slash", () => {
it("fails to match a pathname that does not match", () => {
expect(matchPath("users", "/usersblah")).toBeNull();
});

it("matches a pathname", () => {
expect(matchPath("users", "/users")).toMatchObject({
pathname: "/users"
});
});

it("matches a pathname with multiple segments", () => {
expect(matchPath("users/mj", "/users/mj")).toMatchObject({
pathname: "/users/mj"
});
});

it("matches a pathname with a trailing slash", () => {
expect(matchPath("users", "/users/")).toMatchObject({
pathname: "/users/"
});
});

it("matches a pathname with multiple segments and a trailing slash", () => {
expect(matchPath("users/mj", "/users/mj/")).toMatchObject({
pathname: "/users/mj/"
});
});
});

it("matches a pathname with multiple segments", () => {
let match = matchPath("users/mj", "/users/mj")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/users/mj",
pathnameStart: "/users/mj"
describe("when the pattern has a leading slash", () => {
it("fails to match a pathname that does not match", () => {
expect(matchPath("/users", "/usersblah")).toBeNull();
});

it("matches a pathname", () => {
expect(matchPath("/users", "/users")).toMatchObject({
pathname: "/users"
});
});

it("matches a pathname with multiple segments", () => {
expect(matchPath("/users/mj", "/users/mj")).toMatchObject({
pathname: "/users/mj"
});
});

it("matches a pathname with a trailing slash", () => {
expect(matchPath("/users", "/users/")).toMatchObject({
pathname: "/users/"
});
});

it("matches a pathname with multiple segments and a trailing slash", () => {
expect(matchPath("/users/mj", "/users/mj/")).toMatchObject({
pathname: "/users/mj/"
});
});
});

it("matches a pathname with a trailing slash", () => {
let match = matchPath("/users", "/users/")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/users/",
pathnameStart: "/users/"
describe("when the pattern has a trailing slash", () => {
it("fails to match a pathname that does not match", () => {
expect(matchPath("users/", "/usersblah")).toBeNull();
});

it("matches a pathname", () => {
expect(matchPath("users/", "/users")).toMatchObject({
pathname: "/users"
});
});

it("matches a pathname with multiple segments", () => {
expect(matchPath("users/mj/", "/users/mj")).toMatchObject({
pathname: "/users/mj"
});
});

it("matches a pathname with a trailing slash", () => {
expect(matchPath("users/", "/users/")).toMatchObject({
pathname: "/users/"
});
});

it("matches a pathname with multiple segments and a trailing slash", () => {
expect(matchPath("users/mj/", "/users/mj/")).toMatchObject({
pathname: "/users/mj/"
});
});
});

it("matches a pathname with multiple segments and a trailing slash", () => {
let match = matchPath("/users/mj", "/users/mj/")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/users/mj/",
pathnameStart: "/users/mj/"
describe("with { end: false }", () => {
it("matches the beginning of a pathname", () => {
expect(matchPath({ path: "/users", end: false }, "/users")).toMatchObject(
{ pathname: "/users" }
);
});

it("matches the beginning of a pathname with multiple segments", () => {
expect(
matchPath({ path: "/users", end: false }, "/users/mj")
).toMatchObject({ pathname: "/users" });
});

it("fails to match a pathname where the segments do not match", () => {
expect(matchPath({ path: "/users", end: false }, "/")).toBeNull();
expect(matchPath({ path: "/users", end: false }, "/users2")).toBeNull();
expect(
matchPath({ path: "/users/mj", end: false }, "/users/mj2")
).toBeNull();
});
});

describe("with a / pattern and { end: false }", () => {
describe("with { end: false } and a / pattern", () => {
it("matches a pathname", () => {
let match = matchPath({ path: "/", end: false }, "/users")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/",
pathnameStart: "/"
expect(matchPath({ path: "/", end: false }, "/users")).toMatchObject({
pathname: "/"
});
});

it("matches a pathname with multiple segments", () => {
let match = matchPath({ path: "/", end: false }, "/users/mj")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/",
pathnameStart: "/"
expect(matchPath({ path: "/", end: false }, "/users/mj")).toMatchObject({
pathname: "/"
});
});

it("matches a pathname with a trailing slash", () => {
let match = matchPath({ path: "/", end: false }, "/users/")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/",
pathnameStart: "/"
expect(matchPath({ path: "/", end: false }, "/users/")).toMatchObject({
pathname: "/"
});
});

it("matches a pathname with multiple segments and a trailing slash", () => {
let match = matchPath({ path: "/", end: false }, "/users/mj/")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/",
pathnameStart: "/"
expect(matchPath({ path: "/", end: false }, "/users/mj/")).toMatchObject({
pathname: "/"
});
});
});

it("is not case-sensitive by default", () => {
let match = matchPath({ path: "/SystemDashboard" }, "/systemdashboard")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/systemdashboard",
pathnameStart: "/systemdashboard"
expect(matchPath("/SystemDashboard", "/systemdashboard")).toMatchObject({
pathname: "/systemdashboard"
});
});

it("matches a case-sensitive pathname", () => {
let match = matchPath(
{ path: "/SystemDashboard", caseSensitive: true },
"/SystemDashboard"
)!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
pathname: "/SystemDashboard",
pathnameStart: "/SystemDashboard"
expect(
matchPath(
{ path: "/SystemDashboard", caseSensitive: true },
"/SystemDashboard"
)
).toMatchObject({
pathname: "/SystemDashboard"
});
});

it("does not match a case-sensitive pathname with the wrong case", () => {
let match = matchPath(
{ path: "/SystemDashboard", caseSensitive: true },
"/systemDashboard"
);
expect(match).toBeNull();
expect(
matchPath(
{ path: "/SystemDashboard", caseSensitive: true },
"/systemDashboard"
)
).toBeNull();
});

describe("when the pattern has a trailing *", () => {
it("matches the remaining portion of the pathname", () => {
let match = matchPath("/files/*", "/files/mj.jpg")!;
expect(match).not.toBeNull();
expect(match).toMatchObject({
params: { "*": "mj.jpg" },
pathname: "/files/mj.jpg",
pathnameStart: "/files"
expect(matchPath("/files*", "/files/mj.jpg")).toMatchObject({
params: { "*": "/mj.jpg" },
pathname: "/files/mj.jpg"
});
expect(matchPath("/files*", "/files/")).toMatchObject({
params: { "*": "/" },
pathname: "/files/"
});
expect(matchPath("/files*", "/files")).toMatchObject({
params: { "*": "" },
pathname: "/files"
});
});
});

it("matches only after a slash", () => {
let match = matchPath("/files/*", "/filestypo");
expect(match).toBeNull();
describe("when the pattern has a trailing /*", () => {
it("matches the remaining portion of the pathname", () => {
expect(matchPath("/files/*", "/files/mj.jpg")).toMatchObject({
params: { "*": "mj.jpg" },
pathname: "/files/mj.jpg"
});
expect(matchPath("/files/*", "/files/")).toMatchObject({
params: { "*": "" },
pathname: "/files/"
});
expect(matchPath("/files/*", "/files")).toMatchObject({
params: { "*": "" },
pathname: "/files"
});
});
});
});
27 changes: 12 additions & 15 deletions packages/react-router/__tests__/path-matching-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ describe("path matching with splats", () => {
expect(match).not.toBeNull();
expect(match[0]).toMatchObject({
params: { id: "mj", "*": "secrets.txt" },
pathname: "/users/mj/files/secrets.txt",
pathnameStart: "/users/mj/files"
pathname: "/users/mj/files/secrets.txt"
});
});

Expand All @@ -199,10 +198,13 @@ describe("path matching with splats", () => {

test("splat after something other than /", () => {
let routes = [{ path: "users/:id/files-*" }];
let match = matchRoutes(routes, "/users/mj/files-secrets.txt");
let match = matchRoutes(routes, "/users/mj/files-secrets.txt")!;

// TODO: Maybe throw an error if the path ends with * but not /*?
expect(match).toBeNull();
expect(match).not.toBeNull();
expect(match[0]).toMatchObject({
params: { id: "mj", "*": "secrets.txt" },
pathname: "/users/mj/files-secrets.txt"
});
});

test("parent route with splat after /", () => {
Expand All @@ -214,13 +216,11 @@ describe("path matching with splats", () => {
expect(match).not.toBeNull();
expect(match[0]).toMatchObject({
params: { id: "mj", "*": "secrets.txt" },
pathname: "/users/mj/files/secrets.txt",
pathnameStart: "/users/mj/files"
pathname: "/users/mj/files/secrets.txt"
});
expect(match[1]).toMatchObject({
params: { id: "mj", "*": "secrets.txt" },
pathname: "/users/mj/files/secrets.txt",
pathnameStart: "/users/mj/files/secrets.txt"
pathname: "/users/mj/files/secrets.txt"
});
});

Expand All @@ -233,18 +233,15 @@ describe("path matching with splats", () => {
expect(match).not.toBeNull();
expect(match[0]).toMatchObject({
params: { "*": "one/two/three" },
pathname: "/one/two/three",
pathnameStart: "/"
pathname: "/one/two/three"
});
expect(match[1]).toMatchObject({
params: { "*": "one/two/three" },
pathname: "/one/two/three",
pathnameStart: "/"
pathname: "/one/two/three"
});
expect(match[2]).toMatchObject({
params: { "*": "one/two/three" },
pathname: "/one/two/three",
pathnameStart: "/"
pathname: "/one/two/three"
});
});
});
Loading

0 comments on commit 4bf20d7

Please sign in to comment.