From ee12205c44e057d13c53df485696011c0953b500 Mon Sep 17 00:00:00 2001 From: Riophae Lee Date: Wed, 24 May 2017 15:21:48 +0800 Subject: [PATCH] add `createNamespacedHelpers()` to help dealing with namespaced modules --- src/helpers.js | 7 +++++ src/index.esm.js | 8 ++++-- src/index.js | 5 ++-- test/unit/helpers.spec.js | 59 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index a98be7cf5..7c28563b9 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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 })) diff --git a/src/index.esm.js b/src/index.esm.js index f2db95754..c905e0e6c 100644 --- a/src/index.esm.js +++ b/src/index.esm.js @@ -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, @@ -8,7 +8,8 @@ export default { mapState, mapMutations, mapGetters, - mapActions + mapActions, + createNamespacedHelpers } export { @@ -16,5 +17,6 @@ export { mapState, mapMutations, mapGetters, - mapActions + mapActions, + createNamespacedHelpers } diff --git a/src/index.js b/src/index.js index 899732c30..ea28ccc1c 100644 --- a/src/index.js +++ b/src/index.js @@ -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, @@ -8,5 +8,6 @@ export default { mapState, mapMutations, mapGetters, - mapActions + mapActions, + createNamespacedHelpers } diff --git a/test/unit/helpers.spec.js b/test/unit/helpers.spec.js index 1a04c8c2a..8572044bf 100644 --- a/test/unit/helpers.spec.js +++ b/test/unit/helpers.spec.js @@ -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)', () => { @@ -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() + }) })