Skip to content

Commit

Permalink
feat: add Store#hasModule(path) API (#834)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FranckFreiburger authored Mar 25, 2020
1 parent 16fbb36 commit d65d142
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/module/module-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/module/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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) {
Expand Down
10 changes: 10 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,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.`)
}

return this._modules.isRegistered(path)
}

hotUpdate (newOptions) {
this._modules.update(newOptions)
resetStore(this, true)
Expand Down
11 changes: 11 additions & 0 deletions test/unit/modules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ describe('Modules', () => {
store.commit('a/foo')
expect(mutationSpy).toHaveBeenCalled()
})
it('dynamic module existance test', () => {
const store = new Vuex.Store({
})

store.registerModule('bonjour', {
})

expect(store.hasModule('bonjour')).toBe(true)
store.unregisterModule('bonjour')
expect(store.hasModule('bonjour')).toBe(false)
})

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

0 comments on commit d65d142

Please sign in to comment.