Skip to content

Commit d65d142

Browse files
feat: add Store#hasModule(path) API (#834)
* add Store#hasModule(path) API * fix Store::hasModule(path) return value * add unit test for Store#hasModule(path) * Revert "add unit test for Store#hasModule(path)" (reverts commit 09f3197.) Add a new test for Store#hasModule() * fix linting issues
1 parent 16fbb36 commit d65d142

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

src/module/module-collection.js

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ export default class ModuleCollection {
5353

5454
parent.removeChild(key)
5555
}
56+
57+
isRegistered (path) {
58+
const parent = this.get(path.slice(0, -1))
59+
const key = path[path.length - 1]
60+
61+
return parent.hasChild(key)
62+
}
63+
5664
}
5765

5866
function update (path, targetModule, newModule) {

src/module/module.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export default class Module {
3030
return this._children[key]
3131
}
3232

33+
hasChild (key) {
34+
return key in this._children
35+
}
36+
3337
update (rawModule) {
3438
this._rawModule.namespaced = rawModule.namespaced
3539
if (rawModule.actions) {

src/store.js

+10
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ export class Store {
215215
resetStore(this)
216216
}
217217

218+
hasModule (path) {
219+
if (typeof path === 'string') path = [path]
220+
221+
if (process.env.NODE_ENV !== 'production') {
222+
assert(Array.isArray(path), `module path must be a string or an Array.`)
223+
}
224+
225+
return this._modules.isRegistered(path)
226+
}
227+
218228
hotUpdate (newOptions) {
219229
this._modules.update(newOptions)
220230
resetStore(this, true)

test/unit/modules.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ describe('Modules', () => {
8080
store.commit('a/foo')
8181
expect(mutationSpy).toHaveBeenCalled()
8282
})
83+
it('dynamic module existance test', () => {
84+
const store = new Vuex.Store({
85+
})
86+
87+
store.registerModule('bonjour', {
88+
})
89+
90+
expect(store.hasModule('bonjour')).toBe(true)
91+
store.unregisterModule('bonjour')
92+
expect(store.hasModule('bonjour')).toBe(false)
93+
})
8394

8495
it('dynamic module registration preserving hydration', () => {
8596
const store = new Vuex.Store({})

0 commit comments

Comments
 (0)