]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(abstract): remove listeners
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Jun 2019 13:57:51 +0000 (15:57 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Jun 2019 13:57:51 +0000 (15:57 +0200)
__tests__/abstract.spec.js
src/history/abstract.ts

index 24aeb525e4eed0835b8ec2b361e40b04d479ff61..e4822728431cd88e14dd748bbf567108f042666a 100644 (file)
@@ -61,11 +61,47 @@ describe('Abstract/in memory history', () => {
     })
   })
 
-  it('saves forward information', () => {})
+  it('can replace a location', () => {
+    const history = new AbstractHistory()
+    // partial version
+    history.replace({ path: '/somewhere', hash: '#hey', query: { foo: 'foo' } })
+    expect(history.location).toEqual({
+      fullPath: '/somewhere?foo=foo#hey',
+      path: '/somewhere',
+      query: { foo: 'foo' },
+      hash: '#hey',
+    })
+    expect(history.position).toEqual(0)
+    expect(history.queue).toHaveLength(1)
+    history.push(loc)
+
+    // partial version
+    history.replace({ path: '/path', hash: '#ho' })
+    expect(history.location).toEqual({
+      fullPath: '/path#ho',
+      path: '/path',
+      query: {},
+      hash: '#ho',
+    })
+    expect(history.position).toEqual(1)
+    expect(history.queue).toHaveLength(2)
+  })
 
-  it('can replace a location', () => {})
+  it('does not trigger listeners with push', () => {
+    const history = new AbstractHistory()
+    const spy = jest.fn()
+    history.listen(spy)
+    history.push(loc)
+    expect(spy).not.toHaveBeenCalled()
+  })
 
-  it('can simulate a navigation', () => {})
+  it('does not trigger listeners with replace', () => {
+    const history = new AbstractHistory()
+    const spy = jest.fn()
+    history.listen(spy)
+    history.replace(loc)
+    expect(spy).not.toHaveBeenCalled()
+  })
 
   it('add entries to the queue', () => {
     const history = new AbstractHistory()
@@ -161,4 +197,34 @@ describe('Abstract/in memory history', () => {
     expect(spy).not.toHaveBeenCalled()
     expect(spy2).toHaveBeenCalledTimes(1)
   })
+
+  it('removing the same listener is a noop', () => {
+    const history = new AbstractHistory()
+    const spy = jest.fn()
+    const spy2 = jest.fn()
+    const rem = history.listen(spy)
+    const rem2 = history.listen(spy2)
+    rem()
+    rem()
+    history.push(loc)
+    history.back()
+    expect(spy).not.toHaveBeenCalled()
+    expect(spy2).toHaveBeenCalledTimes(1)
+    rem2()
+    rem2()
+    history.forward()
+    expect(spy).not.toHaveBeenCalled()
+    expect(spy2).toHaveBeenCalledTimes(1)
+  })
+
+  it('removes all listeners with destroy', () => {
+    const history = new AbstractHistory()
+    const spy = jest.fn()
+    history.listen(spy)
+    // @ts-ignore
+    expect(history.listeners).toHaveLength(1)
+    history.destroy()
+    // @ts-ignore
+    expect(history.listeners).toHaveLength(0)
+  })
 })
index c0434f1efb23c91e540e128c252371c337720c78..8e283ff2bdcc6c623d35c234a440dc311c228edd 100644 (file)
@@ -10,8 +10,6 @@ import { NavigationCallback, HistoryState, START } from './base'
 // const cs = consola.withTag('abstract')
 
 export class AbstractHistory extends BaseHistory {
-  // private _listeners: NavigationCallback[] = []
-  private teardowns: Array<() => void> = []
   private listeners: NavigationCallback[] = []
   public queue: HistoryLocationNormalized[] = [START]
   public position: number = 0
@@ -23,7 +21,12 @@ export class AbstractHistory extends BaseHistory {
   // TODO: is this necessary
   ensureLocation() {}
 
-  replace(to: HistoryLocation) {}
+  replace(to: HistoryLocation) {
+    const toNormalized = this.utils.normalizeLocation(to)
+    // remove current entry and decrement position
+    this.queue.splice(this.position--, 1)
+    this.location = toNormalized
+  }
 
   push(to: HistoryLocation, data?: HistoryState) {
     const toNormalized = this.utils.normalizeLocation(to)
@@ -70,8 +73,7 @@ export class AbstractHistory extends BaseHistory {
   }
 
   destroy() {
-    for (const teardown of this.teardowns) teardown()
-    this.teardowns = []
+    this.listeners = []
   }
 
   private triggerListeners(