Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #455 #661

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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.')
Expand Down
15 changes: 15 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,17 @@ 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)
})
}
}
}
}
1 change: 1 addition & 0 deletions test/specs/mounting-options/slots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ describeWithMountingMethods('options.slots', (mountingMethod) => {
const wrapper5 = mountingMethod(ComponentWithSlots, { slots: { default: '1{{ foo }}2' }})
expect(wrapper5.find('main').html()).to.equal('<main>1bar2</main>')
wrapper5.trigger('keydown')
expect(wrapper5.find('main').html()).to.equal('<main>1BAR2</main>')
const wrapper6 = mountingMethod(ComponentWithSlots, { slots: { default: '<p>1</p><p>2</p>' }})
expect(wrapper6.find('main').html()).to.equal('<main><p>1</p><p>2</p></main>')
const wrapper7 = mountingMethod(ComponentWithSlots, { slots: { default: '1<p>2</p>3' }})
Expand Down
36 changes: 35 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,37 @@ describeWithShallowAndMount('options.sync', (mountingMethod) => {
done()
})
})

it('call updated when sync is not false', () => {
const fooSpy = sinon.stub()
const Foo = {
template: '<div>{{ foo }}</div>',
props: ['foo'],
updated () {
fooSpy()
}
}
const spy = sinon.stub()
const TestComponent = {
template: '<div>{{ foo }}<foo :foo="foo" /></div>',
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('<div>bar<div>bar</div></div>')
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyerburgh

One question with this PR—I don't think child components will call update with this solution?

Since this test passed, child components are updated.

})