]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test(parser): stricter checkPathOrder
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 15 Dec 2019 18:41:58 +0000 (19:41 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 18 Dec 2019 09:26:15 +0000 (10:26 +0100)
__tests__/matcher/path-ranking.spec.ts

index 556ae2a217c0ad1693e0dda80cb465fff693f3d7..4442036659a52504f2d627a3f4b867df5cc5508e 100644 (file)
@@ -21,40 +21,68 @@ describe('Path ranking', () => {
     })
   })
 
+  const possibleOptions: PathParserOptions[] = [
+    undefined,
+    { strict: true, sensitive: false },
+    { strict: false, sensitive: true },
+    { strict: true, sensitive: true },
+  ]
+
   function checkPathOrder(paths: Array<string | [string, PathParserOptions]>) {
-    const pathsAsStrings = paths.map(path =>
-      typeof path === 'string' ? path : path[0] + JSON.stringify(path[1])
-    )
-    // reverse the array to force some reordering
-    const parsers = paths.reverse().map(path => {
-      const parser =
-        typeof path === 'string'
-          ? tokensToParser(tokenizePath(path))
-          : tokensToParser(tokenizePath(path[0]), path[1])
-      // @ts-ignore
-      parser.path =
-        typeof path === 'string' ? path : path[0] + JSON.stringify(path[1])
-      return parser
+    const normalizedPaths = paths.map(pathOrArray => {
+      let path: string
+      let options: PathParserOptions
+      if (typeof pathOrArray === 'string') {
+        path = pathOrArray
+      } else {
+        path = pathOrArray[0]
+        options = pathOrArray[1]
+      }
+
+      return {
+        id: path + (options ? JSON.stringify(options) : ''),
+        path,
+        options,
+      }
     })
 
+    // reverse the array to force some reordering
+    const parsers = normalizedPaths
+      .slice()
+      .reverse()
+      .map(({ id, path, options }) => ({
+        ...tokensToParser(tokenizePath(path), options),
+        id,
+      }))
+
     parsers.sort((a, b) => comparePathParserScore(a.score, b.score))
 
-    try {
-      expect(
-        parsers.map(
-          parser =>
-            // @ts-ignore
-            parser.path
+    for (let i = 0; i < parsers.length - 1; i++) {
+      const a = parsers[i]
+      const b = parsers[i + 1]
+
+      try {
+        expect(a.score).not.toEqual(b.score)
+      } catch (err) {
+        console.warn(
+          'Different routes should not have the same score:\n' +
+            `${a.id} -> [${a.score.join(', ')}]\n${b.id} -> [${b.score.join(
+              ', '
+            )}]`
         )
-      ).toEqual(pathsAsStrings)
+
+        throw err
+      }
+    }
+
+    try {
+      expect(parsers.map(parser => parser.id)).toEqual(
+        normalizedPaths.map(path => path.id)
+      )
     } catch (err) {
-      console.log(
+      console.warn(
         parsers
-          .map(
-            parser =>
-              // @ts-ignore
-              `${parser.path} -> [${parser.score.join(', ')}]`
-          )
+          .map(parser => `${parser.id} -> [${parser.score.join(', ')}]`)
           .join('\n')
       )
       throw err
@@ -96,13 +124,6 @@ describe('Path ranking', () => {
   })
 
   it('puts the wildcard at the end', () => {
-    const possibleOptions: PathParserOptions[] = [
-      {},
-      { strict: true, sensitive: false },
-      { strict: false, sensitive: true },
-      { strict: true, sensitive: true },
-    ]
-
     possibleOptions.forEach(options => {
       checkPathOrder([['', options], '/:rest(.*)'])
       checkPathOrder([['/', options], '/:rest(.*)'])