Skip to content

Commit d82a789

Browse files
chrisvfritzyyx990803
authored andcommitted
support array syntax for mapState (#260)
* support array syntax for mapState * fix tests for PRs on circle ci
1 parent 2473178 commit d82a789

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

circle.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
machine:
22
node:
33
version: 5
4+
environment:
5+
BABEL_ENV: development

src/helpers.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
export function mapState (map) {
1+
export function mapState (states) {
22
const res = {}
3-
Object.keys(map).forEach(key => {
4-
const fn = map[key]
3+
normalizeMap(states).forEach(({ key, val }) => {
54
res[key] = function mappedState () {
6-
return fn.call(this, this.$store.state, this.$store.getters)
5+
return typeof val === 'function'
6+
? val.call(this, this.$store.state, this.$store.getters)
7+
: this.$store.state[val]
78
}
89
})
910
return res

test/unit/test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,22 @@ describe('Vuex', () => {
225225
expect(child.$store).toBe(store)
226226
})
227227

228-
it('helper: mapState', () => {
228+
it('helper: mapState (array)', () => {
229+
const store = new Vuex.Store({
230+
state: {
231+
a: 1
232+
}
233+
})
234+
const vm = new Vue({
235+
store,
236+
computed: mapState(['a'])
237+
})
238+
expect(vm.a).toBe(1)
239+
store.state.a++
240+
expect(vm.a).toBe(2)
241+
})
242+
243+
it('helper: mapState (object)', () => {
229244
const store = new Vuex.Store({
230245
state: {
231246
a: 1

0 commit comments

Comments
 (0)