From ac5845e620b903a4eb4e831a7f627c5018136036 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Tue, 12 Jun 2018 20:54:12 +0100 Subject: [PATCH 1/5] fix: extend components correctly --- packages/create-instance/create-instance.js | 7 +------ test/specs/mount.spec.js | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/create-instance/create-instance.js b/packages/create-instance/create-instance.js index c459671a3..b9d18b8d7 100644 --- a/packages/create-instance/create-instance.js +++ b/packages/create-instance/create-instance.js @@ -1,6 +1,5 @@ // @flow -import Vue from 'vue' import { createSlotVNodes } from './add-slots' import addMocks from './add-mocks' import { addEventLogger } from './log-events' @@ -71,11 +70,7 @@ export default function createInstance ( _Vue.component(c, stubComponents[c]) }) - const Constructor = (typeof component === 'function' && component.prototype instanceof Vue) - ? component.extend(instanceOptions) - : _Vue.extend(component).extend(instanceOptions) - - // const Constructor = _Vue.extend(component).extend(instanceOptions) + const Constructor = _Vue.extend(component).extend(instanceOptions) Object.keys(instanceOptions.components || {}).forEach(key => { Constructor.component(key, instanceOptions.components[key]) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 15e40908c..1427c4b00 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -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', () => { @@ -195,6 +197,17 @@ describeRunIf(process.env.TEST_ENV !== 'node', expect(wrapper.vm.$options.listeners).to.equal(undefined) }) + it('handles store correctly', () => { + const localVue = createLocalVue() + localVue.use(Vuex) + const store = new Vuex.Store() + const wrapper = mount(ComponentAsAClass, { + store, + localVue + }) + console.log(wrapper.vm.$store.getters) + }) + it('propagates errors when they are thrown', () => { const TestComponent = { template: '
', @@ -261,7 +274,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: '
{{ foo() }}{{ bar() }}{{ baz() }}
', methods: { From 122ca1184c21b44cf64eae124cbe254ee67ff0da Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Tue, 12 Jun 2018 21:22:51 +0100 Subject: [PATCH 2/5] test: add test for component --- test/specs/mount.spec.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 1427c4b00..f814e54ea 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -197,7 +197,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', expect(wrapper.vm.$options.listeners).to.equal(undefined) }) - it('handles store correctly', () => { + it('injects store correctly', () => { const localVue = createLocalVue() localVue.use(Vuex) const store = new Vuex.Store() @@ -205,7 +205,10 @@ describeRunIf(process.env.TEST_ENV !== 'node', store, localVue }) - console.log(wrapper.vm.$store.getters) + wrapper.vm.getters + mount({ + template: '
{{$store.getters}}
' + }, { store, localVue }) }) it('propagates errors when they are thrown', () => { From cacfda8acb2dfed54295b82fbd93e84205cec9d3 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Tue, 12 Jun 2018 22:06:03 +0100 Subject: [PATCH 3/5] fix: handle Vue < 2.3 --- packages/create-instance/create-instance.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/create-instance/create-instance.js b/packages/create-instance/create-instance.js index b9d18b8d7..8ad1b4af7 100644 --- a/packages/create-instance/create-instance.js +++ b/packages/create-instance/create-instance.js @@ -38,10 +38,10 @@ export default function createInstance ( addEventLogger(_Vue) const instanceOptions = { - ...options, - propsData: { - ...options.propsData - } + ...options + // propsData: { + // ...options.propsData + // } } deleteMountingOptions(instanceOptions) @@ -70,7 +70,9 @@ export default function createInstance ( _Vue.component(c, stubComponents[c]) }) - const Constructor = _Vue.extend(component).extend(instanceOptions) + const Constructor = vueVersion < 2.3 && typeof component === 'function' + ? component + : _Vue.extend(component).extend(instanceOptions) Object.keys(instanceOptions.components || {}).forEach(key => { Constructor.component(key, instanceOptions.components[key]) From fee03607953a872d69757a15a615dc119604b2c4 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Tue, 12 Jun 2018 22:07:13 +0100 Subject: [PATCH 4/5] refactor: remove commented propsData --- packages/create-instance/create-instance.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/create-instance/create-instance.js b/packages/create-instance/create-instance.js index 8ad1b4af7..bf869906d 100644 --- a/packages/create-instance/create-instance.js +++ b/packages/create-instance/create-instance.js @@ -39,9 +39,6 @@ export default function createInstance ( const instanceOptions = { ...options - // propsData: { - // ...options.propsData - // } } deleteMountingOptions(instanceOptions) From dcbf0f65d3d1380376ba4158e9651f5a964a41a9 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Tue, 12 Jun 2018 22:35:25 +0100 Subject: [PATCH 5/5] fix: add test for instance options] --- packages/create-instance/create-instance.js | 2 +- test/specs/mount.spec.js | 26 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/create-instance/create-instance.js b/packages/create-instance/create-instance.js index bf869906d..099befd20 100644 --- a/packages/create-instance/create-instance.js +++ b/packages/create-instance/create-instance.js @@ -68,7 +68,7 @@ export default function createInstance ( }) const Constructor = vueVersion < 2.3 && typeof component === 'function' - ? component + ? component.extend(instanceOptions) : _Vue.extend(component).extend(instanceOptions) Object.keys(instanceOptions.components || {}).forEach(key => { diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index f814e54ea..b6564acf1 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -64,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) @@ -133,6 +138,25 @@ describeRunIf(process.env.TEST_ENV !== 'node', expect(wrapper.html()).to.equal(`
foo
`) }) + it('overrides methods', () => { + const stub = sinon.stub() + const TestComponent = Vue.extend({ + template: '
', + 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', () => {