@@ -20,19 +20,24 @@ import findAll from '../lib/find'
20
20
import createWrapper from './create-wrapper'
21
21
22
22
export default class Wrapper implements BaseWrapper {
23
- vnode : VNode ;
23
+ vnode : VNode | null ;
24
24
vm: Component | null ;
25
25
_emitted: { [ name : string ] : Array < Array < any >> } ;
26
26
_emittedByOrder: Array < { name : string ; args: Array < any > } > ;
27
27
isVueComponent: boolean ;
28
- element: HTMLElement ;
28
+ element: Element ;
29
29
update: Function ;
30
30
options: WrapperOptions ;
31
31
version: number
32
32
33
- constructor ( vnode : VNode , update : Function , options : WrapperOptions ) {
34
- this . vnode = vnode
35
- this . element = vnode . elm
33
+ constructor ( node : VNode | Element , update : Function , options : WrapperOptions ) {
34
+ if ( node instanceof Element ) {
35
+ this . element = node
36
+ this . vnode = null
37
+ } else {
38
+ this . vnode = node
39
+ this . element = node . elm
40
+ }
36
41
this . update = update
37
42
this . options = options
38
43
this . version = Number ( `${ Vue . version . split ( '.' ) [ 0 ] } .${ Vue . version . split ( '.' ) [ 1 ] } ` )
@@ -82,7 +87,7 @@ export default class Wrapper implements BaseWrapper {
82
87
*/
83
88
contains ( selector : Selector ) {
84
89
const selectorType = getSelectorTypeOrThrow ( selector , 'contains' )
85
- const nodes = findAll ( this . vm , this . vnode , selectorType , selector )
90
+ const nodes = findAll ( this . vm , this . vnode , this . element , selector )
86
91
const is = selectorType === REF_SELECTOR ? false : this . is ( selector )
87
92
return nodes . length > 0 || is
88
93
}
@@ -222,14 +227,15 @@ export default class Wrapper implements BaseWrapper {
222
227
const body = document . querySelector ( 'body' )
223
228
const mockElement = document . createElement ( 'div' )
224
229
225
- if ( ! ( body instanceof HTMLElement ) ) {
230
+ if ( ! ( body instanceof Element ) ) {
226
231
return false
227
232
}
228
233
const mockNode = body . insertBefore ( mockElement , null )
229
234
// $FlowIgnore : Flow thinks style[style] returns a number
230
235
mockElement . style [ style ] = value
231
236
232
- if ( ! this . options . attachedToDocument ) {
237
+ if ( ! this . options . attachedToDocument && ( this . vm || this . vnode ) ) {
238
+ // $FlowIgnore : Possible null value, will be removed in 1.0.0
233
239
const vm = this . vm || this . vnode . context . $root
234
240
body . insertBefore ( vm . $root . _vnode . elm , null )
235
241
}
@@ -243,8 +249,7 @@ export default class Wrapper implements BaseWrapper {
243
249
* Finds first node in tree of the current wrapper that matches the provided selector.
244
250
*/
245
251
find ( selector : Selector ) : Wrapper | ErrorWrapper | VueWrapper {
246
- const selectorType = getSelectorTypeOrThrow ( selector , 'find' )
247
- const nodes = findAll ( this . vm , this . vnode , selectorType , selector )
252
+ const nodes = findAll ( this . vm , this . vnode , this . element , selector )
248
253
if ( nodes . length === 0 ) {
249
254
if ( selector . ref ) {
250
255
return new ErrorWrapper ( `ref="${ selector . ref } "` )
@@ -258,8 +263,8 @@ export default class Wrapper implements BaseWrapper {
258
263
* Finds node in tree of the current wrapper that matches the provided selector.
259
264
*/
260
265
findAll ( selector : Selector ) : WrapperArray {
261
- const selectorType = getSelectorTypeOrThrow ( selector , 'findAll' )
262
- const nodes = findAll ( this . vm , this . vnode , selectorType , selector )
266
+ getSelectorTypeOrThrow ( selector , 'findAll' )
267
+ const nodes = findAll ( this . vm , this . vnode , this . element , selector )
263
268
const wrappers = nodes . map ( node =>
264
269
createWrapper ( node , this . update , this . options )
265
270
)
@@ -313,6 +318,9 @@ export default class Wrapper implements BaseWrapper {
313
318
* Checks if node is empty
314
319
*/
315
320
isEmpty ( ) : boolean {
321
+ if ( ! this . vnode ) {
322
+ return this . element . innerHTML === ''
323
+ }
316
324
return this . vnode . children === undefined || this . vnode . children . length === 0
317
325
}
318
326
@@ -331,6 +339,10 @@ export default class Wrapper implements BaseWrapper {
331
339
return this . vm . $options . name
332
340
}
333
341
342
+ if ( ! this . vnode ) {
343
+ return this . element . tagName
344
+ }
345
+
334
346
return this . vnode . tag
335
347
}
336
348
0 commit comments