]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: add replace option to push
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 1 May 2019 16:07:15 +0000 (18:07 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 1 May 2019 16:07:15 +0000 (18:07 +0200)
__tests__/router.spec.js
__tests__/utils.ts
src/matcher.ts
src/router.ts

index fccd74a1537bbe8b60ecf4790ba9f4fe2f127a87..b43682daef47846d38d4efd81d4560ff6ea83c78 100644 (file)
@@ -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(
-      `<!DOCTYPE html><html><head></head><body></body></html>`,
-      {
-        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: '',
+    })
+  })
 })
index 4a3af18af4acc0018dc0689f6b80315ac4df3886..589c133ff13664179bec609e367a954cfb2bbfc1 100644 (file)
@@ -20,3 +20,9 @@ export function createDom(options?: ConstructorOptions) {
 
   return dom
 }
+
+export const components = {
+  Home: { template: `<div>Home</div>` },
+  Foo: { template: `<div>Foo</div>` },
+  Bar: { template: `<div>Bar</div>` },
+}
index 38d418250897daff4be190a92b007b0d3f34236e..75a3b32655d5ce393969ed78bc2ad74f426eb9b3 100644 (file)
@@ -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
index b16fd80950700cc9ce13c20a6120e41b0d023bb5..101cffd4fcc8e32d1d16da411562e26d515822e0 100644 (file)
@@ -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 })