Skip to content

Commit

Permalink
test: update matcher tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 8, 2024
1 parent 8a643ac commit c81d596
Showing 1 changed file with 109 additions and 49 deletions.
158 changes: 109 additions & 49 deletions tests/matcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
import { describe, it, expect } from "vitest";
import { createRouter, formatTree } from "./_utils";
import { matchAllRoutes } from "../src";
import { matchAllRoutes, type RouterContext } from "../src";

describe("readme example", () => {
const router = createRouter({
"/foo": { m: "foo" },
"/foo/**": { m: "foo/**", order: "2" },
"/foo/bar": { m: "foo/bar" },
"/foo/bar/baz": { m: "foo/bar/baz", order: "4" },
"/foo/*/baz": { m: "foo/*/baz", order: "3" },
"/**": { m: "/**", order: "1" },
});
// Helper to make snapsots more readable
const _matchAllRoutes = (
ctx: RouterContext,
method: string = "",
path: string,
) => matchAllRoutes(ctx, method, path).map((m: any) => m.path);

describe("matcher: basic", () => {
const router = createRouter([
"/foo",
"/foo/**",
"/foo/bar",
"/foo/bar/baz",
"/foo/*/baz",
"/**",
]);

it("snapshot", () => {
expect(formatTree(router.root)).toMatchInlineSnapshot(`
"<root>
├── /foo ┈> [GET] {"m":"foo"}
│ ├── /bar ┈> [GET] {"m":"foo/bar"}
│ │ ├── /baz ┈> [GET] {"m":"foo/bar/baz","order":"4"}
├── /foo ┈> [GET] /foo
│ ├── /bar ┈> [GET] /foo/bar
│ │ ├── /baz ┈> [GET] /foo/bar/baz
│ ├── /*
│ │ ├── /baz ┈> [GET] {"m":"foo/*/baz","order":"3"}
│ ├── /** ┈> [GET] {"m":"foo/**","order":"2"}
├── /** ┈> [GET] {"m":"/**","order":"1"}"
│ │ ├── /baz ┈> [GET] /foo/*/baz
│ ├── /** ┈> [GET] /foo/**
├── /** ┈> [GET] /**"
`);
});

it("matches /foo/bar/baz pattern", () => {
const matches = matchAllRoutes(router, "", "/foo/bar/baz");
expect(matches).to.toMatchInlineSnapshot(`[]`);
const matches = _matchAllRoutes(router, "GET", "/foo/bar/baz");
expect(matches).to.toMatchInlineSnapshot(`
[
"/**",
"/foo/**",
"/foo/*/baz",
"/foo/bar/baz",
]
`);
});
});

describe("route matcher", () => {
describe("matcher: complex", () => {
const router = createRouter([
"/",
"/foo",
Expand Down Expand Up @@ -66,49 +80,95 @@ describe("route matcher", () => {
});

it("can match routes", () => {
expect(matchAllRoutes(router, "", "/")).to.toMatchInlineSnapshot(`[]`);
expect(matchAllRoutes(router, "", "/foo")).to.toMatchInlineSnapshot(`[]`);
expect(matchAllRoutes(router, "", "/foo/bar")).to.toMatchInlineSnapshot(
`[]`,
);
expect(matchAllRoutes(router, "", "/foo/baz")).to.toMatchInlineSnapshot(
`[]`,
);
expect(matchAllRoutes(router, "", "/foo/123/sub")).to.toMatchInlineSnapshot(
`[]`,
);
expect(matchAllRoutes(router, "", "/foo/123")).to.toMatchInlineSnapshot(
`[]`,
);
expect(_matchAllRoutes(router, "GET", "/")).to.toMatchInlineSnapshot(`
[
"/",
]
`);
expect(_matchAllRoutes(router, "GET", "/foo")).to.toMatchInlineSnapshot(`
[
"/foo/**",
"/foo",
]
`);
expect(_matchAllRoutes(router, "GET", "/foo/bar")).to
.toMatchInlineSnapshot(`
[
"/foo/**",
"/foo/*",
"/foo/bar",
]
`);
expect(_matchAllRoutes(router, "GET", "/foo/baz")).to
.toMatchInlineSnapshot(`
[
"/foo/**",
"/foo/*",
"/foo/baz/**",
"/foo/baz",
]
`);
expect(_matchAllRoutes(router, "GET", "/foo/123/sub")).to
.toMatchInlineSnapshot(`
[
"/foo/**",
"/foo/*/sub",
]
`);
expect(_matchAllRoutes(router, "GET", "/foo/123")).to
.toMatchInlineSnapshot(`
[
"/foo/**",
"/foo/*",
]
`);
});

it("trailing slash", () => {
// Defined with trailing slash
expect(
matchAllRoutes(router, "", "/with-trailing"),
).to.toMatchInlineSnapshot(`[]`);
expect(matchAllRoutes(router, "", "/with-trailing")).toMatchObject(
matchAllRoutes(router, "", "/with-trailing/"),
expect(_matchAllRoutes(router, "GET", "/with-trailing")).to
.toMatchInlineSnapshot(`
[
"/with-trailing/",
]
`);
expect(_matchAllRoutes(router, "GET", "/with-trailing")).toMatchObject(
_matchAllRoutes(router, "GET", "/with-trailing/"),
);

// Defined without trailing slash
expect(
matchAllRoutes(router, "", "/without-trailing"),
).to.toMatchInlineSnapshot(`[]`);
expect(matchAllRoutes(router, "", "/without-trailing")).toMatchObject(
matchAllRoutes(router, "", "/without-trailing/"),
expect(_matchAllRoutes(router, "GET", "/without-trailing")).to
.toMatchInlineSnapshot(`
[
"/without-trailing",
]
`);
expect(_matchAllRoutes(router, "GET", "/without-trailing")).toMatchObject(
_matchAllRoutes(router, "GET", "/without-trailing/"),
);
});

it("prefix overlap", () => {
expect(matchAllRoutes(router, "", "/c/123")).to.toMatchInlineSnapshot(`[]`);
expect(matchAllRoutes(router, "", "/c/123")).toMatchObject(
matchAllRoutes(router, "", "/c/123/"),
expect(_matchAllRoutes(router, "GET", "/c/123")).to.toMatchInlineSnapshot(
`
[
"/c/**",
]
`,
);
expect(matchAllRoutes(router, "", "/c/123")).toMatchObject(
matchAllRoutes(router, "", "/c"),
expect(_matchAllRoutes(router, "GET", "/c/123")).toMatchObject(
_matchAllRoutes(router, "GET", "/c/123/"),
);
expect(_matchAllRoutes(router, "GET", "/c/123")).toMatchObject(
_matchAllRoutes(router, "GET", "/c"),
);

expect(matchAllRoutes(router, "", "/cart")).to.toMatchInlineSnapshot(`[]`);
expect(_matchAllRoutes(router, "GET", "/cart")).to.toMatchInlineSnapshot(
`
[
"/cart",
]
`,
);
});
});

0 comments on commit c81d596

Please sign in to comment.