From e5456245b5cae71def2a4c8e85f47e61eb576b41 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 27 Sep 2019 10:19:40 +0200 Subject: [PATCH] refactor: use router.resolve in Link --- __tests__/router-link.spec.js | 18 ++++++------------ src/components/Link.ts | 25 +------------------------ src/router.ts | 2 +- 3 files changed, 8 insertions(+), 37 deletions(-) diff --git a/__tests__/router-link.spec.js b/__tests__/router-link.spec.js index 38ff0e06..81a4df79 100644 --- a/__tests__/router-link.spec.js +++ b/__tests__/router-link.spec.js @@ -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) }) }) diff --git a/src/components/Link.ts b/src/components/Link.ts index fee83cf1..8ac08660 100644 --- a/src/components/Link.ts +++ b/src/components/Link.ts @@ -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 diff --git a/src/router.ts b/src/router.ts index 66f36d13..926356df 100644 --- a/src/router.ts +++ b/src/router.ts @@ -152,7 +152,7 @@ export class Router { }) } - resolveLocation( + private resolveLocation( location: MatcherLocation & Required, currentLocation?: RouteLocationNormalized, redirectedFrom?: RouteLocationNormalized -- 2.39.5