Skip to content

Commit 55d831f

Browse files
authored
fix: handle extended components correctly (#709)
1 parent 87936e9 commit 55d831f

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

packages/create-instance/create-instance.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @flow
22

3-
import Vue from 'vue'
43
import { createSlotVNodes } from './add-slots'
54
import addMocks from './add-mocks'
65
import { addEventLogger } from './log-events'
@@ -39,10 +38,7 @@ export default function createInstance (
3938
addEventLogger(_Vue)
4039

4140
const instanceOptions = {
42-
...options,
43-
propsData: {
44-
...options.propsData
45-
}
41+
...options
4642
}
4743

4844
deleteMountingOptions(instanceOptions)
@@ -71,12 +67,10 @@ export default function createInstance (
7167
_Vue.component(c, stubComponents[c])
7268
})
7369

74-
const Constructor = (typeof component === 'function' && component.prototype instanceof Vue)
70+
const Constructor = vueVersion < 2.3 && typeof component === 'function'
7571
? component.extend(instanceOptions)
7672
: _Vue.extend(component).extend(instanceOptions)
7773

78-
// const Constructor = _Vue.extend(component).extend(instanceOptions)
79-
8074
Object.keys(instanceOptions.components || {}).forEach(key => {
8175
Constructor.component(key, instanceOptions.components[key])
8276
_Vue.component(key, instanceOptions.components[key])

test/specs/mount.spec.js

+42-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { mount, createLocalVue } from '~vue/test-utils'
44
import Component from '~resources/components/component.vue'
55
import ComponentWithProps from '~resources/components/component-with-props.vue'
66
import ComponentWithMixin from '~resources/components/component-with-mixin.vue'
7+
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
78
import { injectSupported, vueVersion } from '~resources/utils'
89
import {
910
describeRunIf,
1011
itDoNotRunIf
1112
} from 'conditional-specs'
13+
import Vuex from 'vuex'
1214

1315
describeRunIf(process.env.TEST_ENV !== 'node',
1416
'mount', () => {
@@ -62,7 +64,12 @@ describeRunIf(process.env.TEST_ENV !== 'node',
6264

6365
it('returns new VueWrapper with mounted Vue instance initialized with Vue.extend with props, if passed as propsData', () => {
6466
const prop1 = { test: 'TEST' }
65-
const wrapper = mount(Vue.extend(ComponentWithProps), { propsData: { prop1 }})
67+
const TestComponent = Vue.extend(ComponentWithProps)
68+
const wrapper = mount(TestComponent, {
69+
propsData: {
70+
prop1
71+
}
72+
})
6673
expect(wrapper.vm).to.be.an('object')
6774
if (wrapper.vm.$props) {
6875
expect(wrapper.vm.$props.prop1).to.equal(prop1)
@@ -131,6 +138,25 @@ describeRunIf(process.env.TEST_ENV !== 'node',
131138
expect(wrapper.html()).to.equal(`<div>foo</div>`)
132139
})
133140

141+
it('overrides methods', () => {
142+
const stub = sinon.stub()
143+
const TestComponent = Vue.extend({
144+
template: '<div />',
145+
methods: {
146+
callStub () {
147+
stub()
148+
}
149+
}
150+
})
151+
mount(TestComponent, {
152+
methods: {
153+
callStub () {}
154+
}
155+
}).vm.callStub()
156+
157+
expect(stub).not.called
158+
})
159+
134160
// Problems accessing options of twice extended components in Vue < 2.3
135161
itDoNotRunIf(vueVersion < 2.3,
136162
'compiles extended components', () => {
@@ -195,6 +221,20 @@ describeRunIf(process.env.TEST_ENV !== 'node',
195221
expect(wrapper.vm.$options.listeners).to.equal(undefined)
196222
})
197223

224+
it('injects store correctly', () => {
225+
const localVue = createLocalVue()
226+
localVue.use(Vuex)
227+
const store = new Vuex.Store()
228+
const wrapper = mount(ComponentAsAClass, {
229+
store,
230+
localVue
231+
})
232+
wrapper.vm.getters
233+
mount({
234+
template: '<div>{{$store.getters}}</div>'
235+
}, { store, localVue })
236+
})
237+
198238
it('propagates errors when they are thrown', () => {
199239
const TestComponent = {
200240
template: '<div></div>',
@@ -261,7 +301,7 @@ describeRunIf(process.env.TEST_ENV !== 'node',
261301
Vue.config.errorHandler = null
262302
})
263303

264-
it('overwrites the component options with the options other than the mounting options when the options for mount contain those', () => {
304+
it('overwrites the component options with the instance options', () => {
265305
const Component = {
266306
template: '<div>{{ foo() }}{{ bar() }}{{ baz() }}</div>',
267307
methods: {

0 commit comments

Comments
 (0)