forked from cypress-io/cypress-vue-unit-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
214 lines (186 loc) · 5.4 KB
/
index.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
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
const { stripIndent } = require('common-tags')
// having weak reference to styles prevents garbage collection
// and "losing" styles when the next test starts
const stylesCache = new Map()
const copyStyles = component => {
let styles
if (stylesCache.has(component)) {
styles = stylesCache.get(component)
} else {
styles = document.querySelectorAll('head style')
if (styles.length) {
console.log('injected %d styles', styles.length)
stylesCache.set(component, styles)
} else {
console.log('No styles injected for this component')
styles = null
}
}
if (!styles) {
return
}
const parentDocument = window.parent.document
const projectName = Cypress.config('projectName')
const appIframeId = `Your App: '${projectName}'`
const appIframe = parentDocument.getElementById(appIframeId)
const head = appIframe.contentDocument.querySelector('head')
styles.forEach(style => {
head.appendChild(style)
})
}
const deleteConstructor = comp => delete comp._Ctor
const deleteCachedConstructors = component => {
if (!component.components) {
return
}
Cypress._.values(component.components).forEach(deleteConstructor)
}
const getVuePath = options =>
options.vue || options.vuePath || '../node_modules/vue/dist/vue.js'
const getVuexPath = options => options.vuex || options.vuexPath
const getPageHTML = options => {
if (options.html) {
return options.html
}
const vue = getVuePath(options)
let vuex = getVuexPath(options)
if (vuex) {
vuex = `<script src="${vuex}"></script>`
}
// note: add "base" tag to force loading static assets
// from the server, not from the "spec" file URL
if (options.base) {
if (vue.startsWith('.')) {
console.error(
'You are using base tag %s and relative Vue path %s',
options.base,
vue
)
console.error('the relative path might NOT work')
console.error(
'maybe pass Vue url using "https://unpkg.com/vue/dist/vue.js"'
)
}
return stripIndent`
<html>
<head>
<base href="${options.base}" />
</head>
<body>
<div id="app"></div>
<script src="${vue}"></script>
</body>
</html>
`
}
const vueHtml = stripIndent`
<html>
<head></head>
<body>
<div id="app"></div>
<script src="${vue}"></script>
${vuex || ''}
</body>
</html>
`
return vueHtml
}
const registerGlobalComponents = (Vue, options) => {
const globalComponents = Cypress._.get(options, 'extensions.components')
if (Cypress._.isPlainObject(globalComponents)) {
Cypress._.forEach(globalComponents, (component, id) => {
Vue.component(id, component)
})
}
}
const installPlugins = (Vue, options) => {
const plugins =
Cypress._.get(options, 'extensions.use') ||
Cypress._.get(options, 'extensions.plugins')
if (Cypress._.isArray(plugins)) {
plugins.forEach(plugin => {
Vue.use(plugin)
})
}
}
const installMixins = (Vue, options) => {
const mixins =
Cypress._.get(options, 'extensions.mixin') ||
Cypress._.get(options, 'extensions.mixins')
if (Cypress._.isArray(mixins)) {
mixins.forEach(mixin => {
Vue.mixin(mixin)
})
}
}
const isOptionName = name =>
['vue', 'html', 'vuePath', 'base', 'extensions'].includes(name)
const isOptions = object => Object.keys(object).every(isOptionName)
const isConstructor = object => object && object._compiled
const hasStore = ({ store }) => store && store._vm
const forEachValue = (obj, fn) => {
Object.keys(obj).forEach(key => fn(obj[key], key))
}
const resetStoreVM = (Vue, { store }) => {
// bind store public getters
store.getters = {}
const wrappedGetters = store._wrappedGetters
const computed = {}
forEachValue(wrappedGetters, (fn, key) => {
// use computed to leverage its lazy-caching mechanism
computed[key] = () => fn(store)
Object.defineProperty(store.getters, key, {
get: () => store._vm[key],
enumerable: true // for local getters
})
})
store._watcherVM = new Vue()
store._vm = new Vue({
data: {
$$state: store._vm._data.$$state
},
computed
})
return store
}
// the double function allows mounting a component quickly
// beforeEach(mountVue(component, options))
const mountVue = (component, optionsOrProps = {}) => () => {
let options = {}
let props = {}
if (isOptions(optionsOrProps)) {
options = optionsOrProps
} else {
props = optionsOrProps
}
const vueHtml = getPageHTML(options)
const document = cy.state('document')
document.write(vueHtml)
document.close()
// TODO: do not log out "its(Vue)" command
// but it currently does not support it
return cy
.window({ log: false })
.its('Vue')
.then(Vue => {
// refresh inner Vue instance of Vuex store
if (hasStore(component)) {
component.store = resetStoreVM(Vue, component)
}
installMixins(Vue, options)
installPlugins(Vue, options)
registerGlobalComponents(Vue, options)
deleteCachedConstructors(component)
if (isConstructor(component)) {
const Cmp = Vue.extend(component)
Cypress.vue = new Cmp(props).$mount('#app')
copyStyles(Cmp)
} else {
const allOptions = Object.assign({}, component, { el: '#app' })
Cypress.vue = new Vue(allOptions)
copyStyles(component)
}
return Cypress.vue
})
}
module.exports = mountVue