Skip to content

Commit df8d096

Browse files
committed
feat: enhanced Vuex Counter example and test spec
To thoroughly demonstrate Vuex support after the refactoring in commit e8773383, Counter.vue now: - Utilizes all Vuex mapping functions. - Can properly use computed mapped getters in its template. This was an issue after cypress-io#13. - Set count state via an input field to demonstrate mapped mutations. Two tests have been added to the spec for this new input field. Close cypress-io#6
1 parent e877338 commit df8d096

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

components/Counter.vue

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
<template>
22
<div>
3-
Clicked: {{ $store.state.count }} times, count is {{ $store.getters.evenOrOdd }}.
3+
Clicked: {{ count }} times, count is {{ evenOrOdd }}.<br>
44
<button @click="increment">+</button>
55
<button @click="decrement">-</button>
66
<button @click="incrementIfOdd">Increment if odd</button>
77
<button @click="incrementAsync">Increment async</button>
8+
<br><br>
9+
<span>Set Count: </span>
10+
<input type="number" :value="count" @input="set($event.target.value || 0)">
811
</div>
912
</template>
1013

1114
<script>
12-
import { mapGetters, mapActions } from 'vuex'
15+
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
1316
export default {
14-
computed: mapGetters([
15-
'evenOrOdd'
16-
]),
17-
methods: mapActions([
18-
'increment',
19-
'decrement',
20-
'incrementIfOdd',
21-
'incrementAsync'
22-
])
17+
computed: {
18+
...mapState(['count']),
19+
...mapGetters(['evenOrOdd'])
20+
},
21+
methods: {
22+
...mapMutations(['set']),
23+
...mapActions([
24+
'increment',
25+
'decrement',
26+
'incrementIfOdd',
27+
'incrementAsync'
28+
])
29+
}
2330
}
2431
</script>

components/store.js

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const state = {
1414
// mutations must be synchronous and can be recorded by plugins
1515
// for debugging purposes.
1616
const mutations = {
17+
set (state, value) {
18+
state.count = value
19+
},
1720
increment (state) {
1821
state.count++
1922
},

cypress.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"viewportWidth": 300,
3-
"viewportHeight": 100,
3+
"viewportHeight": 120,
44
"videoRecording": false,
55
"projectId": "134ej7"
66
}

cypress/integration/counter-vuex-spec.js

+28-9
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@ import mountVue from '../..'
77

88
/* eslint-env mocha */
99
describe('Vuex Counter', () => {
10+
11+
// configure component
1012
const extensions = {
1113
plugins: [Vuex],
1214
components: {
13-
counter: Counter
14-
},
15+
Counter
16+
}
1517
}
18+
19+
// define component template
1620
const template = '<counter />'
17-
beforeEach(mountVue({template, store}, {extensions}))
1821

19-
const getCount = () =>
20-
Cypress.vue.$store.state.count
22+
// define count get and set helpers
23+
const getCount = () => Cypress.vue.$store.state.count
24+
const setCount = value => Cypress.vue.$store.commit('set', value)
2125

22-
const setCount = value =>
23-
Cypress.vue.$set(Cypress.vue.$store.state, 'count', value)
26+
// initialize a fresh Vue app before each test
27+
beforeEach(mountVue({template, store}, {extensions}))
2428

2529
it('starts with zero', () => {
2630
cy.contains('0 times')
@@ -37,15 +41,30 @@ describe('Vuex Counter', () => {
3741
})
3842

3943
it('increments the counter if count is odd', () => {
40-
setCount(3)
44+
setCount(3) // start with an odd number
4145
cy.contains('odd')
42-
cy.contains('button', 'Increment if odd').click()
46+
cy.contains('button', 'Increment if odd').as('btn').click()
47+
cy.contains('even')
48+
cy.get('@btn').click()
4349
cy.contains('even')
4450
})
4551

4652
it('asynchronously increments counter', () => {
4753
const count = getCount()
54+
// increment mutation is delayed by 1 second
55+
// Cypress waits 4 seconds by default
4856
cy.contains('button', 'Increment async').click()
4957
cy.contains(`${count + 1} times`)
5058
})
59+
60+
it('count is zero when input is cleared', () => {
61+
cy.get('input').type(`{selectall}{backspace}`)
62+
cy.contains('0 times')
63+
}),
64+
65+
it('set count via input field', () => {
66+
const count = 42
67+
cy.get('input').type(`{selectall}{backspace}${count}`)
68+
cy.contains(`${count} times`)
69+
})
5170
})

0 commit comments

Comments
 (0)