Skip to content

feat: Reenable Vue Router for Vue 3 #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@
"vee-validate": "^4.0.2",
"vue": "^3.0.4",
"vue-apollo": "^3.0.5",
"vue-i18n": "^9.0.0-beta.6",
"vue-i18n": "^9.0.0-beta.10",
"vue-jest": "^5.0.0-alpha.7",
"vue-router": "^4.0.0-rc.6",
"vue-router": "^4.0.1",
"vuetify": "^2.3.19",
"vuex": "^4.0.0-rc.2"
},
Expand Down
18 changes: 6 additions & 12 deletions src/__tests__/components/Router/App.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<template>
<div>
<RouterLink to="/" data-testid="home-link">
Home
</RouterLink>
<RouterLink to="/about" data-testid="about-link">
About
</RouterLink>
<RouterView />
<div data-testid="location-display">{{ $route.fullPath }}</div>
</div>
</template>
<template>
<router-link to="/" data-testid="home-link">Home</router-link>
<router-link to="/about" data-testid="about-link">About</router-link>
<router-view />
<div data-testid="location-display">{{ $route.fullPath }}</div>
</template>
3 changes: 0 additions & 3 deletions src/__tests__/components/Router/LocationDisplay.vue

This file was deleted.

3 changes: 0 additions & 3 deletions src/__tests__/components/Router/NoMatch.vue

This file was deleted.

72 changes: 34 additions & 38 deletions src/__tests__/vue-router.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
test.todo('Your test suite must contain at least one test.')

// // Please notice that this example is a draft example on how to test
// // the router.
// // Related issue on Vue Test Utils: https://github.com/vuejs/vue-test-utils-next/issues/152

// import '@testing-library/jest-dom'
// 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'

// const routes = [
// {path: '/', component: Home},
// {path: '/about', component: About},
// ]

// test('full app rendering/navigating', async () => {
// // Notice how we pass a `routes` object to our render function.
// const {queryByTestId} = render(App, {routes})

// expect(queryByTestId('location-display')).toHaveTextContent('/')

// await fireEvent.click(queryByTestId('about-link'))

// expect(queryByTestId('location-display')).toHaveTextContent('/about')
// })

// test('setting initial route', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK this test is no longer doable

// // The callback function receives three parameters: the Vue instance where
// // the component is mounted, the store instance (if any) and the router
// // object.
// const {queryByTestId} = render(App, {routes}, (vue, store, router) => {
// router.push('/about')
// })

// expect(queryByTestId('location-display')).toHaveTextContent('/about')
// })
// Please notice that this example is a draft example on how to test
// the router.
// Related issue on Vue Test Utils: https://github.com/vuejs/vue-test-utils-next/issues/152

import '@testing-library/jest-dom'
import {waitFor} from '@testing-library/dom'
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'

const routes = [
{path: '/', component: Home},
{path: '/about', component: About},
]

test('full app rendering/navigating', async () => {
// Notice how we pass a `routes` object to our render function.
const {findByText, getByText, getByTestId} = render(App, {routes})

// Vue Router navigation is async, so we need to wait until the
// initial render happens
expect(await findByText('You are home')).toBeInTheDocument()

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()
})
27 changes: 9 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,40 @@ function render(
TestComponent,
{
store = null,
// routes = null,
routes = null,
container: customContainer,
baseElement: customBaseElement,
...mountOptions
} = {},
// configurationCb,
) {
const div = document.createElement('div')
const baseElement = customBaseElement || customContainer || document.body
const container = customContainer || baseElement.appendChild(div)

// let additionalOptions = {}

const plugins = []

if (store) {
const {createStore} = require('vuex')
plugins.push(createStore(store))
}

// if (routes) {
// const requiredRouter = require('vue-router')
// const {createRouter, createWebHistory} =
// requiredRouter.default || requiredRouter
// plugins.push(createRouter({history: createWebHistory(), routes}))
// }
if (routes) {
const requiredRouter = require('vue-router')
const {createRouter, createWebHistory} =
requiredRouter.default || requiredRouter

// Should we expose vue 3 app? if so, how?
// if (configurationCb && typeof configurationCb === 'function') {
// additionalOptions = configurationCb(router)
// }
const routerPlugin = createRouter({history: createWebHistory(), routes})
plugins.push(routerPlugin)
}

const mountComponent = (Component, newProps) => {
const wrapper = mount(
Component,
merge({
attachTo: container,
global: {
plugins,
},
global: {plugins},
...mountOptions,
props: newProps || mountOptions.props,
// ...additionalOptions,
}),
)

Expand Down