From 9f40996e45b7639e5cbb6623f67c9dcd00717e08 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Mon, 6 Jan 2020 22:11:13 +1000 Subject: [PATCH 1/5] fix: add a failing test --- test/specs/mounting-options/watch.spec.js | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/specs/mounting-options/watch.spec.js diff --git a/test/specs/mounting-options/watch.spec.js b/test/specs/mounting-options/watch.spec.js new file mode 100644 index 000000000..09d0d0f13 --- /dev/null +++ b/test/specs/mounting-options/watch.spec.js @@ -0,0 +1,41 @@ +import { + describeWithShallowAndMount, + isRunningPhantomJS, + vueVersion +} from '~resources/utils' +import { itSkipIf, itDoNotRunIf } from 'conditional-specs' + +describeWithShallowAndMount('options.watch', mountingMethod => { + it('overrides a default watch handler', async () => { + const TestComponent = { + props: ['someProp'], + template: '
{{ foo }}
', + data() { + return { + foo: 'bar' + } + }, + watch: { + someProp: { + handler() { + this.foo = 'updated-bar' + } + } + } + } + const wrapper = mountingMethod(TestComponent, { + watch: { + someProp: { + handler() { + // do nothing + } + } + } + }) + + wrapper.setProps({ someProp: 'some-new-val' }) + await wrapper.vm.$nextTick() + + expect(wrapper.text()).to.equal('bar') + }) +}) From 0f0c2606f0ba06486bf0a199d6327e392f1e18e9 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Mon, 6 Jan 2020 23:07:40 +1000 Subject: [PATCH 2/5] fix: override instance watchers with mounting options watchers --- packages/create-instance/create-instance.js | 18 ++++++++++++++++++ test/specs/mounting-options/watch.spec.js | 13 +++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/create-instance/create-instance.js b/packages/create-instance/create-instance.js index c8db93446..e84cd8e03 100644 --- a/packages/create-instance/create-instance.js +++ b/packages/create-instance/create-instance.js @@ -73,6 +73,24 @@ export default function createInstance( // make sure all extends are based on this instance + if (instanceOptions.watch) { + console.log(instanceOptions.watch) + } + + // watchers provided in mounting options should override preexisting ones + if (componentOptions.watch && instanceOptions.watch) { + const componentWatchers = Object.keys(componentOptions.watch) + const instanceWatchers = Object.keys(instanceOptions.watch) + + for (let i = 0; i < instanceWatchers.length; i++) { + const k = instanceWatchers[i] + // override the componentOptions with the one provided in mounting options + if (componentWatchers.includes(k)) { + componentOptions.watch[k] = instanceOptions.watch[k] + } + } + } + const Constructor = _Vue.extend(componentOptions).extend(instanceOptions) componentOptions._Ctor = {} Constructor.options._base = _Vue diff --git a/test/specs/mounting-options/watch.spec.js b/test/specs/mounting-options/watch.spec.js index 09d0d0f13..50cf48ca0 100644 --- a/test/specs/mounting-options/watch.spec.js +++ b/test/specs/mounting-options/watch.spec.js @@ -1,14 +1,10 @@ -import { - describeWithShallowAndMount, - isRunningPhantomJS, - vueVersion -} from '~resources/utils' +import { describeWithShallowAndMount } from '~resources/utils' import { itSkipIf, itDoNotRunIf } from 'conditional-specs' describeWithShallowAndMount('options.watch', mountingMethod => { it('overrides a default watch handler', async () => { const TestComponent = { - props: ['someProp'], + props: ['someProp', 'anotherProp'], template: '
{{ foo }}
', data() { return { @@ -16,6 +12,11 @@ describeWithShallowAndMount('options.watch', mountingMethod => { } }, watch: { + anotherProp: { + handler() { + // placeholder + } + }, someProp: { handler() { this.foo = 'updated-bar' From 9196c640b603fa15fa1e44c0931a3585b5d00896 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Mon, 6 Jan 2020 23:27:07 +1000 Subject: [PATCH 3/5] fix: remove unused import --- test/specs/mounting-options/watch.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/specs/mounting-options/watch.spec.js b/test/specs/mounting-options/watch.spec.js index 50cf48ca0..73e47daf9 100644 --- a/test/specs/mounting-options/watch.spec.js +++ b/test/specs/mounting-options/watch.spec.js @@ -1,5 +1,4 @@ import { describeWithShallowAndMount } from '~resources/utils' -import { itSkipIf, itDoNotRunIf } from 'conditional-specs' describeWithShallowAndMount('options.watch', mountingMethod => { it('overrides a default watch handler', async () => { From 8b3e7f91f876d799f7c50252b61da1a29bf8f74b Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Mon, 6 Jan 2020 23:35:14 +1000 Subject: [PATCH 4/5] fix: refactor spec --- test/specs/mounting-options/watch.spec.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/specs/mounting-options/watch.spec.js b/test/specs/mounting-options/watch.spec.js index 73e47daf9..f92dfbbed 100644 --- a/test/specs/mounting-options/watch.spec.js +++ b/test/specs/mounting-options/watch.spec.js @@ -3,7 +3,7 @@ import { describeWithShallowAndMount } from '~resources/utils' describeWithShallowAndMount('options.watch', mountingMethod => { it('overrides a default watch handler', async () => { const TestComponent = { - props: ['someProp', 'anotherProp'], + props: ['someProp'], template: '
{{ foo }}
', data() { return { @@ -11,11 +11,6 @@ describeWithShallowAndMount('options.watch', mountingMethod => { } }, watch: { - anotherProp: { - handler() { - // placeholder - } - }, someProp: { handler() { this.foo = 'updated-bar' From 75b71764b4ea76bf06634286ecc0d2ca4c905413 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Mon, 6 Jan 2020 23:45:17 +1000 Subject: [PATCH 5/5] fix: remove console.log --- packages/create-instance/create-instance.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/create-instance/create-instance.js b/packages/create-instance/create-instance.js index e84cd8e03..fb7cfc292 100644 --- a/packages/create-instance/create-instance.js +++ b/packages/create-instance/create-instance.js @@ -71,12 +71,6 @@ export default function createInstance( // used to identify extended component using constructor componentOptions.$_vueTestUtils_original = component - // make sure all extends are based on this instance - - if (instanceOptions.watch) { - console.log(instanceOptions.watch) - } - // watchers provided in mounting options should override preexisting ones if (componentOptions.watch && instanceOptions.watch) { const componentWatchers = Object.keys(componentOptions.watch) @@ -91,6 +85,7 @@ export default function createInstance( } } + // make sure all extends are based on this instance const Constructor = _Vue.extend(componentOptions).extend(instanceOptions) componentOptions._Ctor = {} Constructor.options._base = _Vue