소스 검색

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.
Žiga Vidic 9 년 전
부모
커밋
e1940c0d15
3개의 변경된 파일27개의 추가작업 그리고 3개의 파일을 삭제
  1. 2 0
      package.json
  2. 6 2
      src/override.js
  3. 19 1
      test/unit/test.js

+ 2 - 0
package.json

@@ -53,6 +53,8 @@
     "mocha": "^2.3.4",
     "mocha": "^2.3.4",
     "rollup": "^0.25.4",
     "rollup": "^0.25.4",
     "rollup-plugin-babel": "^2.4.0",
     "rollup-plugin-babel": "^2.4.0",
+    "sinon": "^1.17.3",
+    "sinon-chai": "^2.8.0",
     "todomvc-app-css": "^2.0.3",
     "todomvc-app-css": "^2.0.3",
     "uglify-js": "^2.6.2",
     "uglify-js": "^2.6.2",
     "vue": "^1.0.17",
     "vue": "^1.0.17",

+ 6 - 2
src/override.js

@@ -51,7 +51,7 @@ export default function (Vue) {
       if (actions) {
       if (actions) {
         options.methods = options.methods || {}
         options.methods = options.methods || {}
         for (let key in actions) {
         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 {Store} store
    * @param {Function} action
    * @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 function vuexBoundAction (...args) {
       return action.call(this, store, ...args)
       return action.call(this, store, ...args)
     }
     }

+ 19 - 1
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 Vue from 'vue'
 import Vuex from '../../src'
 import Vuex from '../../src'
 import * as util from '../../src/util'
 import * as util from '../../src/util'
 
 
 Vue.use(Vuex)
 Vue.use(Vuex)
+chai.use(sinonChai)
 
 
 const TEST = 'TEST'
 const TEST = 'TEST'
 
 
@@ -404,4 +407,19 @@ describe('Vuex', () => {
     })
     })
     expect(store.state.a).to.equal(3)
     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()
+  })
 })
 })