Skip to content

Commit 9b3aa50

Browse files
committed
add mapState & mapMutations
1 parent 2e46bf9 commit 9b3aa50

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

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

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)
@@ -225,6 +225,62 @@ describe('Vuex', () => {
225225
expect(child.$store).to.equal(store)
226226
})
227227

228+
it('helper: mapState', () => {
229+
const store = new Vuex.Store({
230+
state: {
231+
a: 1
232+
}
233+
})
234+
const vm = new Vue({
235+
store,
236+
computed: mapState({
237+
a: state => state.a + 1
238+
})
239+
})
240+
expect(vm.a).to.equal(2)
241+
store.state.a++
242+
expect(vm.a).to.equal(3)
243+
})
244+
245+
it('helper: mapMutations (array)', () => {
246+
const store = new Vuex.Store({
247+
state: { count: 0 },
248+
mutations: {
249+
inc: state => state.count++,
250+
dec: state => state.count--
251+
}
252+
})
253+
const vm = new Vue({
254+
store,
255+
methods: mapMutations(['inc', 'dec'])
256+
})
257+
vm.inc()
258+
expect(store.state.count).to.equal(1)
259+
vm.dec()
260+
expect(store.state.count).to.equal(0)
261+
})
262+
263+
it('helper: mapMutations (object)', () => {
264+
const store = new Vuex.Store({
265+
state: { count: 0 },
266+
mutations: {
267+
inc: state => state.count++,
268+
dec: state => state.count--
269+
}
270+
})
271+
const vm = new Vue({
272+
store,
273+
methods: mapMutations({
274+
plus: 'inc',
275+
minus: 'dec'
276+
})
277+
})
278+
vm.plus()
279+
expect(store.state.count).to.equal(1)
280+
vm.minus()
281+
expect(store.state.count).to.equal(0)
282+
})
283+
228284
it('helper: mapGetters (array)', () => {
229285
const store = new Vuex.Store({
230286
state: { count: 0 },

0 commit comments

Comments
 (0)