From 2533e145d2a837683f7f63d6501e67721506bcc6 Mon Sep 17 00:00:00 2001 From: Franck Freiburger Date: Thu, 15 Jun 2017 09:42:19 +0200 Subject: [PATCH 1/5] add Store#hasModule(path) API --- src/module/module-collection.js | 8 ++++++++ src/module/module.js | 4 ++++ src/store.js | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/module/module-collection.js b/src/module/module-collection.js index 910c6d2c6..0a150eedf 100644 --- a/src/module/module-collection.js +++ b/src/module/module-collection.js @@ -53,6 +53,14 @@ export default class ModuleCollection { parent.removeChild(key) } + + isRegistered (path) { + const parent = this.get(path.slice(0, -1)) + const key = path[path.length - 1] + + return parent.hasChild(key); + } + } function update (path, targetModule, newModule) { diff --git a/src/module/module.js b/src/module/module.js index cb189f02c..b3a45eb44 100644 --- a/src/module/module.js +++ b/src/module/module.js @@ -25,6 +25,10 @@ export default class Module { return this._children[key] } + hasChild (key) { + return key in this._children; + } + update (rawModule) { this._rawModule.namespaced = rawModule.namespaced if (rawModule.actions) { diff --git a/src/store.js b/src/store.js index 3a4f1bab7..6d2a85e0e 100644 --- a/src/store.js +++ b/src/store.js @@ -179,6 +179,16 @@ export class Store { resetStore(this) } + hasModule (path) { + if (typeof path === 'string') path = [path] + + if (process.env.NODE_ENV !== 'production') { + assert(Array.isArray(path), `module path must be a string or an Array.`) + } + + this._modules.isRegistered(path) + } + hotUpdate (newOptions) { this._modules.update(newOptions) resetStore(this, true) From 57744da437f52e317a731bb98518489fb1f73558 Mon Sep 17 00:00:00 2001 From: Franck Freiburger Date: Thu, 15 Jun 2017 09:54:35 +0200 Subject: [PATCH 2/5] fix Store::hasModule(path) return value --- src/store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/store.js b/src/store.js index 6d2a85e0e..8adf41454 100644 --- a/src/store.js +++ b/src/store.js @@ -186,7 +186,7 @@ export class Store { assert(Array.isArray(path), `module path must be a string or an Array.`) } - this._modules.isRegistered(path) + return this._modules.isRegistered(path) } hotUpdate (newOptions) { From 09f319705a7d1caead6506f40627e244017d8c0c Mon Sep 17 00:00:00 2001 From: Franck Freiburger Date: Thu, 15 Jun 2017 10:04:54 +0200 Subject: [PATCH 3/5] add unit test for Store#hasModule(path) --- test/unit/modules.spec.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/unit/modules.spec.js b/test/unit/modules.spec.js index 1d9c8d688..5adfb272e 100644 --- a/test/unit/modules.spec.js +++ b/test/unit/modules.spec.js @@ -25,6 +25,8 @@ describe('Modules', () => { getters: { a: state => state.a } }) }).not.toThrow() + + expect(store.hasModule('hi')).toBe(true) expect(store._mutations.inc.length).toBe(2) expect(store.state.hi.a).toBe(1) @@ -43,6 +45,7 @@ describe('Modules', () => { // unregister store.unregisterModule('hi') + expect(store.hasModule('hi')).toBe(false) expect(store.state.hi).toBeUndefined() expect(store.getters.a).toBeUndefined() expect(store._mutations.inc.length).toBe(1) From 338dc9adb259b2db5d2149465739e6b8f6cd8c9e Mon Sep 17 00:00:00 2001 From: Franck Freiburger Date: Mon, 10 Jul 2017 22:13:56 +0200 Subject: [PATCH 4/5] Revert "add unit test for Store#hasModule(path)" (reverts commit 09f319705a7d1caead6506f40627e244017d8c0c.) Add a new test for Store#hasModule() --- test/unit/modules.spec.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test/unit/modules.spec.js b/test/unit/modules.spec.js index 5adfb272e..74cf71e47 100644 --- a/test/unit/modules.spec.js +++ b/test/unit/modules.spec.js @@ -25,8 +25,6 @@ describe('Modules', () => { getters: { a: state => state.a } }) }).not.toThrow() - - expect(store.hasModule('hi')).toBe(true) expect(store._mutations.inc.length).toBe(2) expect(store.state.hi.a).toBe(1) @@ -45,7 +43,6 @@ describe('Modules', () => { // unregister store.unregisterModule('hi') - expect(store.hasModule('hi')).toBe(false) expect(store.state.hi).toBeUndefined() expect(store.getters.a).toBeUndefined() expect(store._mutations.inc.length).toBe(1) @@ -83,6 +80,18 @@ describe('Modules', () => { store.commit('a/foo') expect(mutationSpy).toHaveBeenCalled() }) + + it('dynamic module existance test', () => { + + store.registerModule('bonjour', { + state: { a: 1 }, + }) + + expect(store.hasModule('bonjour')).toBe(true) + store.unregisterModule('bonjour') + expect(store.hasModule('bonjour')).toBe(false) + }) + }) // #524 From bf174ed2b471c3929e02c3d03bcddb8e9fd3fcd3 Mon Sep 17 00:00:00 2001 From: Franck Freiburger Date: Mon, 10 Jul 2017 22:20:29 +0200 Subject: [PATCH 5/5] fix linting issues --- src/module/module-collection.js | 4 ++-- src/module/module.js | 2 +- test/unit/modules.spec.js | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/module/module-collection.js b/src/module/module-collection.js index 0a150eedf..6cb4fb9f9 100644 --- a/src/module/module-collection.js +++ b/src/module/module-collection.js @@ -57,8 +57,8 @@ export default class ModuleCollection { isRegistered (path) { const parent = this.get(path.slice(0, -1)) const key = path[path.length - 1] - - return parent.hasChild(key); + + return parent.hasChild(key) } } diff --git a/src/module/module.js b/src/module/module.js index b3a45eb44..d619b151d 100644 --- a/src/module/module.js +++ b/src/module/module.js @@ -26,7 +26,7 @@ export default class Module { } hasChild (key) { - return key in this._children; + return key in this._children } update (rawModule) { diff --git a/test/unit/modules.spec.js b/test/unit/modules.spec.js index 74cf71e47..a93c43c26 100644 --- a/test/unit/modules.spec.js +++ b/test/unit/modules.spec.js @@ -80,18 +80,18 @@ describe('Modules', () => { store.commit('a/foo') expect(mutationSpy).toHaveBeenCalled() }) - + it('dynamic module existance test', () => { - - store.registerModule('bonjour', { - state: { a: 1 }, - }) - - expect(store.hasModule('bonjour')).toBe(true) - store.unregisterModule('bonjour') - expect(store.hasModule('bonjour')).toBe(false) + const store = new Vuex.Store({ + }) + + store.registerModule('bonjour', { + }) + + expect(store.hasModule('bonjour')).toBe(true) + store.unregisterModule('bonjour') + expect(store.hasModule('bonjour')).toBe(false) }) - }) // #524