Skip to content

Commit

Permalink
add createNamespacedHelpers() to help dealing with namespaced modul…
Browse files Browse the repository at this point in the history
…es (vuejs#800)
  • Loading branch information
riophae authored and yyx990803 committed May 24, 2017
1 parent 752dbc6 commit 44d4a72
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ export const mapActions = normalizeNamespace((namespace, actions) => {
return res
})

export const createNamespacedHelpers = (namespace) => ({
mapState: mapState.bind(null, namespace),
mapGetters: mapGetters.bind(null, namespace),
mapMutations: mapMutations.bind(null, namespace),
mapActions: mapActions.bind(null, namespace)
})

function normalizeMap (map) {
return Array.isArray(map)
? map.map(key => ({ key, val: key }))
Expand Down
8 changes: 5 additions & 3 deletions src/index.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Store, install } from './store'
import { mapState, mapMutations, mapGetters, mapActions } from './helpers'
import { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from './helpers'

export default {
Store,
Expand All @@ -8,13 +8,15 @@ export default {
mapState,
mapMutations,
mapGetters,
mapActions
mapActions,
createNamespacedHelpers
}

export {
Store,
mapState,
mapMutations,
mapGetters,
mapActions
mapActions,
createNamespacedHelpers
}
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Store, install } from './store'
import { mapState, mapMutations, mapGetters, mapActions } from './helpers'
import { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from './helpers'

export default {
Store,
Expand All @@ -8,5 +8,6 @@ export default {
mapState,
mapMutations,
mapGetters,
mapActions
mapActions,
createNamespacedHelpers
}
59 changes: 58 additions & 1 deletion test/unit/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue/dist/vue.common.js'
import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../dist/vuex.common.js'
import Vuex, { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from '../../dist/vuex.common.js'

describe('Helpers', () => {
it('mapState (array)', () => {
Expand Down Expand Up @@ -321,4 +321,61 @@ describe('Helpers', () => {
vm.bar()
expect(b).toHaveBeenCalled()
})

it('createNamespacedHelpers', () => {
const actionA = jasmine.createSpy()
const actionB = jasmine.createSpy()
const store = new Vuex.Store({
modules: {
foo: {
namespaced: true,
state: { count: 0 },
getters: {
isEven: state => state.count % 2 === 0
},
mutations: {
inc: state => state.count++,
dec: state => state.count--
},
actions: {
actionA,
actionB
}
}
}
})
const {
mapState,
mapGetters,
mapMutations,
mapActions
} = createNamespacedHelpers('foo/')
const vm = new Vue({
store,
computed: {
...mapState(['count']),
...mapGetters(['isEven'])
},
methods: {
...mapMutations(['inc', 'dec']),
...mapActions(['actionA', 'actionB'])
}
})
expect(vm.count).toBe(0)
expect(vm.isEven).toBe(true)
store.state.foo.count++
expect(vm.count).toBe(1)
expect(vm.isEven).toBe(false)
vm.inc()
expect(store.state.foo.count).toBe(2)
expect(store.getters['foo/isEven']).toBe(true)
vm.dec()
expect(store.state.foo.count).toBe(1)
expect(store.getters['foo/isEven']).toBe(false)
vm.actionA()
expect(actionA).toHaveBeenCalled()
expect(actionB).not.toHaveBeenCalled()
vm.actionB()
expect(actionB).toHaveBeenCalled()
})
})

0 comments on commit 44d4a72

Please sign in to comment.