Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit 7f1db65

Browse files
committed
working on Vuex example for #6
1 parent dd7c07f commit 7f1db65

File tree

5 files changed

+115
-2
lines changed

5 files changed

+115
-2
lines changed

components/Counter.vue

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div id="app">
3+
Clicked: {{ $store.state.count }} times, count is {{ evenOrOdd }}.
4+
<button @click="increment">+</button>
5+
<button @click="decrement">-</button>
6+
<button @click="incrementIfOdd">Increment if odd</button>
7+
<button @click="incrementAsync">Increment async</button>
8+
</div>
9+
</template>
10+
11+
<script>
12+
import { mapGetters, mapActions } from 'vuex'
13+
export default {
14+
computed: mapGetters([
15+
'evenOrOdd'
16+
]),
17+
methods: mapActions([
18+
'increment',
19+
'decrement',
20+
'incrementIfOdd',
21+
'incrementAsync'
22+
])
23+
}
24+
</script>

components/store.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Vue from 'vue'
2+
import Vuex from 'vuex'
3+
Vue.use(Vuex)
4+
5+
// root state object.
6+
// each Vuex instance is just a single state tree.
7+
const state = {
8+
count: 0
9+
}
10+
11+
// mutations are operations that actually mutates the state.
12+
// each mutation handler gets the entire state tree as the
13+
// first argument, followed by additional payload arguments.
14+
// mutations must be synchronous and can be recorded by plugins
15+
// for debugging purposes.
16+
const mutations = {
17+
increment (state) {
18+
state.count++
19+
},
20+
decrement (state) {
21+
state.count--
22+
}
23+
}
24+
25+
// actions are functions that cause side effects and can involve
26+
// asynchronous operations.
27+
const actions = {
28+
increment: ({ commit }) => commit('increment'),
29+
decrement: ({ commit }) => commit('decrement'),
30+
incrementIfOdd ({ commit, state }) {
31+
if ((state.count + 1) % 2 === 0) {
32+
commit('increment')
33+
}
34+
},
35+
incrementAsync ({ commit }) {
36+
return new Promise((resolve, reject) => {
37+
setTimeout(() => {
38+
commit('increment')
39+
resolve()
40+
}, 1000)
41+
})
42+
}
43+
}
44+
45+
// getters are functions
46+
const getters = {
47+
evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
48+
}
49+
50+
// A Vuex instance is created by combining the state, mutations, actions,
51+
// and getters.
52+
export default new Vuex.Store({
53+
state,
54+
getters,
55+
actions,
56+
mutations
57+
})
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// testing Vuex component
2+
// https://github.com/vuejs/vuex/tree/dev/examples/counter
3+
import Counter from '../../components/Counter.vue'
4+
import store from '../../components/store'
5+
const mountVue = require('../..')
6+
7+
/* eslint-env mocha */
8+
describe('Vuex Counter', () => {
9+
const extensions = {
10+
components: {
11+
counter: Counter
12+
},
13+
}
14+
const template = '<counter />'
15+
beforeEach(mountVue({template, store}, {extensions}))
16+
17+
it('starts with zero', () => {
18+
cy.contains('button', '0')
19+
})
20+
})

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
"standard": "10.0.3",
8989
"vue": "2.5.13",
9090
"vue-loader": "13.6.1",
91-
"vue-template-compiler": "2.5.13"
91+
"vue-template-compiler": "2.5.13",
92+
"vuex": "3.0.1"
9293
},
9394
"standard": {
9495
"globals": [

src/index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,18 @@ 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
50+
4851
const getPageHTML = options => {
4952
if (options.html) {
5053
return options.html
5154
}
5255
const vue = getVuePath(options)
56+
let vuex = getVuexPath(options)
57+
if (vuex) {
58+
vuex = `<script src="${vuex}"></script>`
59+
}
5360

5461
// note: add "base" tag to force loading static assets
5562
// from the server, not from the "spec" file URL
@@ -84,6 +91,7 @@ const getPageHTML = options => {
8491
<body>
8592
<div id="app"></div>
8693
<script src="${vue}"></script>
94+
${vuex ? vuex : ''}
8795
</body>
8896
</html>
8997
`
@@ -161,7 +169,10 @@ const mountVue = (component, optionsOrProps = {}) => () => {
161169
Cypress.vue = new Cmp(props).$mount('#app')
162170
copyStyles(Cmp)
163171
} else {
164-
Cypress.vue = new Vue(component).$mount('#app')
172+
debugger
173+
// Cypress.vue = new Vue(component).$mount('#app')
174+
const allOptions = Object.assign({}, component, {el: '#app'})
175+
Cypress.vue = new Vue(allOptions)
165176
copyStyles(component)
166177
}
167178

0 commit comments

Comments
 (0)