Skip to content

Commit

Permalink
fix(guards): avoid enter guards between aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Mar 11, 2021
1 parent 45ecb20 commit 0048b9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions __tests__/guards/beforeRouteEnter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const routes: RouteRecordRaw[] = [
{ path: '/foo', component: Foo },
{
path: '/guard/:n',
alias: '/guard-alias/:n',
component: {
...Foo,
beforeRouteEnter,
Expand Down Expand Up @@ -120,6 +121,20 @@ describe('beforeRouteEnter', () => {
expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
})

it('does not call beforeRouteEnter guards on navigation between aliases', async () => {
const router = createRouter({ routes })
const spy = jest.fn()
beforeRouteEnter.mockImplementation(spy)
await router.push('/guard/valid')
expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
await router.push('/guard-alias/valid')
expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
await router.push('/guard-alias/other')
expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
await router.push('/guard/other')
expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
})

it('calls beforeRouteEnter guards on navigation for nested views', async () => {
const router = createRouter({ routes })
await router.push('/nested/nested/foo')
Expand Down
3 changes: 2 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,8 +1174,9 @@ function extractChangingRecords(
const recordTo = to.matched[i]
if (recordTo) {
// the type doesn't matter because we are comparing per reference
if (from.matched.indexOf(recordTo as any) < 0)
if (!from.matched.find(isSameRouteRecord.bind(null, recordTo))) {
enteringRecords.push(recordTo)
}
}
}

Expand Down

0 comments on commit 0048b9b

Please sign in to comment.