Skip to content

Override component watcher with mounting options #1392

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
Jan 6, 2020
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
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')
})
})