Skip to content

Commit

Permalink
test: unskip slots tests (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh committed Apr 1, 2018
1 parent d588f3e commit 21fe83d
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 385 deletions.
13 changes: 2 additions & 11 deletions packages/create-instance/add-slots.js
Expand Up @@ -2,10 +2,7 @@

import { compileToFunctions } from 'vue-template-compiler'
import { throwError } from 'shared/util'

function isValidSlot (slot: any): boolean {
return true
}
import { validateSlots } from './validate-slots'

function addSlotToVm (vm: Component, slotName: string, slotValue: Component | string | Array<Component> | Array<string>): void {
let elem
Expand Down Expand Up @@ -47,16 +44,10 @@ function addSlotToVm (vm: Component, slotName: string, slotValue: Component | st
}

export function addSlots (vm: Component, slots: Object): void {
validateSlots(slots)
Object.keys(slots).forEach((key) => {
if (!isValidSlot(slots[key])) {
throwError('slots[key] must be a Component, string or an array of Components')
}

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')
}
addSlotToVm(vm, key, slotValue)
})
} else {
Expand Down
14 changes: 4 additions & 10 deletions packages/create-instance/create-functional-component.js
Expand Up @@ -2,10 +2,7 @@

import { compileToFunctions } from 'vue-template-compiler'
import { throwError } from 'shared/util'

function isValidSlot (slot: any): boolean {
return Array.isArray(slot) || (slot !== null && typeof slot === 'object') || typeof slot === 'string'
}
import { validateSlots } from './validate-slots'

function createFunctionalSlots (slots = {}, h) {
if (Array.isArray(slots.default)) {
Expand All @@ -19,18 +16,12 @@ function createFunctionalSlots (slots = {}, h) {
Object.keys(slots).forEach(slotType => {
if (Array.isArray(slots[slotType])) {
slots[slotType].forEach(slot => {
if (!isValidSlot(slot)) {
throwError('slots[key] must be a Component, string or an array of Components')
}
const component = typeof slot === 'string' ? compileToFunctions(slot) : slot
const newSlot = h(component)
newSlot.data.slot = slotType
children.push(newSlot)
})
} else {
if (!isValidSlot(slots[slotType])) {
throwError('slots[key] must be a Component, string or an array of Components')
}
const component = typeof slots[slotType] === 'string' ? compileToFunctions(slots[slotType]) : slots[slotType]
const slot = h(component)
slot.data.slot = slotType
Expand All @@ -44,6 +35,9 @@ export default function createFunctionalComponent (component: Component, mountin
if (mountingOptions.context && typeof mountingOptions.context !== 'object') {
throwError('mount.context must be an object')
}
if (mountingOptions.slots) {
validateSlots(mountingOptions.slots)
}

return {
render (h: Function) {
Expand Down
23 changes: 23 additions & 0 deletions packages/create-instance/validate-slots.js
@@ -0,0 +1,23 @@
// @flow

import { throwError } from 'shared/util'

function isValidSlot (slot: any): boolean {
return Array.isArray(slot) || (slot !== null && typeof slot === 'object') || typeof slot === 'string'
}

export function validateSlots (slots: Object): void {
slots && Object.keys(slots).forEach((key) => {
if (!isValidSlot(slots[key])) {
throwError('slots[key] must be a Component, string or an array of Components')
}

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')
}
})
}
})
}

0 comments on commit 21fe83d

Please sign in to comment.