Browse Source

support array syntax for mapState (#260)

* support array syntax for mapState

* fix tests for PRs on circle ci
Chris Fritz 9 năm trước cách đây
mục cha
commit
d82a789fb6
3 tập tin đã thay đổi với 23 bổ sung5 xóa
  1. 2 0
      circle.yml
  2. 5 4
      src/helpers.js
  3. 16 1
      test/unit/test.js

+ 2 - 0
circle.yml

@@ -1,3 +1,5 @@
 machine:
   node:
     version: 5
+  environment:
+    BABEL_ENV: development

+ 5 - 4
src/helpers.js

@@ -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

+ 16 - 1
test/unit/test.js

@@ -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