1
1
// @flow
2
2
3
3
import Vue from 'vue'
4
- import { compileToFunctions } from 'vue-template-compiler'
5
4
import {
6
5
throwError ,
7
6
camelize ,
@@ -11,30 +10,21 @@ import {
11
10
import {
12
11
componentNeedsCompiling ,
13
12
templateContainsComponent ,
14
- isVueComponent ,
15
- isRequiredComponent
13
+ isVueComponent
16
14
} from './validators'
17
- import { compileTemplate } from './compile-template'
18
-
19
- function compileFromString ( str ) {
20
- if ( ! compileToFunctions ) {
21
- throwError (
22
- `vueTemplateCompiler is undefined, you must pass ` +
23
- `precompiled components if vue-template-compiler is ` +
24
- `undefined`
25
- )
26
- }
27
- return compileToFunctions ( str )
28
- }
15
+ import {
16
+ compileTemplate ,
17
+ compileFromString
18
+ } from './compile-template'
29
19
30
20
function isVueComponentStub ( comp ) : boolean {
31
21
return comp && comp . template || isVueComponent ( comp )
32
22
}
33
23
34
24
function isValidStub ( stub : any ) : boolean {
35
25
return (
26
+ typeof stub === 'boolean' ||
36
27
( ! ! stub && typeof stub === 'string' ) ||
37
- stub === true ||
38
28
isVueComponentStub ( stub )
39
29
)
40
30
}
@@ -67,33 +57,14 @@ function getCoreProperties (componentOptions: Component): Object {
67
57
}
68
58
}
69
59
70
- function createStubFromString (
71
- templateString : string ,
72
- originalComponent : Component ,
73
- name : string
74
- ) : Component {
75
- if ( templateContainsComponent ( templateString , name ) ) {
76
- throwError ( 'options.stub cannot contain a circular reference' )
77
- }
78
-
79
- const componentOptions = typeof originalComponent === 'function'
80
- ? originalComponent . extendOptions
81
- : originalComponent
82
-
83
- return {
84
- ...getCoreProperties ( componentOptions ) ,
85
- ...compileFromString ( templateString )
86
- }
87
- }
88
-
89
60
function createClassString ( staticClass , dynamicClass ) {
90
61
if ( staticClass && dynamicClass ) {
91
62
return staticClass + ' ' + dynamicClass
92
63
}
93
64
return staticClass || dynamicClass
94
65
}
95
66
96
- export function createBlankStub (
67
+ export function createStubFromComponent (
97
68
originalComponent : Component ,
98
69
name : string
99
70
) : Component {
@@ -109,6 +80,7 @@ export function createBlankStub (
109
80
110
81
return {
111
82
...getCoreProperties ( componentOptions ) ,
83
+ $_vueTestUtils_original : originalComponent ,
112
84
render ( h , context ) {
113
85
return h (
114
86
tagName ,
@@ -130,101 +102,123 @@ export function createBlankStub (
130
102
}
131
103
}
132
104
133
- export function createComponentStubs (
105
+ function createStubFromString (
106
+ templateString : string ,
107
+ originalComponent : Component = { } ,
108
+ name : string
109
+ ) : Component {
110
+ if ( templateContainsComponent ( templateString , name ) ) {
111
+ throwError ( 'options.stub cannot contain a circular reference' )
112
+ }
113
+
114
+ const componentOptions = typeof originalComponent === 'function'
115
+ ? originalComponent . extendOptions
116
+ : originalComponent
117
+
118
+ return {
119
+ ...getCoreProperties ( componentOptions ) ,
120
+ ...compileFromString ( templateString )
121
+ }
122
+ }
123
+
124
+ function validateStub ( stub ) {
125
+ if ( ! isValidStub ( stub ) ) {
126
+ throwError (
127
+ `options.stub values must be passed a string or ` +
128
+ `component`
129
+ )
130
+ }
131
+ }
132
+
133
+ export function createStubsFromStubsObject (
134
134
originalComponents : Object = { } ,
135
135
stubs : Object
136
136
) : Components {
137
- const components = { }
138
- if ( ! stubs ) {
139
- return components
140
- }
141
- Object . keys ( stubs ) . forEach ( stubName => {
137
+ return Object . keys ( stubs || { } ) . reduce ( ( acc , stubName ) => {
142
138
const stub = stubs [ stubName ]
143
- if ( stub === false ) {
144
- return
145
- }
146
139
147
- if ( ! isValidStub ( stub ) ) {
148
- throwError (
149
- `options. stub values must be passed a string or ` + `component`
150
- )
140
+ validateStub ( stub )
141
+
142
+ if ( stub === false ) {
143
+ return acc
151
144
}
152
145
153
146
if ( stub === true ) {
154
147
const component = resolveComponent ( originalComponents , stubName )
155
- components [ stubName ] = createBlankStub ( component , stubName )
156
- return
157
- }
158
-
159
- if ( typeof stub !== 'string' && componentNeedsCompiling ( stub ) ) {
160
- compileTemplate ( stub )
148
+ acc [ stubName ] = createStubFromComponent ( component , stubName )
149
+ return acc
161
150
}
162
151
163
152
if ( originalComponents [ stubName ] ) {
164
153
// Remove cached constructor
165
154
delete originalComponents [ stubName ] . _Ctor
166
- if ( typeof stub === 'string' ) {
167
- components [ stubName ] = createStubFromString (
168
- stub ,
169
- originalComponents [ stubName ] ,
170
- stubName
171
- )
172
- } else {
173
- const stubObject = ( stub : Object )
174
- components [ stubName ] = {
175
- ...stubObject ,
176
- name : originalComponents [ stubName ] . name
177
- }
178
- }
179
- } else {
180
- if ( typeof stub === 'string' ) {
181
- components [ stubName ] = {
182
- ...compileFromString ( stub )
183
- }
184
- } else {
185
- const stubObject = ( stub : Object )
186
- components [ stubName ] = {
187
- ...stubObject
188
- }
189
- }
190
155
}
191
- } )
192
- return components
156
+
157
+ if ( typeof stub === 'string' ) {
158
+ acc [ stubName ] = createStubFromString (
159
+ stub ,
160
+ originalComponents [ stubName ] ,
161
+ stubName
162
+ )
163
+ return acc
164
+ }
165
+
166
+ if ( componentNeedsCompiling ( stub ) ) {
167
+ compileTemplate ( stub )
168
+ }
169
+ const name = originalComponents [ stubName ] &&
170
+ originalComponents [ stubName ] . name
171
+
172
+ acc [ stubName ] = {
173
+ name,
174
+ ...stub
175
+ }
176
+
177
+ return acc
178
+ } , { } )
193
179
}
194
180
195
181
function stubComponents (
196
182
components : Components ,
197
183
stubbedComponents : Components
198
184
) : void {
199
- Object . keys ( components ) . forEach ( component => {
185
+ for ( const component in components ) {
200
186
const cmp = components [ component ]
201
187
const componentOptions = typeof cmp === 'function'
202
188
? cmp . extendOptions
203
189
: cmp
204
190
205
191
if ( ! componentOptions ) {
206
- stubbedComponents [ component ] = createBlankStub ( { } , component )
192
+ stubbedComponents [ component ] = createStubFromComponent (
193
+ { } ,
194
+ component
195
+ )
207
196
return
208
197
}
209
198
// Remove cached constructor
210
199
delete componentOptions . _Ctor
211
- if ( ! componentOptions . name ) {
212
- componentOptions . name = component
213
- }
214
- stubbedComponents [ component ] = createBlankStub ( componentOptions , component )
215
- } )
200
+
201
+ stubbedComponents [ component ] = createStubFromComponent (
202
+ cmp ,
203
+ component
204
+ )
205
+ }
216
206
}
217
207
218
- export function createComponentStubsForAll ( component : Component ) : Components {
208
+ export function createStubsForComponent (
209
+ component : Component
210
+ ) : Components {
219
211
const stubbedComponents = { }
220
212
213
+ if ( component . options ) {
214
+ stubComponents ( component . options . components , stubbedComponents )
215
+ }
216
+
221
217
if ( component . components ) {
222
218
stubComponents ( component . components , stubbedComponents )
223
219
}
224
220
225
221
let extended = component . extends
226
-
227
- // Loop through extended component chains to stub all child components
228
222
while ( extended ) {
229
223
if ( extended . components ) {
230
224
stubComponents ( extended . components , stubbedComponents )
@@ -242,18 +236,3 @@ export function createComponentStubsForAll (component: Component): Components {
242
236
243
237
return stubbedComponents
244
238
}
245
-
246
- export function createComponentStubsForGlobals (
247
- instance : Component
248
- ) : Components {
249
- const components = { }
250
- for ( const c in instance . options . components ) {
251
- if ( isRequiredComponent ( c ) ) {
252
- continue
253
- }
254
- components [ c ] = createBlankStub ( instance . options . components [ c ] , c )
255
- delete instance . options . components [ c ] . _Ctor
256
- delete components [ c ] . _Ctor
257
- }
258
- return components
259
- }
0 commit comments