forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstub-components.js
178 lines (158 loc) · 5.04 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
// @flow
import Vue from 'vue'
import { compileToFunctions } from 'vue-template-compiler'
import { throwError } from './util'
import { componentNeedsCompiling } 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): Object {
if (!compileToFunctions) {
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
}
return {
...getCoreProperties(originalComponent),
...compileToFunctions(templateString)
}
}
function createBlankStub (originalComponent: Component) {
return {
...getCoreProperties(originalComponent),
render: h => h('')
}
}
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({})
})
} 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({})
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])
} else {
components[stub] = {
...stubs[stub],
name: originalComponents[stub].name
}
}
} else {
if (typeof stubs[stub] === 'string') {
if (!compileToFunctions) {
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined')
}
components[stub] = {
...compileToFunctions(stubs[stub])
}
} else {
components[stub] = {
...stubs[stub]
}
}
}
// ignoreElements does not exist in Vue 2.0.x
if (Vue.config.ignoredElements) {
Vue.config.ignoredElements.push(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])
// ignoreElements does not exist in Vue 2.0.x
if (Vue.config.ignoredElements) {
Vue.config.ignoredElements.push(component)
}
})
}
export function createComponentStubsForAll (component: Component): Object {
const stubbedComponents = {}
if (component.components) {
stubComponents(component.components, stubbedComponents)
}
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 // eslint-disable-line no-param-reassign
delete components[c]._Ctor // eslint-disable-line no-param-reassign
})
return components
}