Evan You 9 anos atrás
pai
commit
1668bd601d
3 arquivos alterados com 115 adições e 122 exclusões
  1. 6 0
      src/override.js
  2. 0 18
      src/util.js
  3. 109 104
      test/test.js

+ 6 - 0
src/override.js

@@ -14,6 +14,12 @@ export default function (Vue) {
     // vuex option handling
     const vuex = options.vuex || componentOptions.vuex
     if (vuex) {
+      if (!this.$store) {
+        console.warn(
+          '[vuex] store not injected. make sure to ' +
+          'provide the store option in your root component.'
+        )
+      }
       const { state, actions } = vuex
       // state
       if (state) {

+ 0 - 18
src/util.js

@@ -1,21 +1,3 @@
-/**
- * Create a actual callable action function.
- *
- * @param {String|Function} action
- * @param {Vuex} store
- * @return {Function} [description]
- */
-
-export function createAction (action, store) {
-  if (typeof action === 'string') {
-    // simple action string shorthand
-    return (...payload) => store.dispatch(action, ...payload)
-  } else if (typeof action === 'function') {
-    // normal action
-    return (...payload) => action(store, ...payload)
-  }
-}
-
 /**
  * Merge an array of objects into one.
  *

+ 109 - 104
test/test.js

@@ -23,152 +23,157 @@ describe('Vuex', () => {
     expect(store.state.a).to.equal(3)
   })
 
-  it('simple action', function () {
+  it('injecting state and action to components', function () {
     const store = new Vuex.Store({
       state: {
         a: 1
       },
-      actions: {
-        test: TEST
-      },
-      getters: {
-        getA (state) {
-          return state.a
-        }
-      },
       mutations: {
         [TEST] (state, n) {
           state.a += n
         }
       }
     })
-    store.actions.test(2)
+    const vm = new Vue({
+      store,
+      vuex: {
+        state: {
+          a: state => state.a
+        },
+        actions: {
+          test: ({ dispatch }, n) => dispatch(TEST, n)
+        }
+      }
+    })
+    vm.test(2)
+    expect(vm.a).to.equal(3)
     expect(store.state.a).to.equal(3)
-    expect(store.getters.getA()).to.equal(3)
   })
 
-  it('async action', function (done) {
-    const TEST = 'TEST'
+  it('modules', function () {
+    const mutations = {
+      [TEST] (state, n) {
+        state.a += n
+      }
+    }
     const store = new Vuex.Store({
       state: {
-        a: 1,
-        timeout: 10
-      },
-      actions: {
-        test: ({ dispatch, state }, n) => {
-          setTimeout(() => {
-            dispatch(TEST, n)
-          }, state.timeout)
-        }
+        a: 1
       },
-      mutations: {
-        [TEST] (state, n) {
-          state.a += n
+      mutations,
+      modules: {
+        one: {
+          state: { a: 2 },
+          mutations
+        },
+        two: {
+          state: { a: 3 },
+          mutations
         }
       }
     })
-    store.actions.test(2)
-    setTimeout(() => {
-      expect(store.state.a).to.equal(3)
-      done()
-    }, store.state.timeout)
+    store.dispatch(TEST, 1)
+    expect(store.state.a).to.equal(2)
+    expect(store.state.one.a).to.equal(3)
+    expect(store.state.two.a).to.equal(4)
   })
 
-  it('array option syntax', function () {
-    const TEST2 = 'TEST2'
+  it('hot reload', function () {
+    const mutations = {
+      [TEST] (state, n) {
+        state.a += n
+      }
+    }
     const store = new Vuex.Store({
       state: {
-        a: 1,
-        b: 1,
-        c: 1
+        a: 1
       },
-      actions: [{ test: TEST }, { test2: TEST2 }],
-      mutations: [
-        {
-          [TEST] (state, n) {
-            state.a += n
-          }
+      mutations,
+      modules: {
+        one: {
+          state: { a: 2 },
+          mutations
         },
-        // allow multiple handlers for the same mutation type
-        {
-          [TEST] (state, n) {
-            state.b += n
-          },
-          [TEST2] (state, n) {
-            state.c += n
-          }
-        }
-      ],
-      getters: [
-        {
-          getA (state) {
-            return state.a
-          }
-        },
-        {
-          getB (state) {
-            return state.b
-          },
-
-          getC (state) {
-            return state.c
-          }
+        two: {
+          state: { a: 3 },
+          mutations
         }
-      ]
+      }
     })
-    store.actions.test(2)
-    expect(store.state.a).to.equal(3)
-    expect(store.state.b).to.equal(3)
-    expect(store.state.c).to.equal(1)
-    expect(store.getters.getA()).to.equal(3)
-    expect(store.getters.getB()).to.equal(3)
-    expect(store.getters.getC()).to.equal(1)
-    store.actions.test2(2)
-    expect(store.state.c).to.equal(3)
-  })
+    store.dispatch(TEST, 1)
+    expect(store.state.a).to.equal(2)
+    expect(store.state.one.a).to.equal(3)
+    expect(store.state.two.a).to.equal(4)
 
-  it('hot reload', function () {
-    const store = new Vuex.Store({
-      state: {
-        a: 1,
-        b: 2
-      },
-      actions: {
-        test: TEST
-      },
+    // hot reload only root mutations
+    store.hotUpdate({
       mutations: {
         [TEST] (state, n) {
-          state.a += n
+          state.a = n
         }
-      },
-      getters: {
-        getA (state) {
-          return state.b
+      }
+    })
+    store.dispatch(TEST, 1)
+    expect(store.state.a).to.equal(1) // only root mutation updated
+    expect(store.state.one.a).to.equal(4)
+    expect(store.state.two.a).to.equal(5)
+
+    // hot reload modules
+    store.hotUpdate({
+      modules: {
+        one: {
+          state: { a: 234 },
+          mutations: {
+            [TEST] (state, n) {
+              state.a += n
+            }
+          }
+        },
+        two: {
+          state: { a: 345 },
+          mutations: {
+            [TEST] (state, n) {
+              state.a -= n
+            }
+          }
         }
       }
     })
-    const test = store.actions.test
-    test(2)
-    expect(store.state.a).to.equal(3)
-    expect(store.getters.getA()).to.equal(2)
+    store.dispatch(TEST, 2)
+    expect(store.state.a).to.equal(2)
+    expect(store.state.one.a).to.equal(6) // should not reload initial state
+    expect(store.state.two.a).to.equal(3) // should not reload initial state
+
+    // hot reload all
     store.hotUpdate({
-      actions: {
-        test: ({ dispatch }, n) => dispatch(TEST, n + 1)
-      },
       mutations: {
         [TEST] (state, n) {
-          state.a = n
+          state.a -= n
         }
       },
-      getters: {
-        getA (state) {
-          return state.a
+      modules: {
+        one: {
+          state: { a: 234 },
+          mutations: {
+            [TEST] (state, n) {
+              state.a = n
+            }
+          }
+        },
+        two: {
+          state: { a: 345 },
+          mutations: {
+            [TEST] (state, n) {
+              state.a = n
+            }
+          }
         }
       }
     })
-    test(999)
-    expect(store.state.a).to.equal(1000)
-    expect(store.getters.getA()).to.equal(1000)
+    store.dispatch(TEST, 3)
+    expect(store.state.a).to.equal(-1)
+    expect(store.state.one.a).to.equal(3)
+    expect(store.state.two.a).to.equal(3)
   })
 
   it('middleware', function () {