]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: use router.resolve in Link
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 27 Sep 2019 08:19:40 +0000 (10:19 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 27 Sep 2019 08:19:40 +0000 (10:19 +0200)
__tests__/router-link.spec.js
src/components/Link.ts
src/router.ts

index 38ff0e0670825ec163635f7e291f15bf36539ba4..81a4df79732b49272163fab1e4bc6fac18c8ace0 100644 (file)
@@ -62,11 +62,11 @@ describe('RouterLink', () => {
   function factory(currentLocation, propsData, resolvedLocation) {
     const router = {
       history: new HistoryMock(),
-      resolveLocation: jest.fn(),
+      resolve: jest.fn(),
       push: jest.fn(),
     }
 
-    router.resolveLocation.mockReturnValueOnce(resolvedLocation)
+    router.resolve.mockReturnValueOnce(resolvedLocation)
     // @ts-ignore TODO: Some information are missing on RouterLink
     const wrapper = mount(RouterLink, {
       propsData,
@@ -107,11 +107,8 @@ describe('RouterLink', () => {
       { to: locations.basic.string },
       locations.basic.normalized
     )
-    expect(router.resolveLocation).toHaveBeenCalledTimes(1)
-    expect(router.resolveLocation).toHaveBeenCalledWith(
-      expect.objectContaining({ path: locations.basic.string }),
-      START_LOCATION_NORMALIZED
-    )
+    expect(router.resolve).toHaveBeenCalledTimes(1)
+    expect(router.resolve).toHaveBeenCalledWith(locations.basic.string)
   })
 
   it('calls router.push when clicked', () => {
@@ -129,11 +126,8 @@ describe('RouterLink', () => {
     const { router } = factory(
       START_LOCATION_NORMALIZED,
       { to: locations.withQuery.string },
-      locations.withQuery.normalized // it doesn't matter as we want to check what resolveLocation is called with
-    )
-    expect(router.resolveLocation).toHaveBeenCalledWith(
-      expect.objectContaining({ query: locations.withQuery.normalized.query }),
-      START_LOCATION_NORMALIZED
+      locations.withQuery.normalized // it doesn't matter as we want to check what resolve is called with
     )
+    expect(router.resolve).toHaveBeenCalledWith(locations.withQuery.string)
   })
 })
index fee83cf15471007365a0d5e95d231f1d335f0ebb..8ac08660844df7c89eef264e8bb0528c27d15b08 100644 (file)
@@ -1,7 +1,6 @@
 import { Component } from 'vue'
 import { Router } from '../router'
 import { RouteLocationNormalized, RouteLocation } from '../types'
-import { HistoryLocationNormalized } from '../history/base'
 
 const Link: Component = {
   name: 'RouterLink',
@@ -20,29 +19,7 @@ const Link: Component = {
     // @ts-ignore can't get `this`
     const to = this.to as RouteLocation
 
-    // @ts-ignore can't get `this`
-    const history = router.history
-    let url: HistoryLocationNormalized
-    let location: RouteLocationNormalized
-    // TODO: refactor router code and use its function istead of having a copied version here
-    if (typeof to === 'string' || ('path' in to && !('name' in to))) {
-      url = history.utils.normalizeLocation(to)
-      // TODO: should allow a non matching url to allow dynamic routing to work
-      location = router.resolveLocation(url, from)
-    } else {
-      // named or relative route
-      const query = history.utils.normalizeQuery(to.query ? to.query : {})
-      const hash = to.hash || ''
-      // we need to resolve first
-      location = router.resolveLocation({ ...to, query, hash }, from)
-      // intentionally drop current query and hash
-      url = history.utils.normalizeLocation({
-        query,
-        hash,
-        ...location,
-      })
-    }
-    const route = location
+    const route = router.resolve(to)
 
     // TODO: active classes
     // TODO: handle replace prop
index 66f36d1305de39ed87c94f02cf65da63e1b0bd43..926356dfdd0d5646f8f0632296dbd2ef98f4dd5f 100644 (file)
@@ -152,7 +152,7 @@ export class Router {
     })
   }
 
-  resolveLocation(
+  private resolveLocation(
     location: MatcherLocation & Required<RouteQueryAndHash>,
     currentLocation?: RouteLocationNormalized,
     redirectedFrom?: RouteLocationNormalized