From: Eduardo San Martin Morote Date: Wed, 1 May 2019 16:07:15 +0000 (+0200) Subject: feat: add replace option to push X-Git-Tag: v4.0.0-alpha.0~418 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=adee692be5c3351d3075fdbaab08ebc9ec382f10;p=thirdparty%2Fvuejs%2Frouter.git feat: add replace option to push --- diff --git a/__tests__/router.spec.js b/__tests__/router.spec.js index fccd74a1..b43682da 100644 --- a/__tests__/router.spec.js +++ b/__tests__/router.spec.js @@ -3,27 +3,26 @@ require('./helper') const expect = require('expect') const { HTML5History } = require('../src/history/html5') const { Router } = require('../src/router') -const { JSDOM } = require('jsdom') +const { createDom, components } = require('./utils') + +function mockHistory() { + // TODO: actually do a mock + return new HTML5History() +} + +const routes = [ + { path: '/', component: components.Home }, + { path: '/foo', component: components.Foo }, +] describe('Router', () => { beforeAll(() => { - // TODO: move to utils for tests that need DOM - const dom = new JSDOM( - ``, - { - url: 'https://example.org/', - referrer: 'https://example.com/', - contentType: 'text/html', - } - ) - - // @ts-ignore - global.window = dom.window + createDom() }) it('can be instantiated', () => { - const history = new HTML5History() - const router = new Router({ history, routes: [] }) + const history = mockHistory() + const router = new Router({ history, routes }) expect(router.currentRoute).toEqual({ fullPath: '/', hash: '', @@ -32,4 +31,32 @@ describe('Router', () => { query: {}, }) }) + + it('calls history.push with router.push', async () => { + const history = mockHistory() + const router = new Router({ history, routes }) + jest.spyOn(history, 'push') + await router.push('/foo') + expect(history.push).toHaveBeenCalledTimes(1) + expect(history.push).toHaveBeenCalledWith({ + fullPath: '/foo', + path: '/foo', + query: {}, + hash: '', + }) + }) + + it('calls history.replace with router.replace', async () => { + const history = mockHistory() + const router = new Router({ history, routes }) + jest.spyOn(history, 'replace') + await router.replace('/foo') + expect(history.replace).toHaveBeenCalledTimes(1) + expect(history.replace).toHaveBeenCalledWith({ + fullPath: '/foo', + path: '/foo', + query: {}, + hash: '', + }) + }) }) diff --git a/__tests__/utils.ts b/__tests__/utils.ts index 4a3af18a..589c133f 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -20,3 +20,9 @@ export function createDom(options?: ConstructorOptions) { return dom } + +export const components = { + Home: { template: `
Home
` }, + Foo: { template: `
Foo
` }, + Bar: { template: `
Bar
` }, +} diff --git a/src/matcher.ts b/src/matcher.ts index 38d41825..75a3b326 100644 --- a/src/matcher.ts +++ b/src/matcher.ts @@ -34,6 +34,11 @@ export class RouterMatcher { this.matchers = routes.map(generateMatcher) } + /** + * Resolve a location without doing redirections so it can be used for anchors + */ + resolveAsPath() {} + /** * Transforms a MatcherLocation object into a normalized location * @param location MatcherLocation to resolve to a url diff --git a/src/router.ts b/src/router.ts index b16fd809..101cffd4 100644 --- a/src/router.ts +++ b/src/router.ts @@ -45,8 +45,9 @@ export class Router { } /** - * Trigger a navigation, should resolve all guards first - * @param to Where to go + * Trigger a navigation, adding an entry to the history stack. Also apply all navigation + * guards first + * @param to where to go */ async push(to: RouteLocation) { let url: HistoryLocationNormalized @@ -69,7 +70,8 @@ export class Router { // TODO: refactor in a function, some kind of queue const toLocation: RouteLocationNormalized = { ...url, ...location } await this.navigate(toLocation, this.currentRoute) - this.history.push(url) + if (to.replace === true) this.history.replace(url) + else this.history.push(url) const from = this.currentRoute this.currentRoute = toLocation @@ -77,6 +79,11 @@ export class Router { for (const guard of this.afterGuards) guard(toLocation, from) } + /** + * Trigger a navigation, replacing current entry in history. Also apply all navigation + * guards first + * @param to where to go + */ replace(to: RouteLocation) { const location = typeof to === 'string' ? { path: to } : to return this.push({ ...location, replace: true })