Skip to content

feat: allow passing instantiated Vue Router #239

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
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions src/__tests__/vue-router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '@testing-library/jest-dom'
import {render, fireEvent} from '@testing-library/vue'
import VueRouter from 'vue-router'

import App from './components/Router/App.vue'
import Home from './components/Router/Home.vue'
Expand Down Expand Up @@ -32,3 +33,15 @@ test('setting initial route', () => {

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

test('can render with an instantiated Vuex store', async () => {
// Instantiate a router with only one route
const instantiatedRouter = new VueRouter({
routes: [{path: '/special-path', component: Home}],
})

render(App, {routes: instantiatedRouter}, (vue, store, router) => {
expect(router.getRoutes()).toHaveLength(1)
expect(router.getRoutes()[0].path).toEqual('/special-path')
})
})
2 changes: 1 addition & 1 deletion src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function render(
const VueRouter = requiredRouter.default || requiredRouter
localVue.use(VueRouter)

router = new VueRouter({routes})
router = routes instanceof VueRouter ? routes : new VueRouter({routes})
}

if (configurationCb && typeof configurationCb === 'function') {
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface RenderOptions<V extends Vue, S = {}>
extends Omit<ThisTypedMountOptions<V>, 'store' | 'props'> {
props?: object
store?: StoreOptions<S>
routes?: RouteConfig[]
routes?: RouteConfig[] | Router
container?: Element
baseElement?: Element
}
Expand Down
9 changes: 9 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Vue from 'vue'
import Vuex from 'vuex'
import VueRouter from 'vue-router'
import {render, fireEvent, screen, waitFor} from '@testing-library/vue'

declare const elem: Element
Expand Down Expand Up @@ -152,6 +153,14 @@ export function testInstantiatedStore() {
})
}

export function testInstantiatedRouter() {
render(SomeComponent, {
routes: new VueRouter({
routes: [{path: '/', name: 'home', component: SomeComponent}],
}),
})
}

/*
eslint
testing-library/prefer-explicit-assert: "off",
Expand Down