From da83a3d806b7777ffae8c9208a64098240845eb9 Mon Sep 17 00:00:00 2001 From: nekosaur Date: Sat, 14 Jul 2018 11:49:56 +0200 Subject: [PATCH] feat: enabled slots option to take class components --- packages/create-instance/validate-slots.js | 32 ++++++++-------------- test/specs/mounting-options/slots.spec.js | 22 +++++++++++++++ 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/packages/create-instance/validate-slots.js b/packages/create-instance/validate-slots.js index f4487c8ea..97d70a99f 100644 --- a/packages/create-instance/validate-slots.js +++ b/packages/create-instance/validate-slots.js @@ -2,11 +2,11 @@ import { throwError } from 'shared/util' import { compileToFunctions } from 'vue-template-compiler' +import { isVueComponent } from '../shared/validators' function isValidSlot (slot: any): boolean { return ( - Array.isArray(slot) || - (slot !== null && typeof slot === 'object') || + isVueComponent(slot) || typeof slot === 'string' ) } @@ -23,24 +23,16 @@ function requiresTemplateCompiler (slot: any): void { export function validateSlots (slots: SlotsObject): void { Object.keys(slots).forEach(key => { - if (!isValidSlot(slots[key])) { - throwError( - `slots[key] must be a Component, string or an array ` + `of Components` - ) - } + const slot = Array.isArray(slots[key]) ? slots[key] : [slots[key]] - requiresTemplateCompiler(slots[key]) - - if (Array.isArray(slots[key])) { - slots[key].forEach(slotValue => { - if (!isValidSlot(slotValue)) { - throwError( - `slots[key] must be a Component, string or an array ` + - `of Components` - ) - } - requiresTemplateCompiler(slotValue) - }) - } + slot.forEach(slotValue => { + if (!isValidSlot(slotValue)) { + throwError( + `slots[key] must be a Component, string or an array ` + + `of Components` + ) + } + requiresTemplateCompiler(slotValue) + }) }) } diff --git a/test/specs/mounting-options/slots.spec.js b/test/specs/mounting-options/slots.spec.js index d0e769312..19e338b05 100644 --- a/test/specs/mounting-options/slots.spec.js +++ b/test/specs/mounting-options/slots.spec.js @@ -546,4 +546,26 @@ describeWithMountingMethods('options.slots', mountingMethod => { wrapper.find('div').trigger('click') } ) + + it('mounts component with default slot if passed class component in slot object', () => { + const wrapper = mountingMethod(ComponentWithSlots, { + slots: { default: ComponentAsAClass } + }) + if (mountingMethod.name === 'renderToString') { + expect(wrapper).contains('
') + } else { + expect(wrapper.contains(ComponentAsAClass)).to.equal(true) + } + }) + + it('mounts component with default slot if passed class component in array in slot object', () => { + const wrapper = mountingMethod(ComponentWithSlots, { + slots: { default: [ComponentAsAClass] } + }) + if (mountingMethod.name === 'renderToString') { + expect(wrapper).contains('
') + } else { + expect(wrapper.contains(ComponentAsAClass)).to.equal(true) + } + }) })