forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstub-components.js
214 lines (190 loc) · 5.44 KB
/
stub-components.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// @flow
import Vue from 'vue'
import { compileToFunctions } from 'vue-template-compiler'
import { throwError } from './util'
import {
componentNeedsCompiling,
templateContainsComponent
} from './validators'
import { compileTemplate } from './compile-template'
function isVueComponent (comp) {
return comp && (comp.render || comp.template || comp.options)
}
function isValidStub (stub: any) {
return (
(!!stub && typeof stub === 'string') ||
stub === true ||
isVueComponent(stub)
)
}
function isRequiredComponent (name) {
return (
name === 'KeepAlive' || name === 'Transition' || name === 'TransitionGroup'
)
}
function getCoreProperties (component: Component): Object {
return {
attrs: component.attrs,
name: component.name,
on: component.on,
key: component.key,
ref: component.ref,
props: component.props,
domProps: component.domProps,
class: component.class,
staticClass: component.staticClass,
staticStyle: component.staticStyle,
style: component.style,
normalizedStyle: component.normalizedStyle,
nativeOn: component.nativeOn,
functional: component.functional
}
}
function createStubFromString (
templateString: string,
originalComponent: Component,
name: string
): Object {
if (!compileToFunctions) {
throwError(
`vueTemplateCompiler is undefined, you must pass ` +
`precompiled components if vue-template-compiler is ` +
`undefined`
)
}
if (templateContainsComponent(templateString, name)) {
throwError('options.stub cannot contain a circular reference')
}
return {
...getCoreProperties(originalComponent),
...compileToFunctions(templateString)
}
}
function createBlankStub (originalComponent: Component) {
const name = `${originalComponent.name}-stub`
// ignoreElements does not exist in Vue 2.0.x
if (Vue.config.ignoredElements) {
Vue.config.ignoredElements.push(name)
}
return {
...getCoreProperties(originalComponent),
render (h) {
return h(`${originalComponent.name}-stub`,
!originalComponent.functional && this.$slots.default
)
}
}
}
export function createComponentStubs (
originalComponents: Object = {},
stubs: Object
): Object {
const components = {}
if (!stubs) {
return components
}
if (Array.isArray(stubs)) {
stubs.forEach(stub => {
if (stub === false) {
return
}
if (typeof stub !== 'string') {
throwError(`each item in an options.stubs array must be a ` + `string`)
}
components[stub] = createBlankStub({ name: stub })
})
} else {
Object.keys(stubs).forEach(stub => {
if (stubs[stub] === false) {
return
}
if (!isValidStub(stubs[stub])) {
throwError(
`options.stub values must be passed a string or ` + `component`
)
}
if (stubs[stub] === true) {
components[stub] = createBlankStub({ name: stub })
return
}
if (componentNeedsCompiling(stubs[stub])) {
compileTemplate(stubs[stub])
}
if (originalComponents[stub]) {
// Remove cached constructor
delete originalComponents[stub]._Ctor
if (typeof stubs[stub] === 'string') {
components[stub] = createStubFromString(
stubs[stub],
originalComponents[stub],
stub
)
} else {
components[stub] = {
...stubs[stub],
name: originalComponents[stub].name
}
}
} else {
if (typeof stubs[stub] === 'string') {
if (!compileToFunctions) {
throwError(
`vueTemplateCompiler is undefined, you must pass ` +
`precompiled components if vue-template-compiler is ` +
`undefined`
)
}
components[stub] = {
...compileToFunctions(stubs[stub])
}
} else {
components[stub] = {
...stubs[stub]
}
}
}
})
}
return components
}
function stubComponents (components: Object, stubbedComponents: Object) {
Object.keys(components).forEach(component => {
// Remove cached constructor
delete components[component]._Ctor
if (!components[component].name) {
components[component].name = component
}
stubbedComponents[component] = createBlankStub(components[component])
})
}
export function createComponentStubsForAll (component: Component): Object {
const stubbedComponents = {}
if (component.components) {
stubComponents(component.components, stubbedComponents)
}
stubbedComponents[component.name] = createBlankStub(component)
let extended = component.extends
// Loop through extended component chains to stub all child components
while (extended) {
if (extended.components) {
stubComponents(extended.components, stubbedComponents)
}
extended = extended.extends
}
if (component.extendOptions && component.extendOptions.components) {
stubComponents(component.extendOptions.components, stubbedComponents)
}
return stubbedComponents
}
export function createComponentStubsForGlobals (instance: Component): Object {
const components = {}
Object.keys(instance.options.components).forEach(c => {
if (isRequiredComponent(c)) {
return
}
components[c] = createBlankStub(instance.options.components[c])
delete instance.options.components[c]._Ctor
delete components[c]._Ctor
})
return components
}