Skip to content

Commit 44d4a72

Browse files
riophaeyyx990803
authored andcommitted
add createNamespacedHelpers() to help dealing with namespaced modules (#800)
1 parent 752dbc6 commit 44d4a72

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

src/helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ export const mapActions = normalizeNamespace((namespace, actions) => {
7070
return res
7171
})
7272

73+
export const createNamespacedHelpers = (namespace) => ({
74+
mapState: mapState.bind(null, namespace),
75+
mapGetters: mapGetters.bind(null, namespace),
76+
mapMutations: mapMutations.bind(null, namespace),
77+
mapActions: mapActions.bind(null, namespace)
78+
})
79+
7380
function normalizeMap (map) {
7481
return Array.isArray(map)
7582
? map.map(key => ({ key, val: key }))

src/index.esm.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Store, install } from './store'
2-
import { mapState, mapMutations, mapGetters, mapActions } from './helpers'
2+
import { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from './helpers'
33

44
export default {
55
Store,
@@ -8,13 +8,15 @@ export default {
88
mapState,
99
mapMutations,
1010
mapGetters,
11-
mapActions
11+
mapActions,
12+
createNamespacedHelpers
1213
}
1314

1415
export {
1516
Store,
1617
mapState,
1718
mapMutations,
1819
mapGetters,
19-
mapActions
20+
mapActions,
21+
createNamespacedHelpers
2022
}

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Store, install } from './store'
2-
import { mapState, mapMutations, mapGetters, mapActions } from './helpers'
2+
import { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from './helpers'
33

44
export default {
55
Store,
@@ -8,5 +8,6 @@ export default {
88
mapState,
99
mapMutations,
1010
mapGetters,
11-
mapActions
11+
mapActions,
12+
createNamespacedHelpers
1213
}

test/unit/helpers.spec.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from 'vue/dist/vue.common.js'
2-
import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../dist/vuex.common.js'
2+
import Vuex, { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from '../../dist/vuex.common.js'
33

44
describe('Helpers', () => {
55
it('mapState (array)', () => {
@@ -321,4 +321,61 @@ describe('Helpers', () => {
321321
vm.bar()
322322
expect(b).toHaveBeenCalled()
323323
})
324+
325+
it('createNamespacedHelpers', () => {
326+
const actionA = jasmine.createSpy()
327+
const actionB = jasmine.createSpy()
328+
const store = new Vuex.Store({
329+
modules: {
330+
foo: {
331+
namespaced: true,
332+
state: { count: 0 },
333+
getters: {
334+
isEven: state => state.count % 2 === 0
335+
},
336+
mutations: {
337+
inc: state => state.count++,
338+
dec: state => state.count--
339+
},
340+
actions: {
341+
actionA,
342+
actionB
343+
}
344+
}
345+
}
346+
})
347+
const {
348+
mapState,
349+
mapGetters,
350+
mapMutations,
351+
mapActions
352+
} = createNamespacedHelpers('foo/')
353+
const vm = new Vue({
354+
store,
355+
computed: {
356+
...mapState(['count']),
357+
...mapGetters(['isEven'])
358+
},
359+
methods: {
360+
...mapMutations(['inc', 'dec']),
361+
...mapActions(['actionA', 'actionB'])
362+
}
363+
})
364+
expect(vm.count).toBe(0)
365+
expect(vm.isEven).toBe(true)
366+
store.state.foo.count++
367+
expect(vm.count).toBe(1)
368+
expect(vm.isEven).toBe(false)
369+
vm.inc()
370+
expect(store.state.foo.count).toBe(2)
371+
expect(store.getters['foo/isEven']).toBe(true)
372+
vm.dec()
373+
expect(store.state.foo.count).toBe(1)
374+
expect(store.getters['foo/isEven']).toBe(false)
375+
vm.actionA()
376+
expect(actionA).toHaveBeenCalled()
377+
expect(actionB).not.toHaveBeenCalled()
378+
vm.actionB()
379+
expect(actionB).toHaveBeenCalled()
380+
})
324381
})

0 commit comments

Comments
 (0)