1
- // Please notice that this example is a draft example on how to test
2
- // the router.
3
- // Related issue on Vue Test Utils: https://github.com/vuejs/vue-test-utils-next/issues/152
1
+ /* eslint-disable vue/require-prop-types */
2
+ /* eslint-disable vue/one-component-per-file */
4
3
5
4
import '@testing-library/jest-dom'
6
5
import { waitFor } from '@testing-library/dom'
6
+ import { defineComponent } from 'vue'
7
+ import { createRouter , createWebHistory } from 'vue-router'
7
8
import { render , fireEvent } from '..'
8
9
import App from './components/Router/App.vue'
9
10
import Home from './components/Router/Home.vue'
10
11
import About from './components/Router/About.vue'
11
12
12
- const routes = [
13
- { path : '/' , component : Home } ,
14
- { path : '/about' , component : About } ,
15
- ]
13
+ test ( 'full app rendering/navigating from base URL' , async ( ) => {
14
+ // Create a Router instance
15
+ // https://next.router.vuejs.org/api/#createrouter
16
+ // using the a HTML5 history.
17
+ // https://next.router.vuejs.org/api/#createwebhistory
18
+ const router = createRouter ( {
19
+ history : createWebHistory ( ) ,
20
+ routes : [
21
+ { path : '/' , component : Home } ,
22
+ { path : '/about' , component : About } ,
23
+ ] ,
24
+ } )
16
25
17
- test ( 'full app rendering/navigating' , async ( ) => {
18
- // Notice how we pass a `routes` object to our render function.
19
- const { findByText, getByText, getByTestId} = render ( App , { routes} )
26
+ const { findByText, getByText, getByTestId} = render ( App , {
27
+ global : {
28
+ plugins : [ router ] ,
29
+ } ,
30
+ } )
20
31
21
32
// Vue Router navigation is async, so we need to wait until the
22
33
// initial render happens
23
34
expect ( await findByText ( 'You are home' ) ) . toBeInTheDocument ( )
35
+ expect ( getByTestId ( 'location-display' ) ) . toHaveTextContent ( '/' )
24
36
25
37
await fireEvent . click ( getByTestId ( 'about-link' ) )
26
38
@@ -32,3 +44,36 @@ test('full app rendering/navigating', async () => {
32
44
33
45
expect ( getByText ( 'You are on the about page' ) ) . toBeInTheDocument ( )
34
46
} )
47
+
48
+ test ( 'sets router initial state' , async ( ) => {
49
+ const Component = defineComponent ( {
50
+ props : [ 'to' ] ,
51
+ template : `<router-link :to="to">Learn More</router-link>` ,
52
+ } )
53
+
54
+ const route = {
55
+ name : 'routeName' ,
56
+ path : '/' ,
57
+ component : defineComponent ( { template : `<div></div>` } ) ,
58
+ }
59
+
60
+ const router = createRouter ( {
61
+ history : createWebHistory ( ) ,
62
+ routes : [ route ] ,
63
+ } )
64
+
65
+ const to = { name : route . name }
66
+
67
+ const { getByText} = render ( Component , {
68
+ props : { to} ,
69
+ global : {
70
+ plugins : [ router ] ,
71
+ } ,
72
+ } )
73
+
74
+ // We need to wait for router to complete the initial navigation.
75
+ await router . isReady ( )
76
+
77
+ const button = getByText ( 'Learn More' )
78
+ expect ( button ) . toHaveAttribute ( 'href' , route . path )
79
+ } )
0 commit comments