Skip to content

Commit 22df352

Browse files
authored
feat: Reenable Vue Router for Vue 3 (#191)
* Enable vue-router example * Add missing colon * Reenable vue router mocha test
1 parent 3ef7112 commit 22df352

File tree

7 files changed

+62
-91
lines changed

7 files changed

+62
-91
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@
7474
"vee-validate": "^4.0.2",
7575
"vue": "^3.0.4",
7676
"vue-apollo": "^3.0.5",
77-
"vue-i18n": "^9.0.0-beta.6",
77+
"vue-i18n": "^9.0.0-beta.10",
7878
"vue-jest": "^5.0.0-alpha.7",
79-
"vue-router": "^4.0.0-rc.6",
79+
"vue-router": "^4.0.1",
8080
"vuetify": "^2.3.19",
8181
"vuex": "^4.0.0-rc.2"
8282
},
+6-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
<template>
2-
<div>
3-
<RouterLink to="/" data-testid="home-link">
4-
Home
5-
</RouterLink>
6-
<RouterLink to="/about" data-testid="about-link">
7-
About
8-
</RouterLink>
9-
<RouterView />
10-
<div data-testid="location-display">{{ $route.fullPath }}</div>
11-
</div>
12-
</template>
1+
<template>
2+
<router-link to="/" data-testid="home-link">Home</router-link>
3+
<router-link to="/about" data-testid="about-link">About</router-link>
4+
<router-view />
5+
<div data-testid="location-display">{{ $route.fullPath }}</div>
6+
</template>

src/__tests__/components/Router/LocationDisplay.vue

-3
This file was deleted.

src/__tests__/components/Router/NoMatch.vue

-3
This file was deleted.

src/__tests__/vue-router-mocha.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
test.todo('Your test suite must contain at least one test.')
1+
import '@testing-library/jest-dom'
2+
import {render} from '..'
23

3-
// import '@testing-library/jest-dom'
4-
// import {render} from '..'
4+
import About from './components/Router/About.vue'
55

6-
// import About from './components/Router/About.vue'
6+
const routes = []
7+
test('uses require("vue-router").default when require("vue-router") is undefined (useful for mocha users)', () => {
8+
// Test for fix https://github.com/testing-library/vue-testing-library/issues/119
9+
jest.mock('vue-router', () => undefined)
710

8-
// const routes = []
9-
// test('uses require("vue-router").default when require("vue-router") is undefined (useful for mocha users)', () => {
10-
// // Test for fix https://github.com/testing-library/vue-testing-library/issues/119
11-
// jest.mock('vue-router', () => {
12-
// return undefined
13-
// })
14-
15-
// expect(() => render(About, {routes})).toThrowError(
16-
// new TypeError("Cannot read property 'default' of undefined"),
17-
// )
18-
// })
11+
expect(() => render(About, {routes})).toThrowError(
12+
new TypeError("Cannot read property 'default' of undefined"),
13+
)
14+
})

src/__tests__/vue-router.js

+34-38
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
1-
test.todo('Your test suite must contain at least one test.')
2-
3-
// // Please notice that this example is a draft example on how to test
4-
// // the router.
5-
// // Related issue on Vue Test Utils: https://github.com/vuejs/vue-test-utils-next/issues/152
6-
7-
// import '@testing-library/jest-dom'
8-
// import {render, fireEvent} from '..'
9-
// import App from './components/Router/App.vue'
10-
// import Home from './components/Router/Home.vue'
11-
// import About from './components/Router/About.vue'
12-
13-
// const routes = [
14-
// {path: '/', component: Home},
15-
// {path: '/about', component: About},
16-
// ]
17-
18-
// test('full app rendering/navigating', async () => {
19-
// // Notice how we pass a `routes` object to our render function.
20-
// const {queryByTestId} = render(App, {routes})
21-
22-
// expect(queryByTestId('location-display')).toHaveTextContent('/')
23-
24-
// await fireEvent.click(queryByTestId('about-link'))
25-
26-
// expect(queryByTestId('location-display')).toHaveTextContent('/about')
27-
// })
28-
29-
// test('setting initial route', () => {
30-
// // The callback function receives three parameters: the Vue instance where
31-
// // the component is mounted, the store instance (if any) and the router
32-
// // object.
33-
// const {queryByTestId} = render(App, {routes}, (vue, store, router) => {
34-
// router.push('/about')
35-
// })
36-
37-
// expect(queryByTestId('location-display')).toHaveTextContent('/about')
38-
// })
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
4+
5+
import '@testing-library/jest-dom'
6+
import {waitFor} from '@testing-library/dom'
7+
import {render, fireEvent} from '..'
8+
import App from './components/Router/App.vue'
9+
import Home from './components/Router/Home.vue'
10+
import About from './components/Router/About.vue'
11+
12+
const routes = [
13+
{path: '/', component: Home},
14+
{path: '/about', component: About},
15+
]
16+
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})
20+
21+
// Vue Router navigation is async, so we need to wait until the
22+
// initial render happens
23+
expect(await findByText('You are home')).toBeInTheDocument()
24+
25+
await fireEvent.click(getByTestId('about-link'))
26+
27+
// Same thing here: Vue Router navigation is async, so we need to wait until the
28+
// navigation happens
29+
await waitFor(() =>
30+
expect(getByTestId('location-display')).toHaveTextContent('/about'),
31+
)
32+
33+
expect(getByText('You are on the about page')).toBeInTheDocument()
34+
})

src/index.js

+9-18
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,40 @@ function render(
1515
TestComponent,
1616
{
1717
store = null,
18-
// routes = null,
18+
routes = null,
1919
container: customContainer,
2020
baseElement: customBaseElement,
2121
...mountOptions
2222
} = {},
23-
// configurationCb,
2423
) {
2524
const div = document.createElement('div')
2625
const baseElement = customBaseElement || customContainer || document.body
2726
const container = customContainer || baseElement.appendChild(div)
2827

29-
// let additionalOptions = {}
30-
3128
const plugins = []
3229

3330
if (store) {
3431
const {createStore} = require('vuex')
3532
plugins.push(createStore(store))
3633
}
3734

38-
// if (routes) {
39-
// const requiredRouter = require('vue-router')
40-
// const {createRouter, createWebHistory} =
41-
// requiredRouter.default || requiredRouter
42-
// plugins.push(createRouter({history: createWebHistory(), routes}))
43-
// }
35+
if (routes) {
36+
const requiredRouter = require('vue-router')
37+
const {createRouter, createWebHistory} =
38+
requiredRouter.default || requiredRouter
4439

45-
// Should we expose vue 3 app? if so, how?
46-
// if (configurationCb && typeof configurationCb === 'function') {
47-
// additionalOptions = configurationCb(router)
48-
// }
40+
const routerPlugin = createRouter({history: createWebHistory(), routes})
41+
plugins.push(routerPlugin)
42+
}
4943

5044
const mountComponent = (Component, newProps) => {
5145
const wrapper = mount(
5246
Component,
5347
merge({
5448
attachTo: container,
55-
global: {
56-
plugins,
57-
},
49+
global: {plugins},
5850
...mountOptions,
5951
props: newProps || mountOptions.props,
60-
// ...additionalOptions,
6152
}),
6253
)
6354

0 commit comments

Comments
 (0)