Skip to content

Commit d9e368b

Browse files
committed
fix: handle named functional slots
1 parent ff97e28 commit d9e368b

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/lib/create-instance.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ function createFunctionalSlots (slots = {}, h) {
2222
if (typeof slots.default === 'string') {
2323
return [h(compileToFunctions(slots.default))]
2424
}
25+
const children = []
26+
Object.keys(slots).forEach(slotType => {
27+
if (Array.isArray(slots[slotType])) {
28+
slots[slotType].forEach(slot => {
29+
const component = typeof slot === 'string' ? compileToFunctions(slot) : slot
30+
const newSlot = h(component)
31+
newSlot.data.slot = slotType
32+
children.push(newSlot)
33+
})
34+
} else {
35+
const component = typeof slots[slotType] === 'string' ? compileToFunctions(slots[slotType]) : slots[slotType]
36+
const slot = h(component)
37+
slot.data.slot = slotType
38+
children.push(slot)
39+
}
40+
})
41+
return children
2542
}
2643

2744
export default function createConstructor (

test/unit/specs/mount/options/slots.spec.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,68 @@ describe('mount.slots', () => {
8383
expect(Array.isArray(wrapper.vm.$slots.header)).to.equal(true)
8484
})
8585

86-
it.only('mounts functional component with default slot if passed component in slot object', () => {
87-
const wrapper = mount(FunctionalComponentWithSlots, { slots: { default: [Component] }})
86+
it('mounts functional component with default slot if passed component in slot object', () => {
87+
const wrapper = mount(FunctionalComponentWithSlots, { slots: { default: Component }})
8888
expect(wrapper.contains(Component)).to.equal(true)
8989
})
9090

91-
it.only('mounts component with default slot if passed component in slot object', () => {
91+
it('mounts component with default slot if passed component in slot object', () => {
9292
const wrapper = mount(FunctionalComponentWithSlots, { slots: { default: [Component] }})
9393
expect(wrapper.contains(Component)).to.equal(true)
9494
})
9595

96-
it.only('mounts component with default slot if passed object with template prop in slot object', () => {
96+
it('mounts component with default slot if passed object with template prop in slot object', () => {
9797
const compiled = compileToFunctions('<div id="div" />')
9898
const wrapper = mount(FunctionalComponentWithSlots, { slots: { default: [compiled] }})
9999
expect(wrapper.contains('#div')).to.equal(true)
100100
})
101101

102-
it.only('mounts component with default slot if passed string in slot object', () => {
102+
it('mounts component with default slot if passed string in slot object', () => {
103103
const wrapper = mount(FunctionalComponentWithSlots, { slots: { default: '<span />' }})
104104
expect(wrapper.contains('span')).to.equal(true)
105105
})
106+
107+
it('mounts component with named slot if passed string in slot object', () => {
108+
const TestComponent = {
109+
functional: true,
110+
render (h, ctx) {
111+
return h('div', {}, ctx.slots().named)
112+
}
113+
}
114+
const wrapper = mount(TestComponent, { slots: { named: Component }})
115+
expect(wrapper.contains(Component)).to.equal(true)
116+
})
117+
118+
it('mounts component with named slot if passed string in slot object in array', () => {
119+
const TestComponent = {
120+
functional: true,
121+
render (h, ctx) {
122+
return h('div', {}, ctx.slots().named)
123+
}
124+
}
125+
const wrapper = mount(TestComponent, { slots: { named: [Component] }})
126+
expect(wrapper.contains(Component)).to.equal(true)
127+
})
128+
129+
it('mounts component with named slot if passed string in slot object in array', () => {
130+
const TestComponent = {
131+
functional: true,
132+
render (h, ctx) {
133+
return h('div', {}, ctx.slots().named)
134+
}
135+
}
136+
const wrapper = mount(TestComponent, { slots: { named: '<span />' }})
137+
expect(wrapper.contains('span')).to.equal(true)
138+
})
139+
140+
it('mounts component with named slot if passed string in slot object in array', () => {
141+
const TestComponent = {
142+
functional: true,
143+
render (h, ctx) {
144+
return h('div', {}, ctx.slots().named)
145+
}
146+
}
147+
const wrapper = mount(TestComponent, { slots: { named: ['<span />'] }})
148+
expect(wrapper.contains('span')).to.equal(true)
149+
})
106150
})

0 commit comments

Comments
 (0)