forked from testing-library/vue-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-testing-library.js
149 lines (126 loc) · 3.32 KB
/
vue-testing-library.js
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
import { createLocalVue, mount } from '@vue/test-utils'
import {
getQueriesForElement,
prettyDOM,
wait,
fireEvent
} from '@testing-library/dom'
const mountedWrappers = new Set()
function render(
TestComponent,
{ store = null, routes = null, ...mountOptions } = {},
configurationCb
) {
const localVue = createLocalVue()
let vuexStore = null
let router = null
let additionalOptions = {}
if (store) {
const Vuex = require('vuex')
localVue.use(Vuex)
vuexStore = new Vuex.Store(store)
}
if (routes) {
const VueRouter = require('vue-router')
localVue.use(VueRouter)
router = new VueRouter({
routes
})
}
if (configurationCb && typeof configurationCb === 'function') {
additionalOptions = configurationCb(localVue, vuexStore, router)
}
if (!mountOptions.propsData && !!mountOptions.props) {
mountOptions.propsData = mountOptions.props
delete mountOptions.props
}
const wrapper = mount(TestComponent, {
localVue,
router,
store: vuexStore,
attachToDocument: true,
sync: false,
...mountOptions,
...additionalOptions
})
mountedWrappers.add(wrapper)
if (wrapper.element.parentNode === document.body) {
const div = document.createElement('div')
wrapper.element.parentNode.insertBefore(div, wrapper.element)
div.appendChild(wrapper.element)
}
return {
container: wrapper.element.parentNode,
baseElement: document.body,
debug: (el = wrapper.element) => console.log(prettyDOM(el)),
unmount: () => wrapper.destroy(),
isUnmounted: () => wrapper.vm._isDestroyed,
html: () => wrapper.html(),
emitted: () => wrapper.emitted(),
updateProps: _ => {
wrapper.setProps(_)
return wait()
},
...getQueriesForElement(wrapper.element.parentNode)
}
}
function cleanup() {
mountedWrappers.forEach(cleanupAtWrapper)
}
function cleanupAtWrapper(wrapper) {
if (
wrapper.element.parentNode &&
wrapper.element.parentNode.parentNode === document.body
) {
document.body.removeChild(wrapper.element.parentNode)
}
wrapper.destroy()
mountedWrappers.delete(wrapper)
}
Object.keys(fireEvent).forEach(fn => {
fireEvent[`_${fn}`] = fireEvent[fn]
fireEvent[fn] = async (...params) => {
fireEvent[`_${fn}`](...params)
await wait()
}
})
fireEvent.touch = async elem => {
await fireEvent.focus(elem)
await fireEvent.blur(elem)
}
fireEvent.update = async (elem, value) => {
const tagName = elem.tagName
const type = elem.type
switch (tagName) {
case 'OPTION': {
elem.selected = value
const parentElement =
this.element.parentElement.tagName === 'OPTGROUP'
? this.element.parentElement.parentElement
: this.element.parentElement
return fireEvent.change(parentElement)
}
case 'INPUT': {
if (type === 'checkbox') {
elem.checked = value
return fireEvent.change(elem)
} else if (type === 'radio') {
elem.selected = value
return fireEvent.change(elem)
} else {
elem.value = value
return fireEvent.input(elem)
}
}
case 'TEXTAREA': {
elem.value = value
return fireEvent.input(elem)
}
case 'SELECT': {
elem.value = value
return fireEvent.change(elem)
}
}
}
export * from '@testing-library/dom'
export { cleanup, render }