From 51f8a14d6885457077cbe8321064c11b8446ccb2 Mon Sep 17 00:00:00 2001 From: 38elements Date: Sat, 26 May 2018 17:08:14 +0900 Subject: [PATCH 1/4] add updated hook --- .../test-utils/src/set-watchers-to-sync.js | 14 +++++++++++ test/specs/mounting-options/sync.spec.js | 24 ++++++++++++++++++- 2 files changed, 37 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..7577a1515 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) + + if (!vm.$_vueTestUtils_update) { + vm.$_vueTestUtils_update = vm._update + vm._update = function (vnode, hydrating) { + this.$_vueTestUtils_update(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..7426d1ce4 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,25 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => { done() }) }) + + it('call updated when sync is not false', () => { + const spy = sinon.stub() + const TestComponent = { + template: '
{{ foo }}
', + data () { + return { + foo: 'foo' + } + }, + updated () { + spy() + } + } + const wrapper = mountingMethod(TestComponent, { + sync: true + }) + expect(spy.notCalled).to.equal(true) + wrapper.vm.foo = 'bar' + expect(spy.calledOnce).to.equal(true) + }) }) From d09045a6610ff7ea7269600a86eea76d5c5a74f6 Mon Sep 17 00:00:00 2001 From: 38elements Date: Sun, 27 May 2018 13:40:55 +0900 Subject: [PATCH 2/4] fix #582 --- packages/create-instance/create-instance.js | 14 ++++++++++++++ packages/test-utils/src/set-watchers-to-sync.js | 7 ++++--- test/specs/mounting-options/slots.spec.js | 1 + 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/create-instance/create-instance.js b/packages/create-instance/create-instance.js index c898222f3..f5a7e8321 100644 --- a/packages/create-instance/create-instance.js +++ b/packages/create-instance/create-instance.js @@ -1,6 +1,7 @@ // @flow import Vue from 'vue' +import cloneDeep from 'lodash/cloneDeep' import { addSlots } from './add-slots' import { addScopedSlots } from './add-scoped-slots' import addMocks from './add-mocks' @@ -92,6 +93,19 @@ export default function createInstance ( addAttrs(vm, options.attrs) addListeners(vm, options.listeners) + vm.$_vueTestUtils_mountingOptionsSlots = options.slots + vm.$_vueTestUtils_originalSlots = cloneDeep(vm.$slots) + vm.$_vueTestUtils_originalUpdate = vm._update + vm._update = function (vnode, hydrating) { + // updating slot + if (this.$_vueTestUtils_mountingOptionsSlots) { + this.$slots = cloneDeep(this.$_vueTestUtils_originalSlots) + addSlots(this, this.$_vueTestUtils_mountingOptionsSlots) + vnode = this._render() + } + this.$_vueTestUtils_originalUpdate(vnode, hydrating) + } + if (options.scopedSlots) { if (window.navigator.userAgent.match(/PhantomJS/i)) { throwError('the scopedSlots option does not support PhantomJS. Please use Puppeteer, or pass a component.') diff --git a/packages/test-utils/src/set-watchers-to-sync.js b/packages/test-utils/src/set-watchers-to-sync.js index 7577a1515..b6b2a0ee0 100644 --- a/packages/test-utils/src/set-watchers-to-sync.js +++ b/packages/test-utils/src/set-watchers-to-sync.js @@ -27,10 +27,11 @@ export function setWatchersToSync (vm) { vm.$children.forEach(setWatchersToSync) - if (!vm.$_vueTestUtils_update) { - vm.$_vueTestUtils_update = vm._update + // preventing double registration + if (!vm.$_vueTestUtils_updateInSetWatcherSync) { + vm.$_vueTestUtils_updateInSetWatcherSync = vm._update vm._update = function (vnode, hydrating) { - this.$_vueTestUtils_update(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/slots.spec.js b/test/specs/mounting-options/slots.spec.js index d4a779c05..aa1b0f7bf 100644 --- a/test/specs/mounting-options/slots.spec.js +++ b/test/specs/mounting-options/slots.spec.js @@ -113,6 +113,7 @@ describeWithMountingMethods('options.slots', (mountingMethod) => { const wrapper5 = mountingMethod(ComponentWithSlots, { slots: { default: '1{{ foo }}2' }}) expect(wrapper5.find('main').html()).to.equal('
1bar2
') wrapper5.trigger('keydown') + expect(wrapper5.find('main').html()).to.equal('
1BAR2
') const wrapper6 = mountingMethod(ComponentWithSlots, { slots: { default: '

1

2

' }}) expect(wrapper6.find('main').html()).to.equal('

1

2

') const wrapper7 = mountingMethod(ComponentWithSlots, { slots: { default: '1

2

3' }}) From 9e9a863f9ab302ba748ef5bf653ce8f8189921d8 Mon Sep 17 00:00:00 2001 From: 38elements Date: Thu, 31 May 2018 00:33:17 +0900 Subject: [PATCH 3/4] add test for child component --- test/specs/mounting-options/sync.spec.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/specs/mounting-options/sync.spec.js b/test/specs/mounting-options/sync.spec.js index 7426d1ce4..8881fec48 100644 --- a/test/specs/mounting-options/sync.spec.js +++ b/test/specs/mounting-options/sync.spec.js @@ -113,9 +113,17 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => { }) it('call updated when sync is not false', () => { + const fooSpy = sinon.stub() + const Foo = { + template: '
{{ foo }}
', + props: ['foo'], + updated () { + fooSpy() + } + } const spy = sinon.stub() const TestComponent = { - template: '
{{ foo }}
', + template: '
{{ foo }}
', data () { return { foo: 'foo' @@ -126,10 +134,14 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => { } } const wrapper = mountingMethod(TestComponent, { + stubs: { foo: Foo }, sync: true }) expect(spy.notCalled).to.equal(true) + expect(fooSpy.notCalled).to.equal(true) wrapper.vm.foo = 'bar' expect(spy.calledOnce).to.equal(true) + expect(fooSpy.calledOnce).to.equal(true) + expect(wrapper.html()).to.equal('
bar
bar
') }) }) From 495287405456f749c3e13629a988ba4ec29995ab Mon Sep 17 00:00:00 2001 From: 38elements Date: Thu, 31 May 2018 22:29:53 +0900 Subject: [PATCH 4/4] remove code for #455 --- .../test-utils/src/set-watchers-to-sync.js | 15 -------- test/specs/mounting-options/sync.spec.js | 34 ------------------- 2 files changed, 49 deletions(-) diff --git a/packages/test-utils/src/set-watchers-to-sync.js b/packages/test-utils/src/set-watchers-to-sync.js index b6b2a0ee0..93fd85d7a 100644 --- a/packages/test-utils/src/set-watchers-to-sync.js +++ b/packages/test-utils/src/set-watchers-to-sync.js @@ -1,5 +1,3 @@ -import { VUE_VERSION } from './consts' - function setDepsSync (dep) { dep.subs.forEach(setWatcherSync) } @@ -26,17 +24,4 @@ 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 8881fec48..7651f28cb 100644 --- a/test/specs/mounting-options/sync.spec.js +++ b/test/specs/mounting-options/sync.spec.js @@ -1,4 +1,3 @@ -import sinon from 'sinon' import { describeWithShallowAndMount } from '~resources/utils' describeWithShallowAndMount('options.sync', (mountingMethod) => { @@ -111,37 +110,4 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => { done() }) }) - - it('call updated when sync is not false', () => { - const fooSpy = sinon.stub() - const Foo = { - template: '
{{ foo }}
', - props: ['foo'], - updated () { - fooSpy() - } - } - const spy = sinon.stub() - const TestComponent = { - template: '
{{ foo }}
', - data () { - return { - foo: 'foo' - } - }, - updated () { - spy() - } - } - const wrapper = mountingMethod(TestComponent, { - stubs: { foo: Foo }, - sync: true - }) - expect(spy.notCalled).to.equal(true) - expect(fooSpy.notCalled).to.equal(true) - wrapper.vm.foo = 'bar' - expect(spy.calledOnce).to.equal(true) - expect(fooSpy.calledOnce).to.equal(true) - expect(wrapper.html()).to.equal('
bar
bar
') - }) })