Skip to content

fix: extend components correctly #709

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 5 commits into from
Jun 12, 2018
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
10 changes: 2 additions & 8 deletions packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @flow

import Vue from 'vue'
import { createSlotVNodes } from './add-slots'
import addMocks from './add-mocks'
import { addEventLogger } from './log-events'
Expand Down Expand Up @@ -39,10 +38,7 @@ export default function createInstance (
addEventLogger(_Vue)

const instanceOptions = {
...options,
propsData: {
...options.propsData
}
...options
}

deleteMountingOptions(instanceOptions)
Expand Down Expand Up @@ -71,12 +67,10 @@ export default function createInstance (
_Vue.component(c, stubComponents[c])
})

const Constructor = (typeof component === 'function' && component.prototype instanceof Vue)
const Constructor = vueVersion < 2.3 && typeof component === 'function'
? component.extend(instanceOptions)
: _Vue.extend(component).extend(instanceOptions)

// const Constructor = _Vue.extend(component).extend(instanceOptions)

Object.keys(instanceOptions.components || {}).forEach(key => {
Constructor.component(key, instanceOptions.components[key])
_Vue.component(key, instanceOptions.components[key])
Expand Down
44 changes: 42 additions & 2 deletions test/specs/mount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { mount, createLocalVue } from '~vue/test-utils'
import Component from '~resources/components/component.vue'
import ComponentWithProps from '~resources/components/component-with-props.vue'
import ComponentWithMixin from '~resources/components/component-with-mixin.vue'
import ComponentAsAClass from '~resources/components/component-as-a-class.vue'
import { injectSupported, vueVersion } from '~resources/utils'
import {
describeRunIf,
itDoNotRunIf
} from 'conditional-specs'
import Vuex from 'vuex'

describeRunIf(process.env.TEST_ENV !== 'node',
'mount', () => {
Expand Down Expand Up @@ -62,7 +64,12 @@ describeRunIf(process.env.TEST_ENV !== 'node',

it('returns new VueWrapper with mounted Vue instance initialized with Vue.extend with props, if passed as propsData', () => {
const prop1 = { test: 'TEST' }
const wrapper = mount(Vue.extend(ComponentWithProps), { propsData: { prop1 }})
const TestComponent = Vue.extend(ComponentWithProps)
const wrapper = mount(TestComponent, {
propsData: {
prop1
}
})
expect(wrapper.vm).to.be.an('object')
if (wrapper.vm.$props) {
expect(wrapper.vm.$props.prop1).to.equal(prop1)
Expand Down Expand Up @@ -131,6 +138,25 @@ describeRunIf(process.env.TEST_ENV !== 'node',
expect(wrapper.html()).to.equal(`<div>foo</div>`)
})

it('overrides methods', () => {
const stub = sinon.stub()
const TestComponent = Vue.extend({
template: '<div />',
methods: {
callStub () {
stub()
}
}
})
mount(TestComponent, {
methods: {
callStub () {}
}
}).vm.callStub()

expect(stub).not.called
})

// Problems accessing options of twice extended components in Vue < 2.3
itDoNotRunIf(vueVersion < 2.3,
'compiles extended components', () => {
Expand Down Expand Up @@ -195,6 +221,20 @@ describeRunIf(process.env.TEST_ENV !== 'node',
expect(wrapper.vm.$options.listeners).to.equal(undefined)
})

it('injects store correctly', () => {
const localVue = createLocalVue()
localVue.use(Vuex)
const store = new Vuex.Store()
const wrapper = mount(ComponentAsAClass, {
store,
localVue
})
wrapper.vm.getters
mount({
template: '<div>{{$store.getters}}</div>'
}, { store, localVue })
})

it('propagates errors when they are thrown', () => {
const TestComponent = {
template: '<div></div>',
Expand Down Expand Up @@ -261,7 +301,7 @@ describeRunIf(process.env.TEST_ENV !== 'node',
Vue.config.errorHandler = null
})

it('overwrites the component options with the options other than the mounting options when the options for mount contain those', () => {
it('overwrites the component options with the instance options', () => {
const Component = {
template: '<div>{{ foo() }}{{ bar() }}{{ baz() }}</div>',
methods: {
Expand Down