Skip to content

Commit 4fae8da

Browse files
committed
fix: run watchers in setData
Closes #149
1 parent 23bbe0b commit 4fae8da

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/wrappers/wrapper.js

+8
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ export default class Wrapper implements BaseWrapper {
293293
// $FlowIgnore : Problem with possibly null this.vm
294294
this.vm.$set(this.vm, [key], data[key])
295295
})
296+
297+
Object.keys(data).forEach((key) => {
298+
// $FlowIgnore : Problem with possibly null this.vm
299+
this.vm._watchers.forEach((watcher) => {
300+
if (watcher.expression === key) { watcher.run() }
301+
})
302+
})
303+
296304
this.update()
297305
}
298306

test/resources/components/component-with-watch.vue

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
<script>
99
export default {
10+
data: function () {
11+
return {
12+
data1: 'default data1',
13+
data2: 'default data2'
14+
}
15+
},
1016
props: {
1117
prop1: { default: 'default prop1' },
1218
prop2: { default: 'default prop2' }
@@ -17,6 +23,12 @@
1723
},
1824
prop2 () {
1925
console.info(this.prop1)
26+
},
27+
data1 (val) {
28+
this.data2 = val
29+
},
30+
data2 () {
31+
console.info(this.data1)
2032
}
2133
}
2234
}

test/unit/specs/mount/Wrapper/setData.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { compileToFunctions } from 'vue-template-compiler'
22
import mount from '~src/mount'
33
import ComponentWithVIf from '~resources/components/component-with-v-if.vue'
4+
import ComponentWithWatch from '~resources/components/component-with-watch.vue'
45

56
describe('setData', () => {
7+
let info
8+
9+
beforeEach(() => {
10+
info = sinon.stub(console, 'info')
11+
})
12+
13+
afterEach(() => {
14+
info.restore()
15+
})
16+
617
it('sets component data and updates nested vm nodes when called on Vue instance', () => {
718
const wrapper = mount(ComponentWithVIf)
819
expect(wrapper.findAll('.child.ready').length).to.equal(0)
@@ -26,6 +37,20 @@ describe('setData', () => {
2637
expect(wrapper.hasClass('some-class')).to.be.true
2738
})
2839

40+
it('runs watch function when prop is updated', () => {
41+
const wrapper = mount(ComponentWithWatch)
42+
const data1 = 'testest'
43+
wrapper.setData({ data1 })
44+
expect(wrapper.vm.data2).to.equal(data1)
45+
})
46+
47+
it('runs watch function after all props are updated', () => {
48+
const wrapper = mount(ComponentWithWatch)
49+
const data1 = 'testest'
50+
wrapper.setData({ data2: 'newProp', data1 })
51+
expect(info.args[0][0]).to.equal(data1)
52+
})
53+
2954
it('throws an error if node is not a Vue instance', () => {
3055
const message = 'wrapper.setData() can only be called on a Vue instance'
3156
const compiled = compileToFunctions('<div><p></p></div>')

0 commit comments

Comments
 (0)