Skip to content

Commit f98af60

Browse files
committed
add mapState & mapMutations
1 parent 28b70d9 commit f98af60

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

src/helpers.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
export function mapState (map) {
2+
const res = {}
3+
Object.keys(map).forEach(key => {
4+
const fn = map[key]
5+
res[key] = function mappedState () {
6+
return fn.call(this, this.$store.state)
7+
}
8+
})
9+
return res
10+
}
11+
12+
export function mapMutations (mutations) {
13+
const res = {}
14+
normalizeMap(mutations).forEach(({ key, val }) => {
15+
res[key] = function mappedMutation (...args) {
16+
return this.$store.commit(val, ...args)
17+
}
18+
})
19+
return res
20+
}
21+
122
export function mapGetters (getters) {
223
const res = {}
324
normalizeMap(getters).forEach(({ key, val }) => {

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import devtoolPlugin from './plugins/devtool'
22
import applyMixin from './mixin'
3-
import { mapGetters, mapActions } from './helpers'
3+
import { mapState, mapMutations, mapGetters, mapActions } from './helpers'
44

55
let Vue // bind on install
66

@@ -322,6 +322,8 @@ if (typeof window !== 'undefined' && window.Vue) {
322322
export default {
323323
Store,
324324
install,
325+
mapState,
326+
mapMutations,
325327
mapGetters,
326328
mapActions
327329
}

test/unit/test.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chai, { expect } from 'chai'
33
import sinonChai from 'sinon-chai'
44
import sinon from 'sinon'
55
import Vue from 'vue'
6-
import Vuex, { mapGetters, mapActions } from '../../build/dev-entry'
6+
import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../build/dev-entry'
77

88
Vue.use(Vuex)
99
chai.use(sinonChai)
@@ -215,6 +215,62 @@ describe('Vuex', () => {
215215
expect(child.$store).to.equal(store)
216216
})
217217

218+
it('helper: mapState', () => {
219+
const store = new Vuex.Store({
220+
state: {
221+
a: 1
222+
}
223+
})
224+
const vm = new Vue({
225+
store,
226+
computed: mapState({
227+
a: state => state.a + 1
228+
})
229+
})
230+
expect(vm.a).to.equal(2)
231+
store.state.a++
232+
expect(vm.a).to.equal(3)
233+
})
234+
235+
it('helper: mapMutations (array)', () => {
236+
const store = new Vuex.Store({
237+
state: { count: 0 },
238+
mutations: {
239+
inc: state => state.count++,
240+
dec: state => state.count--
241+
}
242+
})
243+
const vm = new Vue({
244+
store,
245+
methods: mapMutations(['inc', 'dec'])
246+
})
247+
vm.inc()
248+
expect(store.state.count).to.equal(1)
249+
vm.dec()
250+
expect(store.state.count).to.equal(0)
251+
})
252+
253+
it('helper: mapMutations (object)', () => {
254+
const store = new Vuex.Store({
255+
state: { count: 0 },
256+
mutations: {
257+
inc: state => state.count++,
258+
dec: state => state.count--
259+
}
260+
})
261+
const vm = new Vue({
262+
store,
263+
methods: mapMutations({
264+
plus: 'inc',
265+
minus: 'dec'
266+
})
267+
})
268+
vm.plus()
269+
expect(store.state.count).to.equal(1)
270+
vm.minus()
271+
expect(store.state.count).to.equal(0)
272+
})
273+
218274
it('helper: mapGetters (array)', () => {
219275
const store = new Vuex.Store({
220276
state: { count: 0 },

0 commit comments

Comments
 (0)