-
Notifications
You must be signed in to change notification settings - Fork 254
/
baseWrapper.ts
391 lines (345 loc) · 12.6 KB
/
baseWrapper.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import { textContent } from './utils'
import type { TriggerOptions } from './createDomEvent'
import {
ComponentInternalInstance,
ComponentOptions,
ComponentPublicInstance,
ComputedOptions,
CreateComponentPublicInstance,
FunctionalComponent,
MethodOptions,
nextTick
} from 'vue'
import { createDOMEvent } from './createDomEvent'
import { DomEventNameWithModifier } from './constants/dom-events'
import type { VueWrapper } from './vueWrapper'
import {
DefinedComponent,
FindAllComponentsSelector,
FindComponentSelector,
NameSelector,
RefSelector,
VueNode
} from './types'
import WrapperLike from './interfaces/wrapperLike'
import { find, matches } from './utils/find'
import { createWrapperError } from './errorWrapper'
import { isElementVisible } from './utils/isElementVisible'
import { isElement } from './utils/isElement'
import type { DOMWrapper } from './domWrapper'
import { createDOMWrapper, createVueWrapper } from './wrapperFactory'
import { stringifyNode } from './utils/stringifyNode'
import beautify, { HTMLBeautifyOptions } from 'js-beautify'
export default abstract class BaseWrapper<ElementType extends Node>
implements WrapperLike
{
protected readonly wrapperElement: VueNode<ElementType>
protected abstract getRootNodes(): VueNode[]
get element() {
return this.wrapperElement
}
protected constructor(element: ElementType) {
this.wrapperElement = element
}
protected findAllDOMElements(selector: string): Element[] {
const elementRootNodes = this.getRootNodes().filter(isElement)
if (elementRootNodes.length === 0) return []
const result: Element[] = [
...elementRootNodes.filter((node) => node.matches(selector))
]
elementRootNodes.forEach((rootNode) => {
result.push(...Array.from(rootNode.querySelectorAll(selector)))
})
return result
}
find<K extends keyof HTMLElementTagNameMap>(
selector: K
): DOMWrapper<HTMLElementTagNameMap[K]>
find<K extends keyof SVGElementTagNameMap>(
selector: K
): DOMWrapper<SVGElementTagNameMap[K]>
find<T extends Element = Element>(selector: string): DOMWrapper<T>
find<T extends Node = Node>(selector: string | RefSelector): DOMWrapper<T>
find(selector: string | RefSelector): DOMWrapper<Node> {
if (typeof selector === 'object' && 'ref' in selector) {
const currentComponent = this.getCurrentComponent()
if (!currentComponent) {
return createWrapperError('DOMWrapper')
}
let result = currentComponent.refs[selector.ref]
// When using ref inside v-for, then refs contains array of component instances and nodes
if (Array.isArray(result)) {
result = result.length ? result[0] : undefined
}
if (result instanceof Node) {
return createDOMWrapper(result)
} else {
return createWrapperError('DOMWrapper')
}
}
const elements = this.findAll(selector)
if (elements.length > 0) {
return elements[0]
}
return createWrapperError('DOMWrapper')
}
abstract findAll<K extends keyof HTMLElementTagNameMap>(
selector: K
): DOMWrapper<HTMLElementTagNameMap[K]>[]
abstract findAll<K extends keyof SVGElementTagNameMap>(
selector: K
): DOMWrapper<SVGElementTagNameMap[K]>[]
abstract findAll<T extends Element>(selector: string): DOMWrapper<T>[]
abstract findAll(selector: string): DOMWrapper<Element>[]
// searching by string without specifying component results in WrapperLike object
findComponent<T extends never>(selector: string): WrapperLike
// Find Component Options aka plain object
findComponent<
Props,
RawBindings = any,
D = any,
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions
>(
selector: ComponentOptions<Props, RawBindings, D, C, M>
): VueWrapper<CreateComponentPublicInstance<Props, RawBindings, D, C, M>>
findComponent<T extends ComponentOptions>(
selector: string
): VueWrapper<
T extends ComponentOptions<
infer Props,
infer RawBindings,
infer D,
infer C,
infer M
>
? CreateComponentPublicInstance<Props, RawBindings, D, C, M>
: VueWrapper<CreateComponentPublicInstance>
>
// searching for component created via defineComponent results in VueWrapper of proper type
findComponent<T extends DefinedComponent>(
selector: T | Exclude<FindComponentSelector, FunctionalComponent>
): VueWrapper<InstanceType<T>>
// searching for functional component results in DOMWrapper
findComponent<T extends FunctionalComponent>(selector: T): DOMWrapper<Node>
findComponent<T extends FunctionalComponent>(
selector: string
): DOMWrapper<Element>
// searching by name or ref always results in VueWrapper
findComponent<T extends never>(
selector: NameSelector | RefSelector
): VueWrapper
findComponent<T extends ComponentPublicInstance>(
selector: T | FindComponentSelector
): VueWrapper<T>
// catch all declaration
findComponent<T extends never>(selector: FindComponentSelector): WrapperLike
findComponent(selector: FindComponentSelector): WrapperLike {
const currentComponent = this.getCurrentComponent()
if (!currentComponent) {
return createWrapperError('VueWrapper')
}
if (typeof selector === 'object' && 'ref' in selector) {
let result = currentComponent.refs[selector.ref]
// When using ref inside v-for, then refs contains array of component instances
if (Array.isArray(result)) {
result = result.length ? result[0] : undefined
}
if (result && !(result instanceof HTMLElement)) {
return createVueWrapper(null, result as ComponentPublicInstance)
} else {
return createWrapperError('VueWrapper')
}
}
if (
matches(currentComponent.vnode, selector) &&
this.element.contains(currentComponent.vnode.el as Node)
) {
return createVueWrapper(
null,
currentComponent.subTree.component
? currentComponent.subTree.component.proxy!
: currentComponent.proxy!
)
}
const [result] = this.findAllComponents(selector)
return result ?? createWrapperError('VueWrapper')
}
findAllComponents<T extends never>(selector: string): WrapperLike[]
findAllComponents<T extends DefinedComponent>(
selector: T | Exclude<FindAllComponentsSelector, FunctionalComponent>
): VueWrapper<InstanceType<T>>[]
findAllComponents<T extends FunctionalComponent>(
selector: T
): DOMWrapper<Node>[]
findAllComponents<T extends FunctionalComponent>(
selector: string
): DOMWrapper<Element>[]
findAllComponents<T extends never>(selector: NameSelector): VueWrapper[]
findAllComponents<T extends ComponentPublicInstance>(
selector: T | FindAllComponentsSelector
): VueWrapper<T>[]
// catch all declaration
findAllComponents<T extends never>(
selector: FindAllComponentsSelector
): WrapperLike[]
findAllComponents(selector: FindAllComponentsSelector): WrapperLike[] {
const currentComponent = this.getCurrentComponent()
if (!currentComponent) {
return []
}
const results = find(currentComponent.subTree, selector)
return results.map((c) =>
c.proxy
? createVueWrapper(null, c.proxy)
: createDOMWrapper(c.vnode.el as Element)
)
}
abstract setValue(value?: any): Promise<void>
html(options?: { raw?: boolean }): string {
const stringNodes = this.getRootNodes().map((node) => stringifyNode(node))
if (options?.raw) return stringNodes.join('')
return stringNodes
.map((node) =>
beautify.html(node, {
unformatted: ['code', 'pre', 'em', 'strong', 'span'],
indent_inner_html: true,
indent_size: 2,
inline_custom_elements: false
// TODO the cast can be removed when @types/js-beautify will be up-to-date
} as HTMLBeautifyOptions)
)
.join('\n')
}
classes(): string[]
classes(className: string): boolean
classes(className?: string): string[] | boolean {
const classes = isElement(this.element)
? Array.from(this.element.classList)
: []
if (className) return classes.includes(className)
return classes
}
attributes(): { [key: string]: string }
attributes(key: string): string | undefined
attributes(key?: string): { [key: string]: string } | string | undefined {
const attributeMap: Record<string, string> = {}
if (isElement(this.element)) {
const attributes = Array.from(this.element.attributes)
for (const attribute of attributes) {
attributeMap[attribute.localName] = attribute.value
}
}
return key ? attributeMap[key] : attributeMap
}
text() {
return this.getRootNodes().map(textContent).join('')
}
exists() {
return true
}
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 = Element>(
selector: string
): Omit<DOMWrapper<T>, 'exists'>
get<T extends Node = Node>(
selector: string | RefSelector
): Omit<DOMWrapper<T>, 'exists'>
get(selector: string | RefSelector): Omit<DOMWrapper<Node>, 'exists'> {
const result = this.find(selector)
if (result.exists()) {
return result
}
throw new Error(`Unable to get ${selector} within: ${this.html()}`)
}
getComponent<T extends never>(selector: string): Omit<WrapperLike, 'exists'>
getComponent<T extends DefinedComponent>(
selector: T | Exclude<FindComponentSelector, FunctionalComponent>
): Omit<VueWrapper<InstanceType<T>>, 'exists'>
// searching for functional component results in DOMWrapper
getComponent<T extends FunctionalComponent>(
selector: T | string
): Omit<DOMWrapper<Element>, 'exists'>
// searching by name or ref always results in VueWrapper
getComponent<T extends never>(
selector: NameSelector | RefSelector
): Omit<VueWrapper, 'exists'>
getComponent<T extends ComponentPublicInstance>(
selector: T | FindComponentSelector
): Omit<VueWrapper<T>, 'exists'>
// catch all declaration
getComponent<T extends never>(
selector: FindComponentSelector
): Omit<WrapperLike, 'exists'>
getComponent(selector: FindComponentSelector): Omit<WrapperLike, 'exists'> {
const result = this.findComponent(selector)
if (result.exists()) {
return result
}
let message = 'Unable to get '
if (typeof selector === 'string') {
message += `component with selector ${selector}`
} else if ('name' in selector) {
message += `component with name ${selector.name}`
} else if ('ref' in selector) {
message += `component with ref ${selector.ref}`
} else {
message += 'specified component'
}
message += ` within: ${this.html()}`
throw new Error(message)
}
protected isDisabled = () => {
const validTagsToBeDisabled = [
'BUTTON',
'COMMAND',
'FIELDSET',
'KEYGEN',
'OPTGROUP',
'OPTION',
'SELECT',
'TEXTAREA',
'INPUT'
]
const hasDisabledAttribute = this.attributes().disabled !== undefined
const elementCanBeDisabled =
isElement(this.element) &&
validTagsToBeDisabled.includes(this.element.tagName)
return hasDisabledAttribute && elementCanBeDisabled
}
isVisible() {
return isElement(this.element) && isElementVisible(this.element)
}
protected abstract getCurrentComponent(): ComponentInternalInstance | void
async trigger(
eventString: DomEventNameWithModifier,
options?: TriggerOptions
): Promise<void>
async trigger(eventString: string, options?: TriggerOptions): Promise<void>
async trigger(eventString: string, options?: TriggerOptions) {
if (options && options['target']) {
throw Error(
`[vue-test-utils]: you cannot set the target value of an event. See the notes section ` +
`of the docs for more details—` +
`https://vue-test-utils.vuejs.org/api/wrapper/trigger.html`
)
}
if (this.element && !this.isDisabled()) {
const event = createDOMEvent(eventString, options)
// see https://github.com/vuejs/test-utils/issues/1854
// fakeTimers provoke an issue as Date.now() always return the same value
// and Vue relies on it to determine if the handler should be invoked
// see https://github.com/vuejs/core/blob/5ee40532a63e0b792e0c1eccf3cf68546a4e23e9/packages/runtime-dom/src/modules/events.ts#L100-L104
// we workaround this issue by manually setting _vts to Date.now() + 1
// thus making sure the event handler is invoked
event._vts = Date.now() + 1
this.element.dispatchEvent(event)
}
return nextTick()
}
}