From a556c94e0934df24b28d268da1a59f6cf6cccca2 Mon Sep 17 00:00:00 2001 From: 38elements Date: Sat, 26 May 2018 17:08:14 +0900 Subject: [PATCH] add updated hook --- .../test-utils/src/set-watchers-to-sync.js | 14 +++++++ test/specs/mounting-options/sync.spec.js | 37 ++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/test-utils/src/set-watchers-to-sync.js b/packages/test-utils/src/set-watchers-to-sync.js index 93fd85d7a..e6cdf0161 100644 --- a/packages/test-utils/src/set-watchers-to-sync.js +++ b/packages/test-utils/src/set-watchers-to-sync.js @@ -1,3 +1,5 @@ +import { VUE_VERSION } from './consts' + function setDepsSync (dep) { dep.subs.forEach(setWatcherSync) } @@ -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) + }) + } + } + } } diff --git a/test/specs/mounting-options/sync.spec.js b/test/specs/mounting-options/sync.spec.js index d387351e0..2268c4e0b 100644 --- a/test/specs/mounting-options/sync.spec.js +++ b/test/specs/mounting-options/sync.spec.js @@ -1,3 +1,4 @@ +import sinon from 'sinon' import { describeWithShallowAndMount } from '~resources/utils' describeWithShallowAndMount('options.sync', (mountingMethod) => { @@ -46,7 +47,7 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => {
computed.text: {{ computedText }}
- + `, data () { return { @@ -110,4 +111,38 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => { done() }) }) + + it('call updated when sync is not false', () => { + const childComponentSpy = sinon.stub() + const ChildComponent = { + template: '
{{ foo }}
', + props: ['foo'], + updated () { + childComponentSpy() + } + } + const spy = sinon.stub() + const TestComponent = { + template: '
{{ foo }}
', + 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('
foo
foo
') + wrapper.vm.foo = 'bar' + expect(spy.calledOnce).to.equal(true) + expect(childComponentSpy.calledOnce).to.equal(true) + expect(wrapper.html()).to.equal('
bar
bar
') + }) })