如何测试 Vue 路由渲染正确的组件?
如何测试用户单击路由器链接时发生的实际导航?例如,我希望渲染链接的组件,但只有路由发生变化,而渲染的组件不会改变。
在下面的代码片段中,我依赖于 Signup
组件,其中包含指向 /login
路径的路由器链接。请参阅注释行以了解我的意图:
test.only('navigates to login page when login btn is clicked', async () => {
const TestLogin = {
template: '<h1>Login page</h1>',
}
const routes = [{ path: '/login', name: 'Home', component: TestLogin }]
const router = new VueRouter({ routes })
localVue.use(VueRouter)
const { getByText } = render(Signup, {
localVue,
router,
vuetify,
})
const btn = getByText('go to login')
await fireEvent.click(btn)
expect(router.history.current.fullPath).toBe('/login') <----- this passes
await findByText('Login page') <-------- this fails!
})
为什么单击链接时未呈现 TestLogin
组件?导航实际上有效,即当我正常运行应用程序时。
How can I test the actual navigation that happens when a user clicks on a router link? E.g. I expect the linked component to be rendered but only the route changes, while the rendered component does not change.
In the following snippet, I rely on a Signup
component which includes a router link to the /login
path. See the commented lines to understand my intent:
test.only('navigates to login page when login btn is clicked', async () => {
const TestLogin = {
template: '<h1>Login page</h1>',
}
const routes = [{ path: '/login', name: 'Home', component: TestLogin }]
const router = new VueRouter({ routes })
localVue.use(VueRouter)
const { getByText } = render(Signup, {
localVue,
router,
vuetify,
})
const btn = getByText('go to login')
await fireEvent.click(btn)
expect(router.history.current.fullPath).toBe('/login') <----- this passes
await findByText('Login page') <-------- this fails!
})
Why is the TestLogin
component not rendered when the link is clicked? The navigation works in actuality, i.e. when I run the application normally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论