From: Eduardo San Martin Morote Date: Mon, 1 Mar 2021 17:38:51 +0000 (+0100) Subject: test(e2e): avoid wrong navigation X-Git-Tag: v4.0.5~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=04bf1a7b28861c6c47a39886a847625a542dde38;p=thirdparty%2Fvuejs%2Frouter.git test(e2e): avoid wrong navigation --- diff --git a/e2e/modal/index.ts b/e2e/modal/index.ts index 1e35e352..0ed4a90c 100644 --- a/e2e/modal/index.ts +++ b/e2e/modal/index.ts @@ -146,7 +146,12 @@ const router = createRouter({ ], }, { path: '/about', component: About }, - { path: '/users/:id', props: true, name: 'user', component: UserDetails }, + { + path: '/users/:id', + props: true, + name: 'user', + component: UserDetails, + }, ], }) @@ -162,6 +167,16 @@ router.beforeEach((to, from, next) => { next() }) +// avoid navigating to non existent users +router.beforeEach(to => { + if (to.name !== 'user') return + + const { id } = to.params + return ( + typeof id === 'string' && !Number.isNaN(Number(id)) && !!users[Number(id)] + ) +}) + const app = createApp({ setup() { const route = useRoute() @@ -188,3 +203,5 @@ const app = createApp({ app.use(router) window.vm = app.mount('#app') +// @ts-ignore +window.router = router