Skip to content

Commit

Permalink
Merge pull request #1392 from vuejs/issue-1391-watchers
Browse files Browse the repository at this point in the history
Override component watcher with mounting options
  • Loading branch information
JessicaSachs authored Jan 6, 2020
2 parents e7b8d50 + 75b7176 commit ee09270
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,21 @@ export default function createInstance(
// used to identify extended component using constructor
componentOptions.$_vueTestUtils_original = component

// make sure all extends are based on this instance
// 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]
}
}
}

// make sure all extends are based on this instance
const Constructor = _Vue.extend(componentOptions).extend(instanceOptions)
componentOptions._Ctor = {}
Constructor.options._base = _Vue
Expand Down
36 changes: 36 additions & 0 deletions test/specs/mounting-options/watch.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describeWithShallowAndMount } from '~resources/utils'

describeWithShallowAndMount('options.watch', mountingMethod => {
it('overrides a default watch handler', async () => {
const TestComponent = {
props: ['someProp'],
template: '<div>{{ foo }}</div>',
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')
})
})

0 comments on commit ee09270

Please sign in to comment.