From 8ba2f570e69f6759074ccf0c6b14898b8d8f4cd5 Mon Sep 17 00:00:00 2001 From: zigomir Date: Sun, 3 Apr 2016 20:14:27 -0700 Subject: [PATCH 1/3] Better user error. --- src/override.js | 11 ++++++++--- test/unit/test.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/override.js b/src/override.js index 470146b83..03af7e0eb 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,11 +126,16 @@ export default function (Vue) { * * @param {Store} store * @param {Function} action + * @param {String} key */ - function makeBoundAction (store, action) { + function makeBoundAction (store, action, key) { return function vuexBoundAction (...args) { - return action.call(this, store, ...args) + if (Function.prototype.isPrototypeOf(action)) { + return action.call(this, store, ...args) + } + + throw new Error(`Action bound to key 'vuex.actions.${key}' is not a function.`) } } diff --git a/test/unit/test.js b/test/unit/test.js index e5132e401..3c51364b8 100644 --- a/test/unit/test.js +++ b/test/unit/test.js @@ -404,4 +404,18 @@ describe('Vuex', () => { }) expect(store.state.a).to.equal(3) }) + + it('throws when action is not a function', function () { + const vm = new Vue({ + vuex: { + actions: { + test: undefined + } + } + }) + + expect(() => { + vm.test(2) + }).to.throw(/Action bound to key 'vuex.actions.test' is not a function./) + }) }) From 45179e7597f8d1444eac956b259bf910141f51a7 Mon Sep 17 00:00:00 2001 From: zigomir Date: Wed, 6 Apr 2016 20:46:26 -0700 Subject: [PATCH 2/3] Simplify function check and use console.warn. --- package.json | 2 ++ src/override.js | 4 ++-- test/unit/test.js | 14 +++++++++----- 3 files changed, 13 insertions(+), 7 deletions(-) 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 03af7e0eb..2f943f8a6 100644 --- a/src/override.js +++ b/src/override.js @@ -131,11 +131,11 @@ export default function (Vue) { function makeBoundAction (store, action, key) { return function vuexBoundAction (...args) { - if (Function.prototype.isPrototypeOf(action)) { + if (typeof action === 'function') { return action.call(this, store, ...args) } - throw new Error(`Action bound to key 'vuex.actions.${key}' is not a function.`) + console.warn(`[vuex] Action bound to key 'vuex.actions.${key}' is not a function.`) } } diff --git a/test/unit/test.js b/test/unit/test.js index 3c51364b8..686f138df 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' @@ -405,7 +408,7 @@ describe('Vuex', () => { expect(store.state.a).to.equal(3) }) - it('throws when action is not a function', function () { + it('console.warn when actions is not a function', function () { const vm = new Vue({ vuex: { actions: { @@ -414,8 +417,9 @@ describe('Vuex', () => { } }) - expect(() => { - vm.test(2) - }).to.throw(/Action bound to key 'vuex.actions.test' is not a function./) + sinon.spy(console, 'warn') + vm.test(2) + expect(console.warn).to.have.been.calledWith(`[vuex] Action bound to key 'vuex.actions.test' is not a function.`) + console.warn.restore() }) }) From 0ac79b6331575a94be7eb1aa01c12352df2e1462 Mon Sep 17 00:00:00 2001 From: zigomir Date: Fri, 8 Apr 2016 10:53:53 -0700 Subject: [PATCH 3/3] Warn user when trying to set non function as vuex action. --- src/override.js | 9 ++++----- test/unit/test.js | 10 +++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/override.js b/src/override.js index 2f943f8a6..fd408f8a5 100644 --- a/src/override.js +++ b/src/override.js @@ -130,13 +130,12 @@ export default function (Vue) { */ function makeBoundAction (store, action, key) { - return function vuexBoundAction (...args) { - if (typeof action === 'function') { - return action.call(this, store, ...args) - } - + 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) + } } // option merging diff --git a/test/unit/test.js b/test/unit/test.js index 686f138df..77d9e7cf8 100644 --- a/test/unit/test.js +++ b/test/unit/test.js @@ -408,8 +408,10 @@ describe('Vuex', () => { expect(store.state.a).to.equal(3) }) - it('console.warn when actions is not a function', function () { - const vm = new Vue({ + it('console.warn when action is not a function', function () { + sinon.spy(console, 'warn') + + new Vue({ vuex: { actions: { test: undefined @@ -417,9 +419,7 @@ describe('Vuex', () => { } }) - sinon.spy(console, 'warn') - vm.test(2) - expect(console.warn).to.have.been.calledWith(`[vuex] Action bound to key 'vuex.actions.test' is not a function.`) + expect(console.warn).to.have.been.calledWith('[vuex] Action bound to key \'vuex.actions.test\' is not a function.') console.warn.restore() }) })