@@ -8,44 +8,35 @@ import {
8
8
WrapperAPI
9
9
} from './types'
10
10
import { ErrorWrapper } from './error-wrapper'
11
- import { MOUNT_ELEMENT_ID } from './constants'
12
11
import { find } from './utils/find'
13
12
14
13
export class VueWrapper implements WrapperAPI {
15
14
private componentVM : ComponentPublicInstance
16
- private __emitted : Record < string , unknown [ ] > = { }
17
- private __vm : ComponentPublicInstance
15
+ private rootVM : ComponentPublicInstance
18
16
private __setProps : ( props : Record < string , any > ) => void
19
17
20
18
constructor (
21
19
vm : ComponentPublicInstance ,
22
- events : Record < string , unknown [ ] > ,
23
- setProps : ( props : Record < string , any > ) => void
20
+ setProps ?: ( props : Record < string , any > ) => void
24
21
) {
25
- this . __vm = vm
22
+ // TODO Remove cast after Vue releases the fix
23
+ this . rootVM = ( vm . $root as any ) as ComponentPublicInstance
24
+ this . componentVM = vm
26
25
this . __setProps = setProps
27
- this . componentVM = this . __vm . $refs [
28
- 'VTU_COMPONENT'
29
- ] as ComponentPublicInstance
30
- this . __emitted = events
31
- }
32
-
33
- private get appRootNode ( ) {
34
- return document . getElementById ( MOUNT_ELEMENT_ID ) as HTMLDivElement
35
26
}
36
27
37
28
private get hasMultipleRoots ( ) : boolean {
38
29
// if the subtree is an array of children, we have multiple root nodes
39
- return this . componentVM . $ . subTree . shapeFlag === ShapeFlags . ARRAY_CHILDREN
30
+ return this . vm . $ . subTree . shapeFlag === ShapeFlags . ARRAY_CHILDREN
40
31
}
41
32
42
33
private get parentElement ( ) : Element {
43
- return this . componentVM . $el . parentElement
34
+ return this . vm . $el . parentElement
44
35
}
45
36
46
37
get element ( ) : Element {
47
38
// if the component has multiple root elements, we use the parent's element
48
- return this . hasMultipleRoots ? this . parentElement : this . componentVM . $el
39
+ return this . hasMultipleRoots ? this . parentElement : this . vm . $el
49
40
}
50
41
51
42
get vm ( ) : ComponentPublicInstance {
@@ -64,8 +55,10 @@ export class VueWrapper implements WrapperAPI {
64
55
return true
65
56
}
66
57
67
- emitted ( ) {
68
- return this . __emitted
58
+ emitted ( ) : Record < string , unknown [ ] > {
59
+ // TODO Should we define this?
60
+ // @ts -ignore
61
+ return this . vm . __emitted
69
62
}
70
63
71
64
html ( ) {
@@ -95,26 +88,32 @@ export class VueWrapper implements WrapperAPI {
95
88
return result
96
89
}
97
90
98
- findComponent ( selector : FindComponentSelector ) : ComponentPublicInstance {
91
+ findComponent ( selector : FindComponentSelector ) : VueWrapper | ErrorWrapper {
99
92
if ( typeof selector === 'object' && 'ref' in selector ) {
100
- return this . componentVM . $refs [ selector . ref ] as ComponentPublicInstance
93
+ return createWrapper (
94
+ this . vm . $refs [ selector . ref ] as ComponentPublicInstance
95
+ )
101
96
}
102
- const result = find ( this . componentVM . $ . subTree , selector )
103
- return result . length ? result [ 0 ] : undefined
97
+ const result = find ( this . vm . $ . subTree , selector )
98
+ if ( ! result . length ) return new ErrorWrapper ( { selector } )
99
+ return createWrapper ( result [ 0 ] )
104
100
}
105
101
106
- findAllComponents (
107
- selector : FindAllComponentsSelector
108
- ) : ComponentPublicInstance [ ] {
109
- return find ( this . componentVM . $ . subTree , selector )
102
+ findAllComponents ( selector : FindAllComponentsSelector ) : VueWrapper [ ] {
103
+ return find ( this . vm . $ . subTree , selector ) . map ( ( c ) => createWrapper ( c ) )
110
104
}
111
105
112
106
findAll < T extends Element > ( selector : string ) : DOMWrapper < T > [ ] {
113
107
const results = this . parentElement . querySelectorAll < T > ( selector )
114
108
return Array . from ( results ) . map ( ( x ) => new DOMWrapper ( x ) )
115
109
}
116
110
117
- setProps ( props : Record < string , any > ) {
111
+ setProps ( props : Record < string , any > ) : Promise < void > {
112
+ // if this VM's parent is not the root, error out
113
+ // TODO: Remove ignore after Vue releases fix
114
+ // @ts -ignore
115
+ if ( this . vm . $parent !== this . rootVM )
116
+ throw Error ( 'You can only use setProps on your mounted component' )
118
117
this . __setProps ( props )
119
118
return nextTick ( )
120
119
}
@@ -127,8 +126,7 @@ export class VueWrapper implements WrapperAPI {
127
126
128
127
export function createWrapper (
129
128
vm : ComponentPublicInstance ,
130
- events : Record < string , unknown [ ] > ,
131
- setProps : ( props : Record < string , any > ) => void
129
+ setProps ?: ( props : Record < string , any > ) => void
132
130
) : VueWrapper {
133
- return new VueWrapper ( vm , events , setProps )
131
+ return new VueWrapper ( vm , setProps )
134
132
}
0 commit comments