Skip to content

Commit

Permalink
feat: allow passing instantiated Vue Router (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
blimmer authored Jun 29, 2021
1 parent 9d63d71 commit 96c0c2d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
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

0 comments on commit 96c0c2d

Please sign in to comment.