Skip to content

Commit

Permalink
Fix update()
Browse files Browse the repository at this point in the history
  • Loading branch information
38elements committed Dec 18, 2017
1 parent 66ecd96 commit 3b99090
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/lib/add-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ function addSlotToVm (vm: Component, slotName: string, slotValue: Component | st
} else {
elem = vm.$createElement(slotValue)
}
if (Array.isArray(vm.$slots[slotName])) {
vm.$slots[slotName].push(elem)
if (Array.isArray(elem)) {
if (Array.isArray(vm.$slots[slotName])) {
vm.$slots[slotName] = [...vm.$slots[slotName], ...elem]
} else {
vm.$slots[slotName] = [...elem]
}
} else {
vm.$slots[slotName] = [elem]
if (Array.isArray(vm.$slots[slotName])) {
vm.$slots[slotName].push(elem)
} else {
vm.$slots[slotName] = [elem]
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import createLocalVue from '../create-local-vue'
import extractOptions from '../options/extract-options'
import deleteMountingOptions from '../options/delete-mounting-options'
import createFunctionalComponent from './create-functional-component'
import cloneDeep from 'lodash/cloneDeep'

export default function createConstructor (
component: Component,
Expand Down Expand Up @@ -58,6 +59,9 @@ export default function createConstructor (
addAttrs(vm, mountingOptions.attrs)
addListeners(vm, mountingOptions.listeners)

vm.$_mountingOptionsSlots = mountingOptions.slots
vm.$_originalSlots = cloneDeep(vm.$slots)

if (mountingOptions.slots) {
addSlots(vm, mountingOptions.slots)
}
Expand Down
12 changes: 11 additions & 1 deletion src/wrappers/vue-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
// @flow

import Wrapper from './wrapper'
import addSlots from '../lib/add-slots'
import cloneDeep from 'lodash/cloneDeep'

function update () {
this._update(this._render())
// the only component made by mount()
if (this.$_originalSlots) {
this.$slots = cloneDeep(this.$_originalSlots)
}
if (this.$_mountingOptionsSlots) {
addSlots(this, this.$_mountingOptionsSlots)
}
const vnodes = this._render()
this._update(vnodes)
this.$children.forEach(child => update.call(child))
}

Expand Down
7 changes: 6 additions & 1 deletion test/resources/components/component-with-slots.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="container">
<div class="container" @keydown="change">
<header>
<slot name="header"></slot>
</header>
Expand All @@ -19,6 +19,11 @@
return {
'foo': 'bar'
}
},
methods: {
change () {
this.foo = 'BAR'
}
}
}
</script>
2 changes: 2 additions & 0 deletions test/unit/specs/mount/options/slots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ describe('mount.slots', () => {
expect(wrapper4.find('main').html()).to.equal('<main>123</main>')
const wrapper5 = mount(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>')
})

it('throws error if passed string in default slot object and vue-template-compiler is undefined', () => {
Expand Down

0 comments on commit 3b99090

Please sign in to comment.