Skip to content

Commit 5954b4b

Browse files
ktsnyyx990803
authored andcommitted
[v2.0] Assert dispatched/committed type (vuejs#554)
* assert dispatched/committed type * add comments
1 parent c716f69 commit 5954b4b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ function unifyObjectStyle (type, payload, options) {
403403
payload = type
404404
type = type.type
405405
}
406+
407+
assert(typeof type === 'string', `Expects string as the type, but found ${typeof type}.`)
408+
406409
return { type, payload, options }
407410
}
408411

test/unit/store.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ describe('Store', () => {
3737
expect(store.state.a).toBe(3)
3838
})
3939

40+
it('asserts committed type', () => {
41+
const store = new Vuex.Store({
42+
state: {
43+
a: 1
44+
},
45+
mutations: {
46+
// Maybe registered with undefined type accidentally
47+
// if the user has typo in a constant type
48+
undefined (state, n) {
49+
state.a += n
50+
}
51+
}
52+
})
53+
expect(() => {
54+
store.commit(undefined, 2)
55+
}).toThrowError(/Expects string as the type, but found undefined/)
56+
expect(store.state.a).toBe(1)
57+
})
58+
4059
it('dispatching actions, sync', () => {
4160
const store = new Vuex.Store({
4261
state: {
@@ -166,6 +185,30 @@ describe('Store', () => {
166185
})
167186
})
168187

188+
it('asserts dispatched type', () => {
189+
const store = new Vuex.Store({
190+
state: {
191+
a: 1
192+
},
193+
mutations: {
194+
[TEST] (state, n) {
195+
state.a += n
196+
}
197+
},
198+
actions: {
199+
// Maybe registered with undefined type accidentally
200+
// if the user has typo in a constant type
201+
undefined ({ commit }, n) {
202+
commit(TEST, n)
203+
}
204+
}
205+
})
206+
expect(() => {
207+
store.dispatch(undefined, 2)
208+
}).toThrowError(/Expects string as the type, but found undefined/)
209+
expect(store.state.a).toBe(1)
210+
})
211+
169212
it('getters', () => {
170213
const store = new Vuex.Store({
171214
state: {

0 commit comments

Comments
 (0)