Browse Source

module option change: namespace: string -> namespaced: boolean

Evan You 8 years ago
parent
commit
bc61bf64c2

+ 1 - 1
src/module/module-collection.js

@@ -24,7 +24,7 @@ export default class ModuleCollection {
     let module = this.root
     return path.reduce((namespace, key) => {
       module = module.getChild(key)
-      return namespace + module.namespace
+      return namespace + (module.namespaced ? key + '/' : '')
     }, '')
   }
 

+ 3 - 3
src/module/module.js

@@ -11,8 +11,8 @@ export default class Module {
     return this._rawModule.state || {}
   }
 
-  get namespace () {
-    return this._rawModule.namespace || ''
+  get namespaced () {
+    return !!this._rawModule.namespaced
   }
 
   addChild (key, module) {
@@ -28,7 +28,7 @@ export default class Module {
   }
 
   update (rawModule) {
-    this._rawModule.namespace = rawModule.namespace
+    this._rawModule.namespaced = rawModule.namespaced
     if (rawModule.actions) {
       this._rawModule.actions = rawModule.actions
     }

+ 6 - 6
test/unit/module/module-collection.js

@@ -25,20 +25,20 @@ describe('ModuleCollection', () => {
   })
 
   it('getNamespace', () => {
-    const module = (namespace, children) => {
+    const module = (namespaced, children) => {
       return {
-        namespace,
+        namespaced,
         modules: children
       }
     }
     const collection = new ModuleCollection({
       namespace: 'ignore/', // root module namespace should be ignored
       modules: {
-        a: module('a/', {
-          b: module(null, {
-            c: module('c/')
+        a: module(true, {
+          b: module(false, {
+            c: module(true)
           }),
-          d: module('d/')
+          d: module(true)
         })
       }
     })

+ 4 - 4
test/unit/module/module.js

@@ -17,13 +17,13 @@ describe('Module', () => {
 
   it('get namespacer: no namespace option', () => {
     const module = new Module({})
-    expect(module.namespace).toBe('')
+    expect(module.namespaced).toBe(false)
   })
 
-  it('get namespacer: namespace option is string value', () => {
+  it('get namespacer: namespace option is true', () => {
     const module = new Module({
-      namespace: 'prefix/'
+      namespaced: true
     })
-    expect(module.namespace).toBe('prefix/')
+    expect(module.namespaced).toBe(true)
   })
 })

+ 33 - 33
test/unit/test.js

@@ -251,7 +251,7 @@ describe('Vuex', () => {
     const store = new Vuex.Store({
       modules: {
         a: {
-          namespace: 'prefix/'
+          namespaced: true
         }
       }
     })
@@ -265,12 +265,12 @@ describe('Vuex', () => {
     })
 
     expect(store.state.a.b.value).toBe(1)
-    expect(store.getters['prefix/foo']).toBe(1)
+    expect(store.getters['a/foo']).toBe(1)
 
-    store.dispatch('prefix/foo')
+    store.dispatch('a/foo')
     expect(actionSpy).toHaveBeenCalled()
 
-    store.commit('prefix/foo')
+    store.commit('a/foo')
     expect(mutationSpy).toHaveBeenCalled()
   })
 
@@ -614,7 +614,7 @@ describe('Vuex', () => {
     const store = new Vuex.Store({
       modules: {
         a: {
-          namespace: 'prefix/',
+          namespaced: true,
           state: {
             a: 1
           },
@@ -632,10 +632,10 @@ describe('Vuex', () => {
     })
 
     expect(store.state.a.a).toBe(1)
-    expect(store.getters['prefix/b']).toBe(2)
-    store.dispatch('prefix/' + TEST)
+    expect(store.getters['a/b']).toBe(2)
+    store.dispatch('a/' + TEST)
     expect(actionSpy).toHaveBeenCalled()
-    store.commit('prefix/' + TEST)
+    store.commit('a/' + TEST)
     expect(mutationSpy).toHaveBeenCalled()
   })
 
@@ -643,7 +643,7 @@ describe('Vuex', () => {
     // mock module generator
     const actionSpys = []
     const mutationSpys = []
-    const createModule = (name, namespace, children) => {
+    const createModule = (name, namespaced, children) => {
       const actionSpy = jasmine.createSpy()
       const mutationSpy = jasmine.createSpy()
 
@@ -651,7 +651,7 @@ describe('Vuex', () => {
       mutationSpys.push(mutationSpy)
 
       return {
-        namespace,
+        namespaced,
         state: {
           [name]: true
         },
@@ -670,11 +670,11 @@ describe('Vuex', () => {
 
     // mock module
     const modules = {
-      a: createModule('a', 'a/', { // a/a
-        b: createModule('b', null, { // a/b - does not add namespace
-          c: createModule('c', 'c/') // a/c/c
+      a: createModule('a', true, { // a/a
+        b: createModule('b', false, { // a/b - does not add namespace
+          c: createModule('c', true) // a/c/c
         }),
-        d: createModule('d', 'd/'), // a/d/d
+        d: createModule('d', true), // a/d/d
       })
     }
 
@@ -714,7 +714,7 @@ describe('Vuex', () => {
       },
       modules: {
         a: {
-          namespace: 'prefix/',
+          namespaced: true,
           state: { value: 'module' },
           getters: {
             foo: state => state.value,
@@ -725,9 +725,9 @@ describe('Vuex', () => {
       }
     })
 
-    expect(store.getters['prefix/foo']).toBe('module')
-    expect(store.getters['prefix/bar']).toBe('module')
-    expect(store.getters['prefix/baz']).toBe('root')
+    expect(store.getters['a/foo']).toBe('module')
+    expect(store.getters['a/bar']).toBe('module')
+    expect(store.getters['a/baz']).toBe('root')
   })
 
   it('module: action context is namespaced in namespaced module', done => {
@@ -743,7 +743,7 @@ describe('Vuex', () => {
       mutations: { foo: rootMutationSpy },
       modules: {
         a: {
-          namespace: 'prefix/',
+          namespaced: true,
           state: { value: 'module' },
           getters: { foo: state => state.value },
           actions: {
@@ -770,7 +770,7 @@ describe('Vuex', () => {
       }
     })
 
-    store.dispatch('prefix/test')
+    store.dispatch('a/test')
   })
 
   it('module: use other module that has same namespace', done => {
@@ -780,7 +780,7 @@ describe('Vuex', () => {
     const store = new Vuex.Store({
       modules: {
         parent: {
-          namespace: 'prefix/',
+          namespaced: true,
 
           modules: {
             a: {
@@ -813,7 +813,7 @@ describe('Vuex', () => {
       }
     })
 
-    store.dispatch('prefix/test')
+    store.dispatch('parent/test')
   })
 
   it('dispatching multiple actions in different modules', done => {
@@ -1210,7 +1210,7 @@ describe('Vuex', () => {
     const store = new Vuex.Store({
       modules: {
         a: {
-          namespace: 'prefix/',
+          namespaced: true,
           state: { value: 1 },
           getters: { foo: state => state.value },
           actions: { foo: actionSpy },
@@ -1220,38 +1220,38 @@ describe('Vuex', () => {
     })
 
     expect(store.state.a.value).toBe(1)
-    expect(store.getters['prefix/foo']).toBe(1)
-    store.dispatch('prefix/foo')
+    expect(store.getters['a/foo']).toBe(1)
+    store.dispatch('a/foo')
     expect(actionSpy.calls.count()).toBe(1)
-    store.commit('prefix/foo')
+    store.commit('a/foo')
     expect(actionSpy.calls.count()).toBe(1)
 
     store.hotUpdate({
       modules: {
         a: {
-          namespace: 'prefix-changed/'
+          namespaced: false
         }
       }
     })
 
     expect(store.state.a.value).toBe(1)
-    expect(store.getters['prefix/foo']).toBe(undefined) // removed
-    expect(store.getters['prefix-changed/foo']).toBe(1) // renamed
+    expect(store.getters['a/foo']).toBe(undefined) // removed
+    expect(store.getters['foo']).toBe(1) // renamed
 
     // should not be called
-    store.dispatch('prefix/foo')
+    store.dispatch('a/foo')
     expect(actionSpy.calls.count()).toBe(1)
 
     // should be called
-    store.dispatch('prefix-changed/foo')
+    store.dispatch('foo')
     expect(actionSpy.calls.count()).toBe(2)
 
     // should not be called
-    store.commit('prefix/foo')
+    store.commit('a/foo')
     expect(mutationSpy.calls.count()).toBe(1)
 
     // should be called
-    store.commit('prefix-changed/foo')
+    store.commit('foo')
     expect(mutationSpy.calls.count()).toBe(2)
   })