Skip to content

Commit 3b99090

Browse files
committed
Fix update()
1 parent 66ecd96 commit 3b99090

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

Diff for: src/lib/add-slots.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,18 @@ function addSlotToVm (vm: Component, slotName: string, slotValue: Component | st
3131
} else {
3232
elem = vm.$createElement(slotValue)
3333
}
34-
if (Array.isArray(vm.$slots[slotName])) {
35-
vm.$slots[slotName].push(elem)
34+
if (Array.isArray(elem)) {
35+
if (Array.isArray(vm.$slots[slotName])) {
36+
vm.$slots[slotName] = [...vm.$slots[slotName], ...elem]
37+
} else {
38+
vm.$slots[slotName] = [...elem]
39+
}
3640
} else {
37-
vm.$slots[slotName] = [elem]
41+
if (Array.isArray(vm.$slots[slotName])) {
42+
vm.$slots[slotName].push(elem)
43+
} else {
44+
vm.$slots[slotName] = [elem]
45+
}
3846
}
3947
}
4048

Diff for: src/lib/create-instance.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import createLocalVue from '../create-local-vue'
1313
import extractOptions from '../options/extract-options'
1414
import deleteMountingOptions from '../options/delete-mounting-options'
1515
import createFunctionalComponent from './create-functional-component'
16+
import cloneDeep from 'lodash/cloneDeep'
1617

1718
export default function createConstructor (
1819
component: Component,
@@ -58,6 +59,9 @@ export default function createConstructor (
5859
addAttrs(vm, mountingOptions.attrs)
5960
addListeners(vm, mountingOptions.listeners)
6061

62+
vm.$_mountingOptionsSlots = mountingOptions.slots
63+
vm.$_originalSlots = cloneDeep(vm.$slots)
64+
6165
if (mountingOptions.slots) {
6266
addSlots(vm, mountingOptions.slots)
6367
}

Diff for: src/wrappers/vue-wrapper.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
// @flow
22

33
import Wrapper from './wrapper'
4+
import addSlots from '../lib/add-slots'
5+
import cloneDeep from 'lodash/cloneDeep'
46

57
function update () {
6-
this._update(this._render())
8+
// the only component made by mount()
9+
if (this.$_originalSlots) {
10+
this.$slots = cloneDeep(this.$_originalSlots)
11+
}
12+
if (this.$_mountingOptionsSlots) {
13+
addSlots(this, this.$_mountingOptionsSlots)
14+
}
15+
const vnodes = this._render()
16+
this._update(vnodes)
717
this.$children.forEach(child => update.call(child))
818
}
919

Diff for: test/resources/components/component-with-slots.vue

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="container">
2+
<div class="container" @keydown="change">
33
<header>
44
<slot name="header"></slot>
55
</header>
@@ -19,6 +19,11 @@
1919
return {
2020
'foo': 'bar'
2121
}
22+
},
23+
methods: {
24+
change () {
25+
this.foo = 'BAR'
26+
}
2227
}
2328
}
2429
</script>

Diff for: test/unit/specs/mount/options/slots.spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ describe('mount.slots', () => {
5858
expect(wrapper4.find('main').html()).to.equal('<main>123</main>')
5959
const wrapper5 = mount(ComponentWithSlots, { slots: { default: '1{{ foo }}2' }})
6060
expect(wrapper5.find('main').html()).to.equal('<main>1bar2</main>')
61+
wrapper5.trigger('keydown')
62+
expect(wrapper5.find('main').html()).to.equal('<main>1BAR2</main>')
6163
})
6264

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

0 commit comments

Comments
 (0)