1
0
Эх сурвалжийг харах

[v2.0] Assert dispatched/committed type (#554)

* assert dispatched/committed type

* add comments
katashin 8 жил өмнө
parent
commit
5954b4b235

+ 3 - 0
src/index.js

@@ -403,6 +403,9 @@ function unifyObjectStyle (type, payload, options) {
     payload = type
     payload = type
     type = type.type
     type = type.type
   }
   }
+
+  assert(typeof type === 'string', `Expects string as the type, but found ${typeof type}.`)
+
   return { type, payload, options }
   return { type, payload, options }
 }
 }
 
 

+ 43 - 0
test/unit/store.spec.js

@@ -37,6 +37,25 @@ describe('Store', () => {
     expect(store.state.a).toBe(3)
     expect(store.state.a).toBe(3)
   })
   })
 
 
+  it('asserts committed type', () => {
+    const store = new Vuex.Store({
+      state: {
+        a: 1
+      },
+      mutations: {
+        // Maybe registered with undefined type accidentally
+        // if the user has typo in a constant type
+        undefined (state, n) {
+          state.a += n
+        }
+      }
+    })
+    expect(() => {
+      store.commit(undefined, 2)
+    }).toThrowError(/Expects string as the type, but found undefined/)
+    expect(store.state.a).toBe(1)
+  })
+
   it('dispatching actions, sync', () => {
   it('dispatching actions, sync', () => {
     const store = new Vuex.Store({
     const store = new Vuex.Store({
       state: {
       state: {
@@ -166,6 +185,30 @@ describe('Store', () => {
       })
       })
   })
   })
 
 
+  it('asserts dispatched type', () => {
+    const store = new Vuex.Store({
+      state: {
+        a: 1
+      },
+      mutations: {
+        [TEST] (state, n) {
+          state.a += n
+        }
+      },
+      actions: {
+        // Maybe registered with undefined type accidentally
+        // if the user has typo in a constant type
+        undefined ({ commit }, n) {
+          commit(TEST, n)
+        }
+      }
+    })
+    expect(() => {
+      store.dispatch(undefined, 2)
+    }).toThrowError(/Expects string as the type, but found undefined/)
+    expect(store.state.a).toBe(1)
+  })
+
   it('getters', () => {
   it('getters', () => {
     const store = new Vuex.Store({
     const store = new Vuex.Store({
       state: {
       state: {