Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(history): proper destroy in memory history
  • Loading branch information
posva committed May 2, 2021
1 parent 8e6ebdf commit 9d188aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __tests__/history/memory.spec.ts
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions src/history/memory.ts
Expand Up @@ -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),
Expand All @@ -75,6 +76,8 @@ export function createMemoryHistory(base: string = ''): RouterHistory {
},
destroy() {
listeners = []
queue = [START]
position = 0
},

go(delta, shouldTrigger = true) {
Expand Down

0 comments on commit 9d188aa

Please sign in to comment.