Skip to content

Commit

Permalink
fix(abstract): call afterHooks with go
Browse files Browse the repository at this point in the history
Fix #3250
  • Loading branch information
posva committed Sep 24, 2020
1 parent 036fced commit 4da7021
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/history/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ export class AbstractHistory extends History {
this.confirmTransition(
route,
() => {
const prev = this.current
this.index = targetIndex
this.updateRoute(route)
this.router.afterHooks.forEach(hook => {
hook && hook(route, prev)
})
},
err => {
if (isNavigationFailure(err, NavigationFailureType.duplicated)) {
Expand Down
42 changes: 42 additions & 0 deletions test/unit/specs/abstract-history.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Vue from 'vue'
import VueRouter from '../../../src/index'

Vue.use(VueRouter)

const delay = t => new Promise(resolve => setTimeout(resolve, t))

describe('abstract history', () => {
it('run afterEach after initial navigation', done => {
const router = new VueRouter({ mode: 'abstract' })
const afterEach = jasmine.createSpy('afterEach')
const onReady = jasmine.createSpy('ready')
const onError = jasmine.createSpy('error')
router.afterEach(afterEach)
router.onReady(onReady, onError)

router.push('/').finally(() => {
expect(onReady).toHaveBeenCalled()
expect(onError).not.toHaveBeenCalled()
expect(afterEach).toHaveBeenCalled()
done()
})
})

it('run afterEach after router.go', done => {
const router = new VueRouter({ mode: 'abstract' })
const afterEach = jasmine.createSpy('afterEach')

router
.push('/')
.then(() => router.push('/foo'))
.then(() => {
router.afterEach(afterEach)
router.go(-1)
return delay(30)
})
.finally(() => {
expect(afterEach).toHaveBeenCalled()
done()
})
})
})

0 comments on commit 4da7021

Please sign in to comment.