Skip to content

Commit

Permalink
fix: add updated hook (#675)
Browse files Browse the repository at this point in the history
closes #661
  • Loading branch information
38elements authored and eddyerburgh committed Jun 1, 2018
1 parent 0ab5a75 commit 9a2a25a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/test-utils/src/set-watchers-to-sync.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { VUE_VERSION } from './consts'

function setDepsSync (dep) {
dep.subs.forEach(setWatcherSync)
}
Expand All @@ -24,4 +26,16 @@ export function setWatchersToSync (vm) {
setWatcherSync(vm._watcher)

vm.$children.forEach(setWatchersToSync)
// preventing double registration
if (!vm.$_vueTestUtils_updateInSetWatcherSync) {
vm.$_vueTestUtils_updateInSetWatcherSync = vm._update
vm._update = function (vnode, hydrating) {
this.$_vueTestUtils_updateInSetWatcherSync(vnode, hydrating)
if (VUE_VERSION >= 2.1 && this._isMounted && this.$options.updated) {
this.$options.updated.forEach((handler) => {
handler.call(this)
})
}
}
}
}
37 changes: 36 additions & 1 deletion test/specs/mounting-options/sync.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sinon from 'sinon'
import { describeWithShallowAndMount } from '~resources/utils'

describeWithShallowAndMount('options.sync', (mountingMethod) => {
Expand Down Expand Up @@ -46,7 +47,7 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => {
<pre>computed.text: <em>{{ computedText }}</em></pre>
</div>
</div>
</div>
</div>
`,
data () {
return {
Expand Down Expand Up @@ -110,4 +111,38 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => {
done()
})
})

it('call updated when sync is not false', () => {
const childComponentSpy = sinon.stub()
const ChildComponent = {
template: '<div>{{ foo }}</div>',
props: ['foo'],
updated () {
childComponentSpy()
}
}
const spy = sinon.stub()
const TestComponent = {
template: '<div>{{ foo }}<child-component :foo="foo" /></div>',
data () {
return {
foo: 'foo'
}
},
updated () {
spy()
}
}
const wrapper = mountingMethod(TestComponent, {
stubs: { 'child-component': ChildComponent },
sync: true
})
expect(spy.notCalled).to.equal(true)
expect(childComponentSpy.notCalled).to.equal(true)
expect(wrapper.html()).to.equal('<div>foo<div>foo</div></div>')
wrapper.vm.foo = 'bar'
expect(spy.calledOnce).to.equal(true)
expect(childComponentSpy.calledOnce).to.equal(true)
expect(wrapper.html()).to.equal('<div>bar<div>bar</div></div>')
})
})

0 comments on commit 9a2a25a

Please sign in to comment.