testing-library / vue-testing-library

🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.
http://testing-library.com/vue
MIT License
1.07k stars 111 forks source link

need to help about dynamic routing test #256

Closed sevkioruc closed 2 years ago

sevkioruc commented 2 years ago

Hello everyone,

I want to test that clicking item after route to detail page of the item.

Although I looked previous issues about this topic I could not solve problem.

I am using Vue 2, Jest and vue-testing-library and I mocked backend with https://github.com/mswjs/msw

const routes = [
    { path: '/', component: TodoList },
    { path: '/todo/:id', component: TodoDetail }
]
it('route to detail page of todo after clicking on todo', async () => {
     render(TodoList, { routes })

     const todo = await screen.findByText('Todo1')
     await userEvent.click(todo)

     const detailPage = await screen.findByTestId('detail-page')
     await waitFor(() => { expect(detailPage).toBeInTheDocument() }, { timeout: 10000 })
})
afontcu commented 2 years ago

Hi!

You're rendering TodoList, not the whole app (with <router-view> and so on). If you want to test a whole router interaction, please check out https://github.com/testing-library/vue-testing-library/blob/main/src/__tests__/vue-router.js#L15 as example. Notice that App.vue is the root component of the app, not one of its routes.

Hope it helps!

sevkioruc commented 2 years ago

Hi!

I understood better more then before and will review this example. Thank you so much for your answer.