diff --git a/__tests__/tree-spec.js b/__tests__/tree-spec.js index c5a6649..db9ad06 100644 --- a/__tests__/tree-spec.js +++ b/__tests__/tree-spec.js @@ -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 = [