Преглед на файлове

avoid to get wrong state with mapState (#709)

* avoid to get wrong state with mapState

* checking only namespaced option is enough
katashin преди 8 години
родител
ревизия
47cf44746a
променени са 2 файла, в които са добавени 27 реда и са изтрити 1 реда
  1. 1 1
      src/store.js
  2. 26 0
      test/unit/helpers.spec.js

+ 1 - 1
src/store.js

@@ -226,7 +226,7 @@ function installModule (store, rootState, path, module, hot) {
   const namespace = store._modules.getNamespace(path)
 
   // register in namespace map
-  if (namespace) {
+  if (module.namespaced) {
     store._modulesNamespaceMap[namespace] = module
   }
 

+ 26 - 0
test/unit/helpers.spec.js

@@ -68,6 +68,32 @@ describe('Helpers', () => {
     expect(vm.a).toBe(7)
   })
 
+  // #708
+  it('mapState (with namespace and a nested module)', () => {
+    const store = new Vuex.Store({
+      modules: {
+        foo: {
+          namespaced: true,
+          state: { a: 1 },
+          modules: {
+            bar: {
+              state: { b: 2 }
+            }
+          }
+        }
+      }
+    })
+    const vm = new Vue({
+      store,
+      computed: mapState('foo', {
+        value: state => state
+      })
+    })
+    expect(vm.value.a).toBe(1)
+    expect(vm.value.bar.b).toBe(2)
+    expect(vm.value.b).toBeUndefined()
+  })
+
   it('mapMutations (array)', () => {
     const store = new Vuex.Store({
       state: { count: 0 },