]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(history): proper destroy in memory history
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 2 May 2021 16:16:51 +0000 (18:16 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 2 May 2021 16:16:57 +0000 (18:16 +0200)
__tests__/history/memory.spec.ts
src/history/memory.ts

index 4afa14544e938a4e6f5cd71cb0ee0d34c18e57f9..62e7d10bc41e853d082025453cd187ddfb7ffd99 100644 (file)
@@ -152,10 +152,29 @@ describe('Memory history', () => {
     const spy = jest.fn()
     history.listen(spy)
     history.destroy()
+    history.push('/2')
     history.go(-1)
     expect(spy).not.toHaveBeenCalled()
   })
 
+  it('can be reused after destroy', () => {
+    const history = createMemoryHistory()
+    history.push('/1')
+    history.push('/2')
+    history.push('/3')
+    history.go(-1)
+
+    expect(history.location).toBe('/2')
+    history.destroy()
+    history.go(-1)
+    expect(history.location).toBe(START)
+    history.push('/4')
+    history.push('/5')
+    expect(history.location).toBe('/5')
+    history.go(-1)
+    expect(history.location).toBe('/4')
+  })
+
   it('can avoid listeners with back and forward', () => {
     const history = createMemoryHistory()
     const spy = jest.fn()
index 4187cf77122aafd907099611d55804e68b625015..5a021fbbcf96d985eecdc7789d7366522fbd56c7 100644 (file)
@@ -52,6 +52,7 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
   const routerHistory: RouterHistory = {
     // rewritten by Object.defineProperty
     location: START,
+    // TODO: should be kept in queue
     state: {},
     base,
     createHref: createHref.bind(null, base),
@@ -75,6 +76,8 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
     },
     destroy() {
       listeners = []
+      queue = [START]
+      position = 0
     },
 
     go(delta, shouldTrigger = true) {