From: Eduardo San Martin Morote Date: Sun, 2 May 2021 16:16:51 +0000 (+0200) Subject: fix(history): proper destroy in memory history X-Git-Tag: v4.0.7~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9d188aa165aeb12aa3771aaa56a269f5dad3ccf6;p=thirdparty%2Fvuejs%2Frouter.git fix(history): proper destroy in memory history --- diff --git a/__tests__/history/memory.spec.ts b/__tests__/history/memory.spec.ts index 4afa1454..62e7d10b 100644 --- a/__tests__/history/memory.spec.ts +++ b/__tests__/history/memory.spec.ts @@ -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() diff --git a/src/history/memory.ts b/src/history/memory.ts index 4187cf77..5a021fbb 100644 --- a/src/history/memory.ts +++ b/src/history/memory.ts @@ -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) {