-
Notifications
You must be signed in to change notification settings - Fork 109
/
vue-router.js
79 lines (65 loc) · 2.19 KB
/
vue-router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* eslint-disable vue/require-prop-types */
/* eslint-disable vue/one-component-per-file */
import '@testing-library/jest-dom'
import {waitFor} from '@testing-library/dom'
import {defineComponent} from 'vue'
import {createRouter, createWebHistory} from 'vue-router'
import {render, fireEvent} from '..'
import App from './components/Router/App.vue'
import Home from './components/Router/Home.vue'
import About from './components/Router/About.vue'
test('full app rendering/navigating from base URL', async () => {
// Create a Router instance
// https://next.router.vuejs.org/api/#createrouter
// using a HTML5 history.
// https://next.router.vuejs.org/api/#createwebhistory
const router = createRouter({
history: createWebHistory(),
routes: [
{path: '/', component: Home},
{path: '/about', component: About},
],
})
const {findByText, getByText, getByTestId} = render(App, {
global: {
plugins: [router],
},
})
// Vue Router navigation is async, so we need to wait until the
// initial render happens
expect(await findByText('You are home')).toBeInTheDocument()
expect(getByTestId('location-display')).toHaveTextContent('/')
await fireEvent.click(getByTestId('about-link'))
// Same thing here: Vue Router navigation is async, so we need to wait until the
// navigation happens
await waitFor(() =>
expect(getByTestId('location-display')).toHaveTextContent('/about'),
)
expect(getByText('You are on the about page')).toBeInTheDocument()
})
test('sets router initial state', async () => {
const Component = defineComponent({
props: ['to'],
template: `<router-link :to="to">Learn More</router-link>`,
})
const route = {
name: 'routeName',
path: '/',
component: defineComponent({template: `<div></div>`}),
}
const router = createRouter({
history: createWebHistory(),
routes: [route],
})
const to = {name: route.name}
const {getByText} = render(Component, {
props: {to},
global: {
plugins: [router],
},
})
// We need to wait for router to complete the initial navigation.
await router.isReady()
const button = getByText('Learn More')
expect(button).toHaveAttribute('href', route.path)
})