]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test(parser): fix tests
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 14 Dec 2019 16:01:36 +0000 (17:01 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 18 Dec 2019 09:26:15 +0000 (10:26 +0100)
__tests__/matcher/path-parser.spec.ts

index 631a75b3b58d418934cbe6a3aa73e7c87458e4bf..d0c6573a40ebf0a18d65c06802b1f6a9ae5525b0 100644 (file)
@@ -317,6 +317,7 @@ describe('Path parser', () => {
         ],
       ])
     })
+    // end of describe token
   })
 
   describe('tokensToParser', () => {
@@ -390,7 +391,7 @@ describe('Path parser', () => {
     })
 
     it('param*', () => {
-      matchRegExp('^/((?:\\d+)(?:/(?:\\d+))*)?$', [
+      matchRegExp('^(?:/((?:\\d+)(?:/(?:\\d+))*))?$', [
         [
           {
             type: TokenType.Param,
@@ -404,7 +405,27 @@ describe('Path parser', () => {
     })
 
     it('param?', () => {
-      matchRegExp('^/(\\d+)?$', [
+      matchRegExp('^(?:/(\\d+))?$', [
+        [
+          {
+            type: TokenType.Param,
+            value: 'id',
+            regexp: '\\d+',
+            repeatable: false,
+            optional: true,
+          },
+        ],
+      ])
+    })
+
+    it('static and param?', () => {
+      matchRegExp('^/ab(?:/(\\d+))?$', [
+        [
+          {
+            type: TokenType.Static,
+            value: 'ab',
+          },
+        ],
         [
           {
             type: TokenType.Param,
@@ -430,6 +451,26 @@ describe('Path parser', () => {
         ],
       ])
     })
+
+    it('static and param+', () => {
+      matchRegExp('^/ab/((?:\\d+)(?:/(?:\\d+))*)$', [
+        [
+          {
+            type: TokenType.Static,
+            value: 'ab',
+          },
+        ],
+        [
+          {
+            type: TokenType.Param,
+            value: 'id',
+            regexp: '\\d+',
+            repeatable: true,
+            optional: false,
+          },
+        ],
+      ])
+    })
     // end of describe
   })
 
@@ -547,8 +588,9 @@ describe('Path parser', () => {
 
     it('param optional followed by static', () => {
       matchParams('/:a?/one', '/two/one', { a: 'two' })
-      // the second one is never matched
-      matchParams('/:a?/one', '/one', null)
+      // since the first one is optional
+      matchParams('/:a?/one', '/one', { a: '' })
+      matchParams('/:a?/one', '/two', null)
       // can only match one time
       matchParams('/:a?/one', '/two/three/one', null)
       matchParams('/:a*/one', '/two/one', { a: ['two'] })
@@ -588,9 +630,10 @@ describe('Path parser', () => {
         ReturnType<ReturnType<typeof tokensToParser>['parse']>,
         null
       >,
-      expectedUrl: string
+      expectedUrl: string,
+      options?: Parameters<typeof tokensToParser>[1]
     ) {
-      const pathParser = tokensToParser(tokenizePath(path))
+      const pathParser = tokensToParser(tokenizePath(path), options)
 
       expect(pathParser.stringify(params)).toEqual(expectedUrl)
     }
@@ -599,6 +642,11 @@ describe('Path parser', () => {
       matchStringify('/home', {}, '/home')
     })
 
+    it('works with trailing slash', () => {
+      matchStringify('/home/', {}, '/home/')
+      matchStringify('/home/', {}, '/home/', { strict: true })
+    })
+
     it('single param one segment', () => {
       matchStringify('/:id', { id: 'one' }, '/one')
     })