forked from vuejs/test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvueWrapper.ts
268 lines (232 loc) · 8.09 KB
/
vueWrapper.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { ComponentPublicInstance, nextTick, App } from 'vue'
import { ShapeFlags } from '@vue/shared'
import { config } from './config'
import { DOMWrapper } from './domWrapper'
import {
FindAllComponentsSelector,
FindComponentSelector,
VueWrapperMeta
} from './types'
import { createWrapperError } from './errorWrapper'
import { TriggerOptions } from './createDomEvent'
import { find, matches } from './utils/find'
import { isFunctionalComponent } from './utils'
export class VueWrapper<T extends ComponentPublicInstance> {
private componentVM: T
private rootVM: ComponentPublicInstance
private __app: App | null
private __setProps: ((props: Record<string, any>) => void) | undefined
private __isFunctionalComponent: boolean
constructor(
app: App | null,
vm: ComponentPublicInstance,
setProps?: (props: Record<string, any>) => void,
meta?: VueWrapperMeta
) {
this.__app = app
this.rootVM = vm.$root!
this.componentVM = vm as T
this.__setProps = setProps
this.__isFunctionalComponent = meta.isFunctionalComponent
// plugins hook
config.plugins.VueWrapper.extend(this)
}
private get hasMultipleRoots(): boolean {
// if the subtree is an array of children, we have multiple root nodes
return this.vm.$.subTree.shapeFlag === ShapeFlags.ARRAY_CHILDREN
}
private get parentElement(): Element {
return this.vm.$el.parentElement
}
get element(): Element {
// if the component has multiple root elements, we use the parent's element
return this.hasMultipleRoots ? this.parentElement : this.vm.$el
}
get vm(): T {
return this.componentVM
}
props(): { [key: string]: any }
props(selector: string): any
props(selector?: string): { [key: string]: any } | any {
const props = this.componentVM.$props as { [key: string]: any }
return selector ? props[selector] : props
}
classes(): string[]
classes(className: string): boolean
classes(className?: string): string[] | boolean {
return new DOMWrapper(this.element).classes(className)
}
attributes(): { [key: string]: string }
attributes(key: string): string
attributes(key?: string): { [key: string]: string } | string {
return new DOMWrapper(this.element).attributes(key)
}
exists() {
return true
}
emitted<T = unknown>(): Record<string, T[]>
emitted<T = unknown>(eventName?: string): T[]
emitted<T = unknown>(eventName?: string): T[] | Record<string, T[]> {
if (this.__isFunctionalComponent) {
console.warn(
'[Vue Test Utils]: capture events emitted from functional components is currently not supported.'
)
}
if (eventName) {
const emitted = (this.vm['__emitted'] as Record<string, T[]>)[eventName]
return emitted
}
return this.vm['__emitted'] as Record<string, T[]>
}
html() {
// cover cases like <Suspense>, multiple root nodes.
if (this.parentElement['__vue_app__']) {
return this.parentElement.innerHTML
}
return this.element.outerHTML
}
text() {
return this.element.textContent?.trim()
}
find<K extends keyof HTMLElementTagNameMap>(
selector: K
): DOMWrapper<HTMLElementTagNameMap[K]>
find<K extends keyof SVGElementTagNameMap>(
selector: K
): DOMWrapper<SVGElementTagNameMap[K]>
find<T extends Element>(selector: string): DOMWrapper<T>
find(selector: string): DOMWrapper<Element> {
// force using the parentElement to allow finding the root element
const result = this.parentElement.querySelector(selector)
if (result) {
return new DOMWrapper(result)
}
return createWrapperError('DOMWrapper')
}
get<K extends keyof HTMLElementTagNameMap>(
selector: K
): Omit<DOMWrapper<HTMLElementTagNameMap[K]>, 'exists'>
get<K extends keyof SVGElementTagNameMap>(
selector: K
): Omit<DOMWrapper<SVGElementTagNameMap[K]>, 'exists'>
get<T extends Element>(selector: string): Omit<DOMWrapper<T>, 'exists'>
get(selector: string): Omit<DOMWrapper<Element>, 'exists'> {
const result = this.find(selector)
if (result instanceof DOMWrapper) {
return result
}
throw new Error(`Unable to get ${selector} within: ${this.html()}`)
}
findComponent<T extends ComponentPublicInstance>(
selector: new () => T
): VueWrapper<T>
findComponent<T extends ComponentPublicInstance>(
selector: FindComponentSelector
): VueWrapper<T>
findComponent<T extends ComponentPublicInstance>(
selector: any
): VueWrapper<T> {
if (typeof selector === 'object' && 'ref' in selector) {
const result = this.vm.$refs[selector.ref]
if (result) {
return createWrapper(null, result as T, {
isFunctionalComponent: isFunctionalComponent(result)
})
}
}
const result = find(this.vm.$.subTree, selector)
if (result.length) {
return createWrapper(null, result[0], {
isFunctionalComponent: isFunctionalComponent(result)
})
}
// https://github.com/vuejs/vue-test-utils-next/issues/211
// VTU v1 supported finding the component mounted itself.
// eg: mount(Comp).findComponent(Comp)
// this is the same as doing `wrapper.vm`, but we keep this behavior for back compat.
if (matches(this.vm.$.vnode, selector)) {
return createWrapper(null, this.vm.$.vnode.component.proxy, {
isFunctionalComponent: false
})
}
return createWrapperError('VueWrapper')
}
getComponent<T extends ComponentPublicInstance>(
selector: new () => T
): Omit<VueWrapper<T>, 'exists'>
getComponent<T extends ComponentPublicInstance>(
selector: FindComponentSelector
): Omit<VueWrapper<T>, 'exists'>
getComponent<T extends ComponentPublicInstance>(
selector: any
): Omit<VueWrapper<T>, 'exists'> {
const result = this.findComponent(selector)
if (result instanceof VueWrapper) {
return result as VueWrapper<T>
}
let message = 'Unable to get '
if (typeof selector === 'string') {
message += `component with selector ${selector}`
} else if (selector.name) {
message += `component with name ${selector.name}`
} else if (selector.ref) {
message += `component with ref ${selector.ref}`
} else {
message += 'specified component'
}
message += ` within: ${this.html()}`
throw new Error(message)
}
findAllComponents(selector: FindAllComponentsSelector): VueWrapper<T>[] {
return find(this.vm.$.subTree, selector).map((c) =>
createWrapper(null, c, {
isFunctionalComponent: isFunctionalComponent(c)
})
)
}
findAll<K extends keyof HTMLElementTagNameMap>(
selector: K
): DOMWrapper<HTMLElementTagNameMap[K]>[]
findAll<K extends keyof SVGElementTagNameMap>(
selector: K
): DOMWrapper<SVGElementTagNameMap[K]>[]
findAll<T extends Element>(selector: string): DOMWrapper<T>[]
findAll(selector: string): DOMWrapper<Element>[] {
const results = this.parentElement.querySelectorAll(selector)
return Array.from(results).map((element) => new DOMWrapper(element))
}
setProps(props: Record<string, any>): Promise<void> {
// if this VM's parent is not the root or if setProps does not exist, error out
if (this.vm.$parent !== this.rootVM || !this.__setProps) {
throw Error('You can only use setProps on your mounted component')
}
this.__setProps(props)
return nextTick()
}
setValue(value: any, prop?: string): Promise<void> {
const propEvent = prop || 'modelValue'
this.vm.$emit(`update:${propEvent}`, value)
return this.vm.$nextTick()
}
trigger(eventString: string, options?: TriggerOptions) {
const rootElementWrapper = new DOMWrapper(this.element)
return rootElementWrapper.trigger(eventString, options)
}
unmount() {
// preventing dispose of child component
if (!this.__app) {
throw new Error(
`wrapper.unmount() can only be called by the root wrapper`
)
}
this.__app.unmount(this.parentElement)
}
}
export function createWrapper<T extends ComponentPublicInstance>(
app: App | null,
vm: ComponentPublicInstance,
meta: VueWrapperMeta,
setProps?: (props: Record<string, any>) => void
): VueWrapper<T> {
return new VueWrapper<T>(app, vm, setProps, meta)
}