1
1
import { ComponentPublicInstance } from 'vue'
2
+ import { ShapeFlags } from '@vue/shared'
2
3
3
4
import { DOMWrapper } from './dom-wrapper'
4
5
import { WrapperAPI } from './types'
5
6
import { ErrorWrapper } from './error-wrapper'
6
7
7
8
export class VueWrapper implements WrapperAPI {
8
- vm : ComponentPublicInstance
9
+ rootVM : ComponentPublicInstance
10
+ componentVM : ComponentPublicInstance
9
11
__emitted : Record < string , unknown [ ] > = { }
10
12
11
13
constructor ( vm : ComponentPublicInstance , events : Record < string , unknown [ ] > ) {
12
- this . vm = vm
14
+ this . rootVM = vm
15
+ this . componentVM = this . rootVM . $refs [
16
+ 'VTU_COMPONENT'
17
+ ] as ComponentPublicInstance
13
18
this . __emitted = events
14
19
}
15
20
21
+ private get hasMultipleRoots ( ) : boolean {
22
+ // if the subtree is an array of children, we have multiple root nodes
23
+ return this . componentVM . $ . subTree . shapeFlag === ShapeFlags . ARRAY_CHILDREN
24
+ }
25
+
26
+ get element ( ) {
27
+ return this . hasMultipleRoots
28
+ ? // get the parent element of the current component
29
+ this . componentVM . $el . parentElement
30
+ : this . componentVM . $el
31
+ }
32
+
16
33
classes ( className ?) {
17
- return new DOMWrapper ( this . vm . $el ) . classes ( className )
34
+ return new DOMWrapper ( this . element ) . classes ( className )
18
35
}
19
36
20
37
attributes ( key ?: string ) {
21
- return new DOMWrapper ( this . vm . $el ) . attributes ( key )
38
+ return new DOMWrapper ( this . element ) . attributes ( key )
22
39
}
23
40
24
41
exists ( ) {
@@ -30,15 +47,17 @@ export class VueWrapper implements WrapperAPI {
30
47
}
31
48
32
49
html ( ) {
33
- return this . vm . $el . outerHTML
50
+ return this . hasMultipleRoots
51
+ ? this . element . innerHTML
52
+ : this . element . outerHTML
34
53
}
35
54
36
55
text ( ) {
37
- return this . vm . $el . textContent ?. trim ( )
56
+ return this . element . textContent ?. trim ( )
38
57
}
39
58
40
59
find < T extends Element > ( selector : string ) : DOMWrapper < T > | ErrorWrapper {
41
- const result = this . vm . $el . querySelector ( selector ) as T
60
+ const result = this . element . querySelector ( selector ) as T
42
61
if ( result ) {
43
62
return new DOMWrapper ( result )
44
63
}
@@ -47,16 +66,16 @@ export class VueWrapper implements WrapperAPI {
47
66
}
48
67
49
68
findAll < T extends Element > ( selector : string ) : DOMWrapper < T > [ ] {
50
- const results = ( this . vm . $el as Element ) . querySelectorAll < T > ( selector )
69
+ const results = ( this . element as Element ) . querySelectorAll < T > ( selector )
51
70
return Array . from ( results ) . map ( ( x ) => new DOMWrapper ( x ) )
52
71
}
53
72
54
73
async setChecked ( checked : boolean = true ) {
55
- return new DOMWrapper ( this . vm . $el ) . setChecked ( checked )
74
+ return new DOMWrapper ( this . element ) . setChecked ( checked )
56
75
}
57
76
58
77
trigger ( eventString : string ) {
59
- const rootElementWrapper = new DOMWrapper ( this . vm . $el )
78
+ const rootElementWrapper = new DOMWrapper ( this . element )
60
79
return rootElementWrapper . trigger ( eventString )
61
80
}
62
81
}
0 commit comments