Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
machine:
node:
version: 5
environment:
BABEL_ENV: development
9 changes: 5 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export function mapState (map) {
export function mapState (states) {
const res = {}
Object.keys(map).forEach(key => {
const fn = map[key]
normalizeMap(states).forEach(({ key, val }) => {
res[key] = function mappedState () {
return fn.call(this, this.$store.state, this.$store.getters)
return typeof val === 'function'
? val.call(this, this.$store.state, this.$store.getters)
: this.$store.state[val]
}
})
return res
Expand Down
17 changes: 16 additions & 1 deletion test/unit/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,22 @@ describe('Vuex', () => {
expect(child.$store).toBe(store)
})

it('helper: mapState', () => {
it('helper: mapState (array)', () => {
const store = new Vuex.Store({
state: {
a: 1
}
})
const vm = new Vue({
store,
computed: mapState(['a'])
})
expect(vm.a).toBe(1)
store.state.a++
expect(vm.a).toBe(2)
})

it('helper: mapState (object)', () => {
const store = new Vuex.Store({
state: {
a: 1
Expand Down