Skip to content

Commit ee09270

Browse files
authored
Merge pull request #1392 from vuejs/issue-1391-watchers
Override component watcher with mounting options
2 parents e7b8d50 + 75b7176 commit ee09270

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/create-instance/create-instance.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,21 @@ export default function createInstance(
7171
// used to identify extended component using constructor
7272
componentOptions.$_vueTestUtils_original = component
7373

74-
// make sure all extends are based on this instance
74+
// watchers provided in mounting options should override preexisting ones
75+
if (componentOptions.watch && instanceOptions.watch) {
76+
const componentWatchers = Object.keys(componentOptions.watch)
77+
const instanceWatchers = Object.keys(instanceOptions.watch)
78+
79+
for (let i = 0; i < instanceWatchers.length; i++) {
80+
const k = instanceWatchers[i]
81+
// override the componentOptions with the one provided in mounting options
82+
if (componentWatchers.includes(k)) {
83+
componentOptions.watch[k] = instanceOptions.watch[k]
84+
}
85+
}
86+
}
7587

88+
// make sure all extends are based on this instance
7689
const Constructor = _Vue.extend(componentOptions).extend(instanceOptions)
7790
componentOptions._Ctor = {}
7891
Constructor.options._base = _Vue
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describeWithShallowAndMount } from '~resources/utils'
2+
3+
describeWithShallowAndMount('options.watch', mountingMethod => {
4+
it('overrides a default watch handler', async () => {
5+
const TestComponent = {
6+
props: ['someProp'],
7+
template: '<div>{{ foo }}</div>',
8+
data() {
9+
return {
10+
foo: 'bar'
11+
}
12+
},
13+
watch: {
14+
someProp: {
15+
handler() {
16+
this.foo = 'updated-bar'
17+
}
18+
}
19+
}
20+
}
21+
const wrapper = mountingMethod(TestComponent, {
22+
watch: {
23+
someProp: {
24+
handler() {
25+
// do nothing
26+
}
27+
}
28+
}
29+
})
30+
31+
wrapper.setProps({ someProp: 'some-new-val' })
32+
await wrapper.vm.$nextTick()
33+
34+
expect(wrapper.text()).to.equal('bar')
35+
})
36+
})

0 commit comments

Comments
 (0)