Skip to content

Commit ebccc8d

Browse files
committed
fix(setComputed): permanently update computed watcher when called
fixes #273
1 parent e78ac8e commit ebccc8d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/wrappers/wrapper.js

+3
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ export default class Wrapper implements BaseWrapper {
413413
}
414414
// $FlowIgnore : Problem with possibly null this.vm
415415
this.vm._computedWatchers[key].value = computed[key]
416+
this.vm._computedWatchers[key].getter = () => computed[key]
417+
416418
} else {
417419
// $FlowIgnore : Problem with possibly null this.vm
418420
if (!this.vm._watchers.some(w => w.getter.name === key)) {
@@ -422,6 +424,7 @@ export default class Wrapper implements BaseWrapper {
422424
this.vm._watchers.forEach((watcher) => {
423425
if (watcher.getter.name === key) {
424426
watcher.value = computed[key]
427+
watcher.getter = () => computed[key]
425428
}
426429
})
427430
}

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

+20
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ describe('setComputed', () => {
3232
const computed1 = 'new computed'
3333
wrapper.setComputed({ computed1 })
3434
expect(info.args[0][0]).to.equal(computed1)
35+
expect(wrapper.vm.computed1).to.equal(computed1)
36+
})
37+
38+
it('updates vm computed value', () => {
39+
const TestComponent = {
40+
data () {
41+
return {
42+
a: 1
43+
}
44+
},
45+
computed: {
46+
b () {
47+
return this.a * 2
48+
}
49+
}
50+
}
51+
52+
const wrapper = mount(TestComponent)
53+
wrapper.setComputed({b: 3})
54+
expect(wrapper.vm.b).to.equal(3)
3555
})
3656

3757
it('throws an error if node is not a Vue instance', () => {

0 commit comments

Comments
 (0)