|
@@ -22,7 +22,9 @@ Example testing a mutation using Mocha + Chai (you can use any framework/asserti
|
|
|
|
|
|
``` js
|
|
``` js
|
|
// mutations.js
|
|
// mutations.js
|
|
-export const INCREMENT = state => state.count++
|
|
|
|
|
|
+export const mutations = {
|
|
|
|
+ increment: state => state.count++
|
|
|
|
+}
|
|
```
|
|
```
|
|
|
|
|
|
``` js
|
|
``` js
|
|
@@ -31,14 +33,14 @@ import { expect } from 'chai'
|
|
import { mutations } from './store'
|
|
import { mutations } from './store'
|
|
|
|
|
|
// destructure assign mutations
|
|
// destructure assign mutations
|
|
-const { INCREMENT } = mutations
|
|
|
|
|
|
+const { increment } = mutations
|
|
|
|
|
|
describe('mutations', () => {
|
|
describe('mutations', () => {
|
|
it('INCREMENT', () => {
|
|
it('INCREMENT', () => {
|
|
// mock state
|
|
// mock state
|
|
const state = { count: 0 }
|
|
const state = { count: 0 }
|
|
// apply mutation
|
|
// apply mutation
|
|
- INCREMENT(state)
|
|
|
|
|
|
+ increment(state)
|
|
// assert result
|
|
// assert result
|
|
expect(state.count).to.equal(1)
|
|
expect(state.count).to.equal(1)
|
|
})
|
|
})
|
|
@@ -86,10 +88,11 @@ const actions = actionsInjector({
|
|
// helper for testing action with expected mutations
|
|
// helper for testing action with expected mutations
|
|
const testAction = (action, args, state, expectedMutations, done) => {
|
|
const testAction = (action, args, state, expectedMutations, done) => {
|
|
let count = 0
|
|
let count = 0
|
|
- // mock dispatch
|
|
|
|
- const dispatch = (name, ...payload) => {
|
|
|
|
|
|
+
|
|
|
|
+ // mock commit
|
|
|
|
+ const commit = (type, payload) => {
|
|
const mutation = expectedMutations[count]
|
|
const mutation = expectedMutations[count]
|
|
- expect(mutation.name).to.equal(name)
|
|
|
|
|
|
+ expect(mutation.type).to.equal(type)
|
|
if (payload) {
|
|
if (payload) {
|
|
expect(mutation.payload).to.deep.equal(payload)
|
|
expect(mutation.payload).to.deep.equal(payload)
|
|
}
|
|
}
|
|
@@ -98,8 +101,9 @@ const testAction = (action, args, state, expectedMutations, done) => {
|
|
done()
|
|
done()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
// call the action with mocked store and arguments
|
|
// call the action with mocked store and arguments
|
|
- action({dispatch, state}, ...args)
|
|
|
|
|
|
+ action({ commit, state }, ...args)
|
|
|
|
|
|
// check if no mutations should have been dispatched
|
|
// check if no mutations should have been dispatched
|
|
if (expectedMutations.length === 0) {
|
|
if (expectedMutations.length === 0) {
|
|
@@ -111,8 +115,8 @@ const testAction = (action, args, state, expectedMutations, done) => {
|
|
describe('actions', () => {
|
|
describe('actions', () => {
|
|
it('getAllProducts', done => {
|
|
it('getAllProducts', done => {
|
|
testAction(actions.getAllProducts, [], {}, [
|
|
testAction(actions.getAllProducts, [], {}, [
|
|
- { name: 'REQUEST_PRODUCTS' },
|
|
|
|
- { name: 'RECEIVE_PRODUCTS', payload: [ /* mocked response */ ] }
|
|
|
|
|
|
+ { type: 'REQUEST_PRODUCTS' },
|
|
|
|
+ { type: 'RECEIVE_PRODUCTS', payload: { /* mocked response */ } }
|
|
], done)
|
|
], done)
|
|
})
|
|
})
|
|
})
|
|
})
|
|
@@ -124,9 +128,10 @@ If your mutations and actions are written properly, the tests should have no dir
|
|
|
|
|
|
#### Running in Node
|
|
#### Running in Node
|
|
|
|
|
|
-Create the following webpack config:
|
|
|
|
|
|
+Create the following webpack config (together with proper [`.babelrc`](https://babeljs.io/docs/usage/babelrc/)):
|
|
|
|
|
|
``` js
|
|
``` js
|
|
|
|
+// webpack.config.js
|
|
module.exports = {
|
|
module.exports = {
|
|
entry: './test.js',
|
|
entry: './test.js',
|
|
output: {
|
|
output: {
|
|
@@ -141,9 +146,6 @@ module.exports = {
|
|
exclude: /node_modules/
|
|
exclude: /node_modules/
|
|
}
|
|
}
|
|
]
|
|
]
|
|
- },
|
|
|
|
- babel: {
|
|
|
|
- presets: ['es2015']
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
```
|
|
@@ -164,4 +166,4 @@ mocha test-bundle.js
|
|
|
|
|
|
#### Running in Browser with Karma + karma-webpack
|
|
#### Running in Browser with Karma + karma-webpack
|
|
|
|
|
|
-Consult the setup in [vue-loader documentation](http://vuejs.github.io/vue-loader/workflow/testing.html).
|
|
|
|
|
|
+Consult the setup in [vue-loader documentation](http://vue-loader.vuejs.org/en/workflow/testing.html).
|