Skip to content

Commit 9a2a25a

Browse files
38elementseddyerburgh
authored andcommitted
fix: add updated hook (#675)
closes #661
1 parent 0ab5a75 commit 9a2a25a

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/test-utils/src/set-watchers-to-sync.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { VUE_VERSION } from './consts'
2+
13
function setDepsSync (dep) {
24
dep.subs.forEach(setWatcherSync)
35
}
@@ -24,4 +26,16 @@ export function setWatchersToSync (vm) {
2426
setWatcherSync(vm._watcher)
2527

2628
vm.$children.forEach(setWatchersToSync)
29+
// preventing double registration
30+
if (!vm.$_vueTestUtils_updateInSetWatcherSync) {
31+
vm.$_vueTestUtils_updateInSetWatcherSync = vm._update
32+
vm._update = function (vnode, hydrating) {
33+
this.$_vueTestUtils_updateInSetWatcherSync(vnode, hydrating)
34+
if (VUE_VERSION >= 2.1 && this._isMounted && this.$options.updated) {
35+
this.$options.updated.forEach((handler) => {
36+
handler.call(this)
37+
})
38+
}
39+
}
40+
}
2741
}

test/specs/mounting-options/sync.spec.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sinon from 'sinon'
12
import { describeWithShallowAndMount } from '~resources/utils'
23

34
describeWithShallowAndMount('options.sync', (mountingMethod) => {
@@ -46,7 +47,7 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => {
4647
<pre>computed.text: <em>{{ computedText }}</em></pre>
4748
</div>
4849
</div>
49-
</div>
50+
</div>
5051
`,
5152
data () {
5253
return {
@@ -110,4 +111,38 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => {
110111
done()
111112
})
112113
})
114+
115+
it('call updated when sync is not false', () => {
116+
const childComponentSpy = sinon.stub()
117+
const ChildComponent = {
118+
template: '<div>{{ foo }}</div>',
119+
props: ['foo'],
120+
updated () {
121+
childComponentSpy()
122+
}
123+
}
124+
const spy = sinon.stub()
125+
const TestComponent = {
126+
template: '<div>{{ foo }}<child-component :foo="foo" /></div>',
127+
data () {
128+
return {
129+
foo: 'foo'
130+
}
131+
},
132+
updated () {
133+
spy()
134+
}
135+
}
136+
const wrapper = mountingMethod(TestComponent, {
137+
stubs: { 'child-component': ChildComponent },
138+
sync: true
139+
})
140+
expect(spy.notCalled).to.equal(true)
141+
expect(childComponentSpy.notCalled).to.equal(true)
142+
expect(wrapper.html()).to.equal('<div>foo<div>foo</div></div>')
143+
wrapper.vm.foo = 'bar'
144+
expect(spy.calledOnce).to.equal(true)
145+
expect(childComponentSpy.calledOnce).to.equal(true)
146+
expect(wrapper.html()).to.equal('<div>bar<div>bar</div></div>')
147+
})
113148
})

0 commit comments

Comments
 (0)