Skip to content

Commit e5ca2d5

Browse files
EdyHartonoktsn
authored andcommitted
fix: Should vuex mapState print error message #1093 (#1297)
1 parent 4003382 commit e5ca2d5

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/helpers.js

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isObject } from './util'
2+
13
/**
24
* Reduce the code which written in Vue.js for getting the state.
35
* @param {String} [namespace] - Module's namespace
@@ -6,6 +8,9 @@
68
*/
79
export const mapState = normalizeNamespace((namespace, states) => {
810
const res = {}
11+
if (process.env.NODE_ENV !== 'production' && !isValidMap(states)) {
12+
console.error('[vuex] states parameter must be either an Array/Object')
13+
}
914
normalizeMap(states).forEach(({ key, val }) => {
1015
res[key] = function mappedState () {
1116
let state = this.$store.state
@@ -36,6 +41,9 @@ export const mapState = normalizeNamespace((namespace, states) => {
3641
*/
3742
export const mapMutations = normalizeNamespace((namespace, mutations) => {
3843
const res = {}
44+
if (process.env.NODE_ENV !== 'production' && !isValidMap(mutations)) {
45+
console.error('[vuex] mutations parameter must be either an Array/Object')
46+
}
3947
normalizeMap(mutations).forEach(({ key, val }) => {
4048
res[key] = function mappedMutation (...args) {
4149
// Get the commit method from store
@@ -63,6 +71,9 @@ export const mapMutations = normalizeNamespace((namespace, mutations) => {
6371
*/
6472
export const mapGetters = normalizeNamespace((namespace, getters) => {
6573
const res = {}
74+
if (process.env.NODE_ENV !== 'production' && !isValidMap(getters)) {
75+
console.error('[vuex] getters parameter must be either an Array/Object')
76+
}
6677
normalizeMap(getters).forEach(({ key, val }) => {
6778
// The namespace has been mutated by normalizeNamespace
6879
val = namespace + val
@@ -90,6 +101,9 @@ export const mapGetters = normalizeNamespace((namespace, getters) => {
90101
*/
91102
export const mapActions = normalizeNamespace((namespace, actions) => {
92103
const res = {}
104+
if (process.env.NODE_ENV !== 'production' && !isValidMap(actions)) {
105+
console.error('[vuex] actions parameter must be either an Array/Object')
106+
}
93107
normalizeMap(actions).forEach(({ key, val }) => {
94108
res[key] = function mappedAction (...args) {
95109
// get dispatch function from store
@@ -129,11 +143,23 @@ export const createNamespacedHelpers = (namespace) => ({
129143
* @return {Object}
130144
*/
131145
function normalizeMap (map) {
146+
if (!isValidMap(map)) {
147+
return []
148+
}
132149
return Array.isArray(map)
133150
? map.map(key => ({ key, val: key }))
134151
: Object.keys(map).map(key => ({ key, val: map[key] }))
135152
}
136153

154+
/**
155+
* Validate whether given map is valid or not
156+
* @param {*} map
157+
* @return {Boolean}
158+
*/
159+
function isValidMap (map) {
160+
return Array.isArray(map) || isObject(map)
161+
}
162+
137163
/**
138164
* Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
139165
* @param {Function} fn

test/unit/helpers.spec.js

+82
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ describe('Helpers', () => {
9494
expect(vm.value.b).toBeUndefined()
9595
})
9696

97+
it('mapState (with undefined states)', () => {
98+
const store = new Vuex.Store({
99+
modules: {
100+
foo: {
101+
namespaced: true,
102+
state: { a: 1 }
103+
}
104+
}
105+
})
106+
const vm = new Vue({
107+
store,
108+
computed: mapState('foo')
109+
})
110+
expect(vm.a).toBeUndefined()
111+
})
112+
97113
it('mapMutations (array)', () => {
98114
const store = new Vuex.Store({
99115
state: { count: 0 },
@@ -206,6 +222,27 @@ describe('Helpers', () => {
206222
expect(store.state.foo.count).toBe(43)
207223
})
208224

225+
it('mapMutations (with undefined mutations)', () => {
226+
const store = new Vuex.Store({
227+
modules: {
228+
foo: {
229+
namespaced: true,
230+
state: { count: 0 },
231+
mutations: {
232+
inc: state => state.count++,
233+
dec: state => state.count--
234+
}
235+
}
236+
}
237+
})
238+
const vm = new Vue({
239+
store,
240+
methods: mapMutations('foo')
241+
})
242+
expect(vm.inc).toBeUndefined()
243+
expect(vm.dec).toBeUndefined()
244+
})
245+
209246
it('mapGetters (array)', () => {
210247
const store = new Vuex.Store({
211248
state: { count: 0 },
@@ -351,6 +388,31 @@ describe('Helpers', () => {
351388
expect(vm.count).toBe(9)
352389
})
353390

391+
it('mapGetters (with undefined getters)', () => {
392+
const store = new Vuex.Store({
393+
modules: {
394+
foo: {
395+
namespaced: true,
396+
state: { count: 0 },
397+
mutations: {
398+
inc: state => state.count++,
399+
dec: state => state.count--
400+
},
401+
getters: {
402+
hasAny: ({ count }) => count > 0,
403+
negative: ({ count }) => count < 0
404+
}
405+
}
406+
}
407+
})
408+
const vm = new Vue({
409+
store,
410+
computed: mapGetters('foo')
411+
})
412+
expect(vm.a).toBeUndefined()
413+
expect(vm.b).toBeUndefined()
414+
})
415+
354416
it('mapActions (array)', () => {
355417
const a = jasmine.createSpy()
356418
const b = jasmine.createSpy()
@@ -461,6 +523,26 @@ describe('Helpers', () => {
461523
expect(a.calls.argsFor(0)[1]).toBe('foobar')
462524
})
463525

526+
it('mapActions (with undefined actions)', () => {
527+
const a = jasmine.createSpy()
528+
const store = new Vuex.Store({
529+
modules: {
530+
foo: {
531+
namespaced: true,
532+
actions: {
533+
a
534+
}
535+
}
536+
}
537+
})
538+
const vm = new Vue({
539+
store,
540+
methods: mapActions('foo/')
541+
})
542+
expect(vm.a).toBeUndefined()
543+
expect(a).not.toHaveBeenCalled()
544+
})
545+
464546
it('createNamespacedHelpers', () => {
465547
const actionA = jasmine.createSpy()
466548
const actionB = jasmine.createSpy()

0 commit comments

Comments
 (0)