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

Commit 8097ef8

Browse files
amirrustambahmutov
authored andcommitted
Full Vuex support with enhanced Counter.vue example and test spec (#15)
* fix: vuex integration (#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. * fix: stale mapped getter `evenOrOdd` (#6) Current mapped vuex getters within components become stale. To have a working Counter example, the computed mapped `evenOrOdd` is accessed directly via `$store`. This will be changed back once the stale mapped getter issue (WIP) is fixed. * feat: added example tests for Counter component Added tests to cover all the features within the component. Fixes #6 * minor: following better practices * fix: refactor Vue initialization in app frame Refactors how the Vue instance within the component app frame is initialized, and fully fixes Vue integration. - fixes lingering issues after pull request #13 - fully fixes #6 - fixes amirrustam#1 - README docs have been updated to reflect refactoring - 'vue' option has been deprecated (with warning to user) as it's no longer necessary - spread operator support has been added for upcoming Vuex example * feat: enhanced Vuex Counter example and test spec To thoroughly demonstrate Vuex support after the refactoring in commit amirrustam/cypress-vue-unit-test@e8773383, Counter.vue now: - Utilizes all Vuex mapping functions. - Can properly use computed mapped getters in its template. This was an issue after #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 #6 * minor: remove redundant declaration
1 parent 3e1cfeb commit 8097ef8

File tree

8 files changed

+142
-116
lines changed

8 files changed

+142
-116
lines changed

README.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ See examples below for details.
4545
See [cypress/integration/options-spec.js](cypress/integration/options-spec.js)
4646
for examples of options.
4747

48-
* `vue` - path or URL to the Vue library to load. By default, will
49-
try to load `../node_modules/vue/dist/vue.js`, but you can pass your
50-
own path or URL.
48+
* `mountId` - specify root Vue app mount element ID. Defaults to `app`.
5149

5250
```js
5351
const options = {
54-
vue: 'https://unpkg.com/vue'
52+
mountId: 'rootApp' // div#rootApp
5553
}
5654
beforeEach(mountVue(/* my Vue code */, options))
5755
```
@@ -71,12 +69,25 @@ beforeEach(mountVue(/* my Vue code */, options))
7169
place to load additional libraries, polyfills and styles.
7270

7371
```js
74-
const vue = '../node_modules/vue/dist/vue.js'
72+
const polyfill = '../node_modules/mypolyfill/dist/polyfill.js'
7573
const options = {
76-
html: `<div id="app"></div><script src="${vue}"></script>`
74+
html: `<div id="app"></div><script src="${polyfill}"></script>`
75+
}
76+
beforeEach(mountVue(/* my Vue code */, options))
77+
```
78+
79+
* `vue` **[DEPRECATED]** - path or URL to the Vue library to load. By default, will
80+
try to load `../node_modules/vue/dist/vue.js`, but you can pass your
81+
own path or URL.
82+
83+
```js
84+
const options = {
85+
vue: 'https://unpkg.com/vue'
7786
}
7887
beforeEach(mountVue(/* my Vue code */, options))
7988
```
89+
> #### Deprecation Warning
90+
> `vue` option has been deprecated. `node_modules/vue/dist/vue` is always used.
8091
8192
### Global Vue extensions
8293

components/.babelrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": [
3+
"transform-object-rest-spread"
4+
]
5+
}

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
})

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"generateNotes": "github-post-release"
7272
},
7373
"devDependencies": {
74+
"babel-plugin-transform-object-rest-spread": "6.26.0",
7475
"ban-sensitive-files": "1.9.2",
7576
"css-loader": "0.28.7",
7677
"cypress": "1.4.1",
@@ -86,7 +87,6 @@
8687
"semantic-action": "1.1.0",
8788
"simple-commit-message": "3.3.2",
8889
"standard": "10.0.3",
89-
"vue": "2.5.13",
9090
"vue-loader": "13.6.1",
9191
"vue-template-compiler": "2.5.13",
9292
"vuex": "3.0.1"
@@ -100,6 +100,7 @@
100100
},
101101
"dependencies": {
102102
"@cypress/webpack-preprocessor": "1.1.2",
103-
"common-tags": "1.6.0"
103+
"common-tags": "1.6.0",
104+
"vue": "2.5.13"
104105
}
105106
}

0 commit comments

Comments
 (0)