1
1
import { exec } from '../router.js' ;
2
2
3
+ function execPath ( path , pattern ) {
4
+ return exec ( path , pattern , { path } ) ;
5
+ }
6
+
3
7
describe ( 'match' , ( ) => {
4
8
it ( 'Base route' , ( ) => {
5
- const accurateResult = exec ( '/' , '/' , { path : '/' } ) ;
9
+ const accurateResult = execPath ( '/' , '/' ) ;
6
10
expect ( accurateResult ) . toEqual ( { path : '/' , params : { } } ) ;
7
11
8
- const inaccurateResult = exec ( '/user/1' , '/' , { path : '/' } ) ;
12
+ const inaccurateResult = execPath ( '/user/1' , '/' ) ;
9
13
expect ( inaccurateResult ) . toEqual ( undefined ) ;
10
14
} ) ;
11
15
12
16
it ( 'Param route' , ( ) => {
13
- const accurateResult = exec ( '/user/2' , '/user/:id' , { path : '/' } ) ;
14
- expect ( accurateResult ) . toEqual ( { path : '/' , params : { id : '2' } } ) ;
17
+ const accurateResult = execPath ( '/user/2' , '/user/:id' ) ;
18
+ expect ( accurateResult ) . toEqual ( { path : '/user/2 ' , params : { id : '2' } } ) ;
15
19
16
- const inaccurateResult = exec ( '/' , '/user/:id' , { path : '/' } ) ;
20
+ const inaccurateResult = execPath ( '/' , '/user/:id' ) ;
17
21
expect ( inaccurateResult ) . toEqual ( undefined ) ;
18
22
} ) ;
19
23
20
24
it ( 'Optional param route' , ( ) => {
21
- const accurateResult = exec ( '/user' , '/user/:id?' , { path : '/' } ) ;
22
- expect ( accurateResult ) . toEqual ( { path : '/' , params : { id : undefined } } ) ;
25
+ const accurateResult = execPath ( '/user' , '/user/:id?' ) ;
26
+ expect ( accurateResult ) . toEqual ( { path : '/user' , params : { id : undefined } } ) ;
27
+
28
+ const inaccurateResult = execPath ( '/' , '/user/:id?' ) ;
29
+ expect ( inaccurateResult ) . toEqual ( undefined ) ;
30
+ } ) ;
31
+
32
+ it ( 'Optional rest param route "/:x*"' , ( ) => {
33
+ const accurateResult = execPath ( '/user' , '/user/:id?' ) ;
34
+ expect ( accurateResult ) . toEqual ( { path : '/user' , params : { id : undefined } } ) ;
23
35
24
- const inaccurateResult = exec ( '/' , '/user/:id?' , { path : '/' } ) ;
36
+ const inaccurateResult = execPath ( '/' , '/user/:id?' ) ;
25
37
expect ( inaccurateResult ) . toEqual ( undefined ) ;
26
38
} ) ;
27
39
40
+ it ( 'Rest param route "/:x+"' , ( ) => {
41
+ const matchedResult = execPath ( '/user/foo' , '/user/:id+' ) ;
42
+ expect ( matchedResult ) . toEqual ( { path : '/user/foo' , params : { id : 'foo' } } ) ;
43
+
44
+ const matchedResultWithSlash = execPath ( '/user/foo/bar' , '/user/:id+' ) ;
45
+ expect ( matchedResultWithSlash ) . toEqual ( { path : '/user/foo/bar' , params : { id : 'foo/bar' } } ) ;
46
+
47
+ const emptyResult = execPath ( '/user' , '/user/:id+' ) ;
48
+ expect ( emptyResult ) . toEqual ( undefined ) ;
49
+
50
+ const mismatchedResult = execPath ( '/' , '/user/:id+' ) ;
51
+ expect ( mismatchedResult ) . toEqual ( undefined ) ;
52
+ } ) ;
53
+
28
54
it ( 'Handles leading/trailing slashes' , ( ) => {
29
- const result = exec ( '/about-late/_SEGMENT1_/_SEGMENT2_/' , '/about-late/:seg1/:seg2/' , { } ) ;
55
+ const result = execPath ( '/about-late/_SEGMENT1_/_SEGMENT2_/' , '/about-late/:seg1/:seg2/' ) ;
30
56
expect ( result ) . toEqual ( {
57
+ path : '/about-late/_SEGMENT1_/_SEGMENT2_/' ,
31
58
params : {
32
59
seg1 : '_SEGMENT1_' ,
33
60
seg2 : '_SEGMENT2_'
@@ -36,10 +63,10 @@ describe('match', () => {
36
63
} ) ;
37
64
38
65
it ( 'should not overwrite existing properties' , ( ) => {
39
- const result = exec ( '/foo/bar' , '/:path/:query' , { path : '/' } ) ;
66
+ const result = exec ( '/foo/bar' , '/:path/:query' , { path : '/custom-path ' } ) ;
40
67
expect ( result ) . toEqual ( {
41
68
params : { path : 'foo' , query : 'bar' } ,
42
- path : '/'
69
+ path : '/custom-path '
43
70
} ) ;
44
71
} ) ;
45
72
} ) ;
0 commit comments