Skip to content

Commit e1940c0

Browse files
zigomiryyx990803
authored andcommitted
Better user error. (#134)
* Better user error. * Simplify function check and use console.warn. * Warn user when trying to set non function as vuex action.
1 parent bfa81db commit e1940c0

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
"mocha": "^2.3.4",
5454
"rollup": "^0.25.4",
5555
"rollup-plugin-babel": "^2.4.0",
56+
"sinon": "^1.17.3",
57+
"sinon-chai": "^2.8.0",
5658
"todomvc-app-css": "^2.0.3",
5759
"uglify-js": "^2.6.2",
5860
"vue": "^1.0.17",

src/override.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default function (Vue) {
5151
if (actions) {
5252
options.methods = options.methods || {}
5353
for (let key in actions) {
54-
options.methods[key] = makeBoundAction(this.$store, actions[key])
54+
options.methods[key] = makeBoundAction(this.$store, actions[key], key)
5555
}
5656
}
5757
}
@@ -126,9 +126,13 @@ export default function (Vue) {
126126
*
127127
* @param {Store} store
128128
* @param {Function} action
129+
* @param {String} key
129130
*/
130131

131-
function makeBoundAction (store, action) {
132+
function makeBoundAction (store, action, key) {
133+
if (typeof action !== 'function') {
134+
console.warn(`[vuex] Action bound to key 'vuex.actions.${key}' is not a function.`)
135+
}
132136
return function vuexBoundAction (...args) {
133137
return action.call(this, store, ...args)
134138
}

test/unit/test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { expect } from 'chai'
1+
import chai, { expect } from 'chai'
2+
import sinonChai from 'sinon-chai'
3+
import sinon from 'sinon'
24
import Vue from 'vue'
35
import Vuex from '../../src'
46
import * as util from '../../src/util'
57

68
Vue.use(Vuex)
9+
chai.use(sinonChai)
710

811
const TEST = 'TEST'
912

@@ -404,4 +407,19 @@ describe('Vuex', () => {
404407
})
405408
expect(store.state.a).to.equal(3)
406409
})
410+
411+
it('console.warn when action is not a function', function () {
412+
sinon.spy(console, 'warn')
413+
414+
new Vue({
415+
vuex: {
416+
actions: {
417+
test: undefined
418+
}
419+
}
420+
})
421+
422+
expect(console.warn).to.have.been.calledWith('[vuex] Action bound to key \'vuex.actions.test\' is not a function.')
423+
console.warn.restore()
424+
})
407425
})

0 commit comments

Comments
 (0)