Skip to content

Commit

Permalink
[v2.0] Assert dispatched/committed type (vuejs#554)
Browse files Browse the repository at this point in the history
* assert dispatched/committed type

* add comments
  • Loading branch information
ktsn authored and yyx990803 committed Feb 6, 2017
1 parent c716f69 commit 5954b4b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ function unifyObjectStyle (type, payload, options) {
payload = type
type = type.type
}

assert(typeof type === 'string', `Expects string as the type, but found ${typeof type}.`)

return { type, payload, options }
}

Expand Down
43 changes: 43 additions & 0 deletions test/unit/store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ describe('Store', () => {
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', () => {
const store = new Vuex.Store({
state: {
Expand Down Expand Up @@ -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', () => {
const store = new Vuex.Store({
state: {
Expand Down

0 comments on commit 5954b4b

Please sign in to comment.