Skip to content

Commit 377be76

Browse files
committed
fix: improve createLocalVue and add more complete router tests
1 parent 7b567d3 commit 377be76

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/create-local-vue.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ function createLocalVue (): Component {
2323
// so that merge strats registered by plguins can work properly
2424
instance.config.optionMergeStrategies = Vue.config.optionMergeStrategies
2525

26+
// make sure all extends are based on this instance.
27+
// this is important so that global components registered by plugins,
28+
// e.g. router-link are created using the correct base constructor
29+
instance.options._base = instance
30+
2631
// compat for vue-router < 2.7.1 where it does not allow multiple installs
2732
const use = instance.use
2833
instance.use = (plugin) => {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<div>
3+
<router-link to="/foo">foo</router-link>
4+
<router-view></router-view>
5+
</div>
6+
</template>

test/unit/specs/create-local-vue.spec.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import VueRouter from 'vue-router'
44
import mount from '~src/mount'
55
import Component from '~resources/components/component.vue'
66
import ComponentWithVuex from '~resources/components/component-with-vuex.vue'
7+
import ComponentWithRouter from '~resources/components/component-with-router.vue'
78

89
describe('createLocalVue', () => {
910
it('installs Vuex without polluting global Vue', () => {
@@ -63,17 +64,34 @@ describe('createLocalVue', () => {
6364
expect(typeof freshWrapper.vm.$route).to.equal('undefined')
6465
})
6566

66-
it('installs Router after a previous installed', () => {
67+
it('Router should work properly with local Vue', () => {
6768
const localVue = createLocalVue()
6869
localVue.use(VueRouter)
6970
const routes = [
70-
{ path: '/foo', component: Component }
71+
{
72+
path: '/',
73+
component: {
74+
render: h => h('div', 'home')
75+
}
76+
},
77+
{
78+
path: '/foo',
79+
component: {
80+
render: h => h('div', 'foo')
81+
}
82+
}
7183
]
7284
const router = new VueRouter({
7385
routes
7486
})
75-
const wrapper = mount(Component, { localVue, router })
87+
const wrapper = mount(ComponentWithRouter, { localVue, router })
7688
expect(wrapper.vm.$route).to.be.an('object')
89+
90+
expect(wrapper.text()).to.contain('home')
91+
92+
wrapper.find('a').trigger('click')
93+
expect(wrapper.text()).to.contain('foo')
94+
7795
const freshWrapper = mount(Component)
7896
expect(typeof freshWrapper.vm.$route).to.equal('undefined')
7997
})

0 commit comments

Comments
 (0)