]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: add tests for aliases
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 13 Mar 2020 16:25:15 +0000 (17:25 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 13 Mar 2020 16:25:15 +0000 (17:25 +0100)
__tests__/matcher/__snapshots__/resolve.spec.ts.snap
__tests__/matcher/resolve.spec.ts

index 1c56326491244c6275cbe876aea76494a0bb0259..4592f7d0976e1659e12b16d270710eae8f7fbb02 100644 (file)
@@ -5,14 +5,6 @@ exports[`Router Matcher resolve LocationAsName throws if the named route does no
 {"name":"Home"}]
 `;
 
-exports[`Router Matcher resolve LocationAsRelative redirects throws if relative location when redirecting 1`] = `
-[InvalidRouteMatch: Cannot redirect using a relative location:
-{
-  "params": {}
-}
-Use the function redirect and explicitely provide a name]
-`;
-
 exports[`Router Matcher resolve LocationAsRelative throws if the current named route does not exists 1`] = `
 [NoRouteMatchError: No match for
 {"params":{"a":"foo"}}
index 51fb50a541d9a5e3bec29977bac996db6f0b10aa..5b868b68aa370a73f284043a1ae48ffa058d45fa 100644 (file)
@@ -5,7 +5,6 @@ import {
   RouteRecord,
   MatcherLocation,
   MatcherLocationNormalized,
-  MatcherLocationRedirect,
 } from '../../src/types'
 import { MatcherLocationNormalizedLoose } from '../utils'
 
@@ -125,8 +124,73 @@ describe('Router Matcher', () => {
         )
       })
 
-      it.todo('multiple aliases')
-      it.todo('resolve named child with parent with alias')
+      it('multiple aliases', () => {
+        const record = {
+          path: '/',
+          alias: ['/home', '/start'],
+          name: 'Home',
+          components,
+          meta: { foo: true },
+        }
+
+        assertRecordMatch(
+          record,
+          { path: '/' },
+          {
+            name: 'Home',
+            path: '/',
+            params: {},
+            meta: { foo: true },
+            matched: [
+              {
+                path: '/',
+                name: 'Home',
+                components,
+                aliasOf: undefined,
+                meta: { foo: true },
+              },
+            ],
+          }
+        )
+        assertRecordMatch(
+          record,
+          { path: '/home' },
+          {
+            name: 'Home',
+            path: '/home',
+            params: {},
+            meta: { foo: true },
+            matched: [
+              {
+                path: '/home',
+                name: 'Home',
+                components,
+                aliasOf: expect.objectContaining({ name: 'Home', path: '/' }),
+                meta: { foo: true },
+              },
+            ],
+          }
+        )
+        assertRecordMatch(
+          record,
+          { path: '/start' },
+          {
+            name: 'Home',
+            path: '/start',
+            params: {},
+            meta: { foo: true },
+            matched: [
+              {
+                path: '/start',
+                name: 'Home',
+                components,
+                aliasOf: expect.objectContaining({ name: 'Home', path: '/' }),
+                meta: { foo: true },
+              },
+            ],
+          }
+        )
+      })
 
       it('resolves the original record by name', () => {
         assertRecordMatch(
@@ -757,189 +821,6 @@ describe('Router Matcher', () => {
         )
       })
 
-      // TODO: replace tests with a transformation check to the `beforeEnter` guard
-      describe.skip('redirects', () => {
-        function assertRedirect(
-          records: RouteRecord[],
-          location: MatcherLocation,
-          expected: MatcherLocationNormalized | MatcherLocationRedirect,
-          currentLocation: MatcherLocationNormalized = START_LOCATION_NORMALIZED
-        ) {
-          const matcher = createRouterMatcher(records, {})
-          const resolved = matcher.resolve(location, currentLocation)
-          expect(resolved).toEqual(expected)
-          return resolved
-        }
-
-        it('resolves a redirect string', () => {
-          const records = [
-            { path: '/home', components },
-            { path: '/redirect', redirect: '/home' },
-          ]
-          assertRedirect(
-            records,
-            {
-              name: undefined,
-              path: '/redirect',
-            },
-            {
-              redirect: '/home',
-              normalizedLocation: {
-                path: '/redirect',
-                params: {},
-                name: undefined,
-                matched: [],
-                meta: {},
-              },
-            }
-          )
-        })
-
-        it('resolves a redirect function that returns a string', () => {
-          const redirect = () => '/home'
-          const records = [
-            { path: '/home', components },
-            { path: '/redirect', redirect },
-          ]
-          assertRedirect(
-            records,
-            {
-              name: undefined,
-              path: '/redirect',
-            },
-            {
-              redirect,
-              normalizedLocation: {
-                path: '/redirect',
-                params: {},
-                name: undefined,
-                matched: [],
-                meta: {},
-              },
-            }
-          )
-        })
-
-        it('resolves a redirect function that returns an object route', () => {
-          const redirect = () => {
-            path: '/home'
-          }
-          const records = [
-            { path: '/home', components },
-            { path: '/redirect', redirect },
-          ]
-          assertRedirect(
-            records,
-            {
-              name: undefined,
-              path: '/redirect',
-            },
-            {
-              redirect,
-              normalizedLocation: {
-                path: '/redirect',
-                params: {},
-                name: undefined,
-                matched: [],
-                meta: {},
-              },
-            }
-          )
-        })
-
-        it('resolves a redirect as an object', () => {
-          const records = [
-            { path: '/home', components },
-            { path: '/redirect', redirect: { path: 'home' } },
-          ]
-          assertRedirect(
-            records,
-            {
-              name: undefined,
-              path: '/redirect',
-            },
-            {
-              redirect: { path: 'home' },
-              normalizedLocation: {
-                path: '/redirect',
-                params: {},
-                name: undefined,
-                matched: [],
-                meta: {},
-              },
-            }
-          )
-        })
-
-        it('works with a named location', () => {
-          const records = [
-            { path: '/home', components },
-            { path: '/redirect', name: 'redirect', redirect: { path: 'home' } },
-          ]
-          assertRedirect(
-            records,
-            {
-              name: 'redirect',
-            },
-            {
-              redirect: { path: 'home' },
-              normalizedLocation: {
-                path: '/redirect',
-                params: {},
-                name: 'redirect',
-                matched: [],
-                meta: {},
-              },
-            }
-          )
-        })
-
-        it('throws if relative location when redirecting', () => {
-          expect(
-            assertErrorMatch(
-              { path: '/redirect', redirect: '/home' },
-              { params: {} },
-              {
-                path: '/redirect',
-                params: {},
-                matched: [],
-                name: undefined,
-                meta: {},
-              }
-            )
-          ).toMatchSnapshot()
-        })
-
-        it('normalize a location when redirecting', () => {
-          const redirect = (to: any) => ({ name: 'b', params: to.params })
-          const records = [
-            { path: '/home', components },
-            {
-              path: '/a/:a',
-              name: 'a',
-              redirect,
-            },
-            { path: '/b/:a', name: 'b', components },
-          ]
-          assertRedirect(
-            records,
-            {
-              path: '/a/foo',
-            },
-            {
-              redirect,
-              normalizedLocation: {
-                path: '/a/foo',
-                params: { a: 'foo' },
-                name: 'a',
-                matched: [],
-                meta: {},
-              },
-            }
-          )
-        })
-      })
-
       it('throws if the current named route does not exists', () => {
         const record = { path: '/', components }
         const start = {