Skip to content

Commit d8485f5

Browse files
38elementseddyerburgh
authored andcommitted
fix: function names (#580)
* Revert "refactor: refactor stub-components.js (#544)" This reverts commit db5e07e. * Revert "refactor: refactor add-slots.js (#556)" This reverts commit ce9e1bf. * delete packages/test-utils/src/shallow.js * add functions * add type SlotValue * add test * fix slotValue * fix validateEnvironment * add test * fix lint * Update slots.spec.js
1 parent 1a4d12f commit d8485f5

File tree

10 files changed

+227
-173
lines changed

10 files changed

+227
-173
lines changed

flow/vue.flow.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
declare type Component = Object // eslint-disable-line no-undef
66
declare type VNode = Object // eslint-disable-line no-undef
7+
declare type SlotValue = Component | string | Array<Component> | Array<string>

packages/create-instance/add-slots.js

+48-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
// @flow
22

33
import { compileToFunctions } from 'vue-template-compiler'
4+
import { throwError } from 'shared/util'
45
import { validateSlots } from './validate-slots'
5-
import { toArray } from 'shared/util'
66

7-
function isSingleHTMLTag (template: string) {
8-
if (!template.startsWith('<') || !template.endsWith('>')) {
7+
function isSingleElement (slotValue: string): boolean {
8+
const _slotValue = slotValue.trim()
9+
if (_slotValue[0] !== '<' || _slotValue[_slotValue.length - 1] !== '>') {
910
return false
1011
}
11-
const _document = new window.DOMParser().parseFromString(template, 'text/html')
12+
const domParser = new window.DOMParser()
13+
const _document = domParser.parseFromString(slotValue, 'text/html')
1214
return _document.body.childElementCount === 1
1315
}
1416

15-
function createElementFromAdvancedString (slotValue, vm) {
17+
// see https://github.com/vuejs/vue-test-utils/pull/274
18+
function createVNodes (vm: Component, slotValue: string) {
1619
const compiledResult = compileToFunctions(`<div>${slotValue}{{ }}</div>`)
1720
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns
1821
vm._renderProxy.$options.staticRenderFns = compiledResult.staticRenderFns
@@ -21,23 +24,54 @@ function createElementFromAdvancedString (slotValue, vm) {
2124
return elem
2225
}
2326

24-
function createElement (slotValue: string | Object, vm) {
27+
function validateEnvironment (): void {
28+
if (!compileToFunctions) {
29+
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
30+
}
31+
if (typeof window === 'undefined') {
32+
throwError('the slots string option does not support strings in server-test-uitls.')
33+
}
34+
if (window.navigator.userAgent.match(/PhantomJS/i)) {
35+
throwError('the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.')
36+
}
37+
}
38+
39+
function addSlotToVm (vm: Component, slotName: string, slotValue: SlotValue): void {
40+
let elem
2541
if (typeof slotValue === 'string') {
26-
slotValue = slotValue.trim()
27-
if (isSingleHTMLTag(slotValue)) {
28-
return vm.$createElement(compileToFunctions(slotValue))
42+
validateEnvironment()
43+
if (isSingleElement(slotValue)) {
44+
elem = vm.$createElement(compileToFunctions(slotValue))
2945
} else {
30-
return createElementFromAdvancedString(slotValue, vm)
46+
elem = createVNodes(vm, slotValue)
3147
}
3248
} else {
33-
return vm.$createElement(slotValue)
49+
elem = vm.$createElement(slotValue)
50+
}
51+
if (Array.isArray(elem)) {
52+
if (Array.isArray(vm.$slots[slotName])) {
53+
vm.$slots[slotName] = [...vm.$slots[slotName], ...elem]
54+
} else {
55+
vm.$slots[slotName] = [...elem]
56+
}
57+
} else {
58+
if (Array.isArray(vm.$slots[slotName])) {
59+
vm.$slots[slotName].push(elem)
60+
} else {
61+
vm.$slots[slotName] = [elem]
62+
}
3463
}
3564
}
3665

3766
export function addSlots (vm: Component, slots: Object): void {
3867
validateSlots(slots)
39-
Object.keys(slots).forEach(name => {
40-
vm.$slots[name] = toArray(slots[name])
41-
.map(slotValue => createElement(slotValue, vm))
68+
Object.keys(slots).forEach((key) => {
69+
if (Array.isArray(slots[key])) {
70+
slots[key].forEach((slotValue) => {
71+
addSlotToVm(vm, key, slotValue)
72+
})
73+
} else {
74+
addSlotToVm(vm, key, slots[key])
75+
}
4276
})
4377
}
+15-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
// @flow
22

3-
import { throwError, toArray, isObject } from 'shared/util'
4-
import { compileToFunctions } from 'vue-template-compiler'
3+
import { throwError } from 'shared/util'
4+
5+
function isValidSlot (slot: any): boolean {
6+
return Array.isArray(slot) || (slot !== null && typeof slot === 'object') || typeof slot === 'string'
7+
}
58

69
export function validateSlots (slots: Object): void {
7-
Object.keys(slots).forEach(key => {
8-
toArray(slots[key]).forEach(slotValue => {
9-
if (!isObject(slotValue) && typeof slotValue !== 'string') {
10-
throwError('slots[key] must be a Component, string or an array of Components')
11-
}
10+
slots && Object.keys(slots).forEach((key) => {
11+
if (!isValidSlot(slots[key])) {
12+
throwError('slots[key] must be a Component, string or an array of Components')
13+
}
1214

13-
if (typeof slotValue === 'string') {
14-
if (!compileToFunctions) {
15-
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
16-
}
17-
if (typeof window === 'undefined') {
18-
throwError('the slots string option does not support strings in server-test-uitls.')
19-
}
20-
if (window.navigator.userAgent.match(/PhantomJS/i)) {
21-
throwError('the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.')
15+
if (Array.isArray(slots[key])) {
16+
slots[key].forEach((slotValue) => {
17+
if (!isValidSlot(slotValue)) {
18+
throwError('slots[key] must be a Component, string or an array of Components')
2219
}
23-
}
24-
})
20+
})
21+
}
2522
})
2623
}

packages/shared/compile-template.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import { compileToFunctions } from 'vue-template-compiler'
44

55
export function compileTemplate (component: Component) {
6-
Object.keys(component.components || {}).forEach((c) => {
7-
const cmp = component.components[c]
8-
if (!cmp.render) {
9-
compileTemplate(cmp)
10-
}
11-
})
12-
6+
if (component.components) {
7+
Object.keys(component.components).forEach((c) => {
8+
const cmp = component.components[c]
9+
if (!cmp.render) {
10+
compileTemplate(cmp)
11+
}
12+
})
13+
}
1314
if (component.extends) {
1415
compileTemplate(component.extends)
1516
}

packages/shared/stub-components-validate.js

-44
This file was deleted.

0 commit comments

Comments
 (0)