await p
expect(router.currentRoute.value.fullPath).toBe('/guard/foo')
})
-
- it.todo('invokes with the component context')
- it.todo('invokes with the component context with named views')
- it.todo('invokes with the component context with nested views')
- it.todo('invokes with the component context with nested named views')
})
await router.push('/')
})
})
+
+describe('beforeRouteUpdate', () => {
+ it('invokes with the component context', async () => {
+ expect.assertions(2)
+ const spy = jest
+ .fn()
+ .mockImplementationOnce(function (this: any, to, from, next) {
+ expect(typeof this.counter).toBe('number')
+ next()
+ })
+ const WithParam = defineComponent({
+ template: `text`,
+ // we use data to check if the context is the right one because saving `this` in a variable logs a few warnings
+ data: () => ({ counter: 0 }),
+ beforeRouteUpdate: spy,
+ })
+
+ const router = createRouter({
+ history: createMemoryHistory(),
+ routes: [
+ { path: '/', component },
+ { path: '/:id', component: WithParam },
+ ],
+ })
+ const app = createApp({
+ template: `
+ <router-view />
+ `,
+ })
+ app.use(router)
+ const rootEl = document.createElement('div')
+ document.body.appendChild(rootEl)
+ app.mount(rootEl)
+
+ await router.isReady()
+ await router.push('/one')
+ await router.push('/foo')
+ expect(spy).toHaveBeenCalledTimes(1)
+ })
+})