Skip to content

Commit

Permalink
improved tests to check that found handle is correct
Browse files Browse the repository at this point in the history
  • Loading branch information
maybephilipp committed Jun 13, 2024
1 parent f14b529 commit 6314db6
Showing 1 changed file with 126 additions and 60 deletions.
186 changes: 126 additions & 60 deletions __tests__/tree-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,96 +105,162 @@ describe("Add and get", () => {
describe("Wildcard", () => {
const tree = new Tree();
const routes = [
"/",
"/cmd/foo/bar",
"/cmd/:tool/",
"/cmd/:tool/:sub",
"/src/*filepath",
"/search/",
"/search/:query",
"/user_:name",
"/user_:name/about",
"/files/:dir/*filepath",
"/doc/",
"/doc/node_faq.html",
"/doc/node1.html",
"/info/:user/public",
"/info/:user/project/:project"
];

routes.forEach(route => {
tree.addRoute(route, noOp);
});

// tree.printTree();

const foundData = [
{
route: "/",
params: []
found: [
{
route: "/",
params: []
},
],
},
{
route: "/cmd/foo/bar",
params: []
found: [
{
route: "/cmd/foo/bar",
params: []
},
],
},
{
route: "/cmd/test/",
params: [{ key: "tool", value: "test" }]
route: "/cmd/a_:tool",
found: [
{
route: "/cmd/a_test",
params: [{ key: "tool", value: "test" }]
},
],
},
{
route: "/cmd/test/3",
params: [{ key: "tool", value: "test" }, { key: "sub", value: "3" }]
route: "/cmd/:tool/",
found: [
{
route: "/cmd/test/",
params: [{ key: "tool", value: "test" }]
},
],
},
{
route: "/src/",
params: [{ key: "filepath", value: "/" }]
route: "/cmd/:tool/:sub",
found: [
{
route: "/cmd/test/3",
params: [{ key: "tool", value: "test" }, { key: "sub", value: "3" }]
},
],
},
{
route: "/src/some/file.png",
params: [{ key: "filepath", value: "/some/file.png" }]
route: "/src/*filepath",
found: [
{
route: "/src/",
params: [{ key: "filepath", value: "/" }]
},
{
route: "/src/some/file.png",
params: [{ key: "filepath", value: "/some/file.png" }]
},
],
},
{
route: "/search/",
params: []
found: [
{
route: "/search/",
params: []
},
],
},
{
route: "/search/中文",
params: [{ key: "query", value: "中文" }]
route: "/search/:query",
found: [
{
route: "/search/中文",
params: [{ key: "query", value: "中文" }]
},
],
},
{
route: "/user_noder",
params: [{ key: "name", value: "noder" }]
route: "/user_:name",
found: [
{
route: "/user_noder",
params: [{ key: "name", value: "noder" }]
},
],
},
{
route: "/user_noder/about",
params: [{ key: "name", value: "noder" }]
route: "/user_:name/about",
found: [
{
route: "/user_noder/about",
params: [{ key: "name", value: "noder" }]
},
],
},
{
route: "/files/js/inc/framework.js",
params: [
{ key: "dir", value: "js" },
{ key: "filepath", value: "/inc/framework.js" }
]
route: "/files/:dir/*filepath",
found: [
{
route: "/files/js/inc/framework.js",
params: [
{ key: "dir", value: "js" },
{ key: "filepath", value: "/inc/framework.js" }
]
},
],
},
{
route: "/info/gordon/public",
params: [{ key: "user", value: "gordon" }]
route: "/doc/",
found: [],
},
{
route: "/info/gordon/project/node",
params: [
{ key: "user", value: "gordon" },
{ key: "project", value: "node" }
]
}
route: "/doc/node_faq.html",
found: [],
},
{
route: "/doc/node1.html",
found: [],
},
{
route: "/info/:user/public",
found: [
{
route: "/info/gordon/public",
params: [{ key: "user", value: "gordon" }]
},
],
},
{
route: "/info/:user/project/:project",
found: [
{
route: "/info/gordon/project/node",
params: [
{ key: "user", value: "gordon" },
{ key: "project", value: "node" }
]
},
],
},
];

foundData.forEach(data => {
it(data.route, () => {
const { handle, params } = tree.search(data.route);
expect(handle).toBeTruthy();
expect(params).toMatchObject(data.params);
});
routes.forEach((route) => {
route.handle = noOp.slice(0) // creating an unique handle
tree.addRoute(route.route, route.handle);
});

// tree.printTree();

routes.forEach((route) => {
route.found.forEach((data) => {
it(data.route, () => {
const { handle, params } = tree.search(data.route);
expect(handle).toBe(route.handle);
expect(params).toMatchObject(data.params);
});
})
});

const noHandlerData = [
Expand Down

0 comments on commit 6314db6

Please sign in to comment.