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

Full Vuex support with enhanced Counter.vue example and test spec #15

Merged
merged 8 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ See examples below for details.
See [cypress/integration/options-spec.js](cypress/integration/options-spec.js)
for examples of options.

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

```js
const options = {
vue: 'https://unpkg.com/vue'
mountId: 'rootApp' // div#rootApp
}
beforeEach(mountVue(/* my Vue code */, options))
```
Expand All @@ -71,12 +69,25 @@ beforeEach(mountVue(/* my Vue code */, options))
place to load additional libraries, polyfills and styles.

```js
const vue = '../node_modules/vue/dist/vue.js'
const polyfill = '../node_modules/mypolyfill/dist/polyfill.js'
const options = {
html: `<div id="app"></div><script src="${vue}"></script>`
html: `<div id="app"></div><script src="${polyfill}"></script>`
}
beforeEach(mountVue(/* my Vue code */, options))
```

* `vue` **[DEPRECATED]** - path or URL to the Vue library to load. By default, will
try to load `../node_modules/vue/dist/vue.js`, but you can pass your
own path or URL.

```js
const options = {
vue: 'https://unpkg.com/vue'
}
beforeEach(mountVue(/* my Vue code */, options))
```
> #### Deprecation Warning
> `vue` option has been deprecated. `node_modules/vue/dist/vue` is always used.

### Global Vue extensions

Expand Down
5 changes: 5 additions & 0 deletions components/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"transform-object-rest-spread"
]
}
29 changes: 18 additions & 11 deletions components/Counter.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
<template>
<div>
Clicked: {{ $store.state.count }} times, count is {{ $store.getters.evenOrOdd }}.
Clicked: {{ count }} times, count is {{ evenOrOdd }}.<br>
<button @click="increment">+</button>
<button @click="decrement">-</button>
<button @click="incrementIfOdd">Increment if odd</button>
<button @click="incrementAsync">Increment async</button>
<br><br>
<span>Set Count: </span>
<input type="number" :value="count" @input="set($event.target.value || 0)">
</div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
export default {
computed: mapGetters([
'evenOrOdd'
]),
methods: mapActions([
'increment',
'decrement',
'incrementIfOdd',
'incrementAsync'
])
computed: {
...mapState(['count']),
...mapGetters(['evenOrOdd'])
},
methods: {
...mapMutations(['set']),
...mapActions([
'increment',
'decrement',
'incrementIfOdd',
'incrementAsync'
])
}
}
</script>
3 changes: 3 additions & 0 deletions components/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const state = {
// mutations must be synchronous and can be recorded by plugins
// for debugging purposes.
const mutations = {
set (state, value) {
state.count = value
},
increment (state) {
state.count++
},
Expand Down
2 changes: 1 addition & 1 deletion cypress.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"viewportWidth": 300,
"viewportHeight": 100,
"viewportHeight": 120,
"videoRecording": false,
"projectId": "134ej7"
}
37 changes: 28 additions & 9 deletions cypress/integration/counter-vuex-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import mountVue from '../..'

/* eslint-env mocha */
describe('Vuex Counter', () => {

// configure component
const extensions = {
plugins: [Vuex],
components: {
counter: Counter
},
Counter
}
}

// define component template
const template = '<counter />'
beforeEach(mountVue({template, store}, {extensions}))

const getCount = () =>
Cypress.vue.$store.state.count
// define count get and set helpers
const getCount = () => Cypress.vue.$store.state.count
const setCount = value => Cypress.vue.$store.commit('set', value)

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

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

it('increments the counter if count is odd', () => {
setCount(3)
setCount(3) // start with an odd number
cy.contains('odd')
cy.contains('button', 'Increment if odd').click()
cy.contains('button', 'Increment if odd').as('btn').click()
cy.contains('even')
cy.get('@btn').click()
cy.contains('even')
})

it('asynchronously increments counter', () => {
const count = getCount()
// increment mutation is delayed by 1 second
// Cypress waits 4 seconds by default
cy.contains('button', 'Increment async').click()
cy.contains(`${count + 1} times`)
})

it('count is zero when input is cleared', () => {
cy.get('input').type(`{selectall}{backspace}`)
cy.contains('0 times')
}),

it('set count via input field', () => {
const count = 42
cy.get('input').type(`{selectall}{backspace}${count}`)
cy.contains(`${count} times`)
})
})
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"generateNotes": "github-post-release"
},
"devDependencies": {
"babel-plugin-transform-object-rest-spread": "6.26.0",
"ban-sensitive-files": "1.9.2",
"css-loader": "0.28.7",
"cypress": "1.4.1",
Expand All @@ -86,7 +87,6 @@
"semantic-action": "1.1.0",
"simple-commit-message": "3.3.2",
"standard": "10.0.3",
"vue": "2.5.13",
"vue-loader": "13.6.1",
"vue-template-compiler": "2.5.13",
"vuex": "3.0.1"
Expand All @@ -100,6 +100,7 @@
},
"dependencies": {
"@cypress/webpack-preprocessor": "1.1.2",
"common-tags": "1.6.0"
"common-tags": "1.6.0",
"vue": "2.5.13"
}
}
Loading