Skip to content

Commit 321ba88

Browse files
committed
fix: vuex integration (cypress-io#6)
The inner Vue instance within Vuex Store must be refeshed by `resetStoreVM` to restore reactivity of the store state. This doesn’t fix stale mapped getters within components. That’s a separate WIP issue.
1 parent 7f1db65 commit 321ba88

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/index.js

+37-6
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ const deleteCachedConstructors = component => {
4545
const getVuePath = options =>
4646
options.vue || options.vuePath || '../node_modules/vue/dist/vue.js'
4747

48-
const getVuexPath = options =>
49-
options.vuex || options.vuexPath
48+
const getVuexPath = options => options.vuex || options.vuexPath
5049

5150
const getPageHTML = options => {
5251
if (options.html) {
@@ -91,7 +90,7 @@ const getPageHTML = options => {
9190
<body>
9291
<div id="app"></div>
9392
<script src="${vue}"></script>
94-
${vuex ? vuex : ''}
93+
${vuex || ''}
9594
</body>
9695
</html>
9796
`
@@ -136,6 +135,36 @@ const isOptions = object => Object.keys(object).every(isOptionName)
136135

137136
const isConstructor = object => object && object._compiled
138137

138+
const hasStore = ({ store }) => store && store._vm
139+
140+
const forEachValue = (obj, fn) => {
141+
Object.keys(obj).forEach(key => fn(obj[key], key))
142+
}
143+
144+
const resetStoreVM = (Vue, { store }) => {
145+
// bind store public getters
146+
store.getters = {}
147+
const wrappedGetters = store._wrappedGetters
148+
const computed = {}
149+
forEachValue(wrappedGetters, (fn, key) => {
150+
// use computed to leverage its lazy-caching mechanism
151+
computed[key] = () => fn(store)
152+
Object.defineProperty(store.getters, key, {
153+
get: () => store._vm[key],
154+
enumerable: true // for local getters
155+
})
156+
})
157+
158+
store._watcherVM = new Vue()
159+
store._vm = new Vue({
160+
data: {
161+
$$state: store._vm._data.$$state
162+
},
163+
computed
164+
})
165+
return store
166+
}
167+
139168
// the double function allows mounting a component quickly
140169
// beforeEach(mountVue(component, options))
141170
const mountVue = (component, optionsOrProps = {}) => () => {
@@ -159,6 +188,10 @@ const mountVue = (component, optionsOrProps = {}) => () => {
159188
.window({ log: false })
160189
.its('Vue')
161190
.then(Vue => {
191+
// refresh inner Vue instance of Vuex store
192+
if (hasStore(component)) {
193+
component.store = resetStoreVM(Vue, component)
194+
}
162195
installMixins(Vue, options)
163196
installPlugins(Vue, options)
164197
registerGlobalComponents(Vue, options)
@@ -169,9 +202,7 @@ const mountVue = (component, optionsOrProps = {}) => () => {
169202
Cypress.vue = new Cmp(props).$mount('#app')
170203
copyStyles(Cmp)
171204
} else {
172-
debugger
173-
// Cypress.vue = new Vue(component).$mount('#app')
174-
const allOptions = Object.assign({}, component, {el: '#app'})
205+
const allOptions = Object.assign({}, component, { el: '#app' })
175206
Cypress.vue = new Vue(allOptions)
176207
copyStyles(component)
177208
}

0 commit comments

Comments
 (0)