11import { describe , it , expect } from "vitest" ;
22import { createRouter , formatTree } from "./_utils.ts" ;
33import { findRoute , removeRoute } from "../src/index.ts" ;
4+ import { compileRouter } from "../src/compiler.ts" ;
45
5- describe ( "Basic router " , ( ) => {
6+ describe ( "route matching " , ( ) => {
67 const router = createRouter ( [
78 "/test" ,
89 "/test/:id" ,
@@ -18,6 +19,8 @@ describe("Basic router", () => {
1819 "/wildcard/**" ,
1920 ] ) ;
2021
22+ const compiledLookup = compileRouter ( router ) ;
23+
2124 it ( "snapshot" , ( ) => {
2225 expect ( formatTree ( router . root ) ) . toMatchInlineSnapshot ( `
2326 "<root>
@@ -39,55 +42,68 @@ describe("Basic router", () => {
3942 ` ) ;
4043 } ) ;
4144
42- it ( "lookup works" , ( ) => {
43- // Static
44- expect ( findRoute ( router , "GET" , "/test" ) ) . toEqual ( {
45- data : { path : "/test" } ,
46- } ) ;
47- expect ( findRoute ( router , "GET" , "/test/foo" ) ) . toEqual ( {
48- data : { path : "/test/foo" } ,
49- } ) ;
50- expect ( findRoute ( router , "GET" , "/test/fooo" ) ) . toEqual ( {
51- data : { path : "/test/fooo" } ,
52- } ) ;
53- expect ( findRoute ( router , "GET" , "/another/path" ) ) . toEqual ( {
54- data : { path : "/another/path" } ,
55- } ) ;
56- // Param
57- expect ( findRoute ( router , "GET" , "/test/123" ) ) . toEqual ( {
58- data : { path : "/test/:id" } ,
59- params : { id : "123" } ,
60- } ) ;
61- expect ( findRoute ( router , "GET" , "/test/123/y" ) ) . toEqual ( {
62- data : { path : "/test/:idY/y" } ,
63- params : { idY : "123" } ,
64- } ) ;
65- expect ( findRoute ( router , "GET" , "/test/123/y/z" ) ) . toEqual ( {
66- data : { path : "/test/:idYZ/y/z" } ,
67- params : { idYZ : "123" } ,
68- } ) ;
69- expect ( findRoute ( router , "GET" , "/test/foo/123" ) ) . toEqual ( {
70- data : { path : "/test/foo/*" } ,
71- params : { _0 : "123" } ,
72- } ) ;
73- // Wildcard
74- expect ( findRoute ( router , "GET" , "/test/foo/123/456" ) ) . toEqual ( {
75- data : { path : "/test/foo/**" } ,
76- params : { _ : "123/456" } ,
77- } ) ;
78- expect ( findRoute ( router , "GET" , "/wildcard/foo" ) ) . toEqual ( {
79- data : { path : "/wildcard/**" } ,
80- params : { _ : "foo" } ,
81- } ) ;
82- expect ( findRoute ( router , "GET" , "/wildcard/foo/bar" ) ) . toEqual ( {
83- data : { path : "/wildcard/**" } ,
84- params : { _ : "foo/bar" } ,
85- } ) ;
86- expect ( findRoute ( router , "GET" , "/wildcard" ) ) . toEqual ( {
87- data : { path : "/wildcard/**" } ,
88- params : { _ : "" } ,
45+ const lookups = [
46+ {
47+ name : "findRoute" ,
48+ match : ( method : string , path : string ) => findRoute ( router , method , path ) ,
49+ } ,
50+ {
51+ name : "compiledLookup" ,
52+ match : ( method : string , path : string ) => compiledLookup ( method , path ) ,
53+ } ,
54+ ] ;
55+
56+ for ( const { name, match } of lookups ) {
57+ it ( `match with ${ name } ` , ( ) => {
58+ // Static
59+ expect ( match ( "GET" , "/test" ) ) . toEqual ( {
60+ data : { path : "/test" } ,
61+ } ) ;
62+ expect ( match ( "GET" , "/test/foo" ) ) . toEqual ( {
63+ data : { path : "/test/foo" } ,
64+ } ) ;
65+ expect ( match ( "GET" , "/test/fooo" ) ) . toEqual ( {
66+ data : { path : "/test/fooo" } ,
67+ } ) ;
68+ expect ( match ( "GET" , "/another/path" ) ) . toEqual ( {
69+ data : { path : "/another/path" } ,
70+ } ) ;
71+ // Param
72+ expect ( match ( "GET" , "/test/123" ) ) . toEqual ( {
73+ data : { path : "/test/:id" } ,
74+ params : { id : "123" } ,
75+ } ) ;
76+ expect ( match ( "GET" , "/test/123/y" ) ) . toEqual ( {
77+ data : { path : "/test/:idY/y" } ,
78+ params : { idY : "123" } ,
79+ } ) ;
80+ expect ( match ( "GET" , "/test/123/y/z" ) ) . toEqual ( {
81+ data : { path : "/test/:idYZ/y/z" } ,
82+ params : { idYZ : "123" } ,
83+ } ) ;
84+ expect ( match ( "GET" , "/test/foo/123" ) ) . toEqual ( {
85+ data : { path : "/test/foo/*" } ,
86+ params : { _0 : "123" } ,
87+ } ) ;
88+ // Wildcard
89+ expect ( match ( "GET" , "/test/foo/123/456" ) ) . toEqual ( {
90+ data : { path : "/test/foo/**" } ,
91+ params : { _ : "123/456" } ,
92+ } ) ;
93+ expect ( match ( "GET" , "/wildcard/foo" ) ) . toEqual ( {
94+ data : { path : "/wildcard/**" } ,
95+ params : { _ : "foo" } ,
96+ } ) ;
97+ expect ( match ( "GET" , "/wildcard/foo/bar" ) ) . toEqual ( {
98+ data : { path : "/wildcard/**" } ,
99+ params : { _ : "foo/bar" } ,
100+ } ) ;
101+ expect ( match ( "GET" , "/wildcard" ) ) . toEqual ( {
102+ data : { path : "/wildcard/**" } ,
103+ params : { _ : "" } ,
104+ } ) ;
89105 } ) ;
90- } ) ;
106+ }
91107
92108 it ( "remove works" , ( ) => {
93109 removeRoute ( router , "GET" , "/test" ) ;
0 commit comments