Skip to content

Commit 8712aca

Browse files
committed
fix: run watchers when setComputed runs
1 parent b21a496 commit 8712aca

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/wrappers/wrapper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,9 @@ export default class Wrapper implements BaseWrapper {
413413
}
414414
})
415415
}
416+
this.vm._watchers.forEach((watcher) => {
417+
if (watcher.expression === key) { watcher.run() }
418+
})
416419
})
417420
this.update()
418421
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
prop1: { default: 'default prop1' },
1818
prop2: { default: 'default prop2' }
1919
},
20+
computed: {
21+
computed1: () => 'some value'
22+
},
2023
watch: {
2124
prop1 (val) {
2225
this.prop2 = val
@@ -29,6 +32,9 @@
2932
},
3033
data2 () {
3134
console.info(this.data1)
35+
},
36+
computed1 () {
37+
console.info(this.computed1)
3238
}
3339
}
3440
}

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

Lines changed: 18 additions & 0 deletions
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 ComponentWithComputed from '~resources/components/component-with-computed.vue'
4+
import ComponentWithWatch from '~resources/components/component-with-watch.vue'
45

56
describe('setComputed', () => {
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 computed props and updates when called on Vue instance', () => {
718
const wrapper = mount(ComponentWithComputed)
819
expect(wrapper.text()).to.contain('message')
@@ -16,6 +27,13 @@ describe('setComputed', () => {
1627
expect(() => wrapper.setComputed({ noExist: '' })).throw(Error, message)
1728
})
1829

30+
it('runs watch function after all props are updated', () => {
31+
const wrapper = mount(ComponentWithWatch)
32+
const computed1 = 'new computed'
33+
wrapper.setComputed({ computed1 })
34+
expect(info.args[0][0]).to.equal(computed1)
35+
})
36+
1937
it('throws an error if node is not a Vue instance', () => {
2038
const message = 'wrapper.setComputed() can only be called on a Vue instance'
2139
const compiled = compileToFunctions('<div><p></p></div>')

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('setData', () => {
3737
expect(wrapper.hasClass('some-class')).to.be.true
3838
})
3939

40-
it('runs watch function when prop is updated', () => {
40+
it('runs watch function when data is updated', () => {
4141
const wrapper = mount(ComponentWithWatch)
4242
const data1 = 'testest'
4343
wrapper.setData({ data1 })

0 commit comments

Comments
 (0)