diff --git a/package.json b/package.json index ddfe02d89..1f3c56fc6 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,8 @@ "mocha": "^2.3.4", "rollup": "^0.25.4", "rollup-plugin-babel": "^2.4.0", + "sinon": "^1.17.3", + "sinon-chai": "^2.8.0", "todomvc-app-css": "^2.0.3", "uglify-js": "^2.6.2", "vue": "^1.0.17", diff --git a/src/override.js b/src/override.js index 470146b83..fd408f8a5 100644 --- a/src/override.js +++ b/src/override.js @@ -51,7 +51,7 @@ export default function (Vue) { if (actions) { options.methods = options.methods || {} for (let key in actions) { - options.methods[key] = makeBoundAction(this.$store, actions[key]) + options.methods[key] = makeBoundAction(this.$store, actions[key], key) } } } @@ -126,9 +126,13 @@ export default function (Vue) { * * @param {Store} store * @param {Function} action + * @param {String} key */ - function makeBoundAction (store, action) { + function makeBoundAction (store, action, key) { + if (typeof action !== 'function') { + console.warn(`[vuex] Action bound to key 'vuex.actions.${key}' is not a function.`) + } return function vuexBoundAction (...args) { return action.call(this, store, ...args) } diff --git a/test/unit/test.js b/test/unit/test.js index e5132e401..77d9e7cf8 100644 --- a/test/unit/test.js +++ b/test/unit/test.js @@ -1,9 +1,12 @@ -import { expect } from 'chai' +import chai, { expect } from 'chai' +import sinonChai from 'sinon-chai' +import sinon from 'sinon' import Vue from 'vue' import Vuex from '../../src' import * as util from '../../src/util' Vue.use(Vuex) +chai.use(sinonChai) const TEST = 'TEST' @@ -404,4 +407,19 @@ describe('Vuex', () => { }) expect(store.state.a).to.equal(3) }) + + it('console.warn when action is not a function', function () { + sinon.spy(console, 'warn') + + new Vue({ + vuex: { + actions: { + test: undefined + } + } + }) + + expect(console.warn).to.have.been.calledWith('[vuex] Action bound to key \'vuex.actions.test\' is not a function.') + console.warn.restore() + }) })