Skip to content

Commit f76d72d

Browse files
committed
fix: stop throwing an error on hasModule when parent does not exists (#1850)
fix #1850
1 parent 027feff commit f76d72d

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/module/module-collection.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ export default class ModuleCollection {
7272
const parent = this.get(path.slice(0, -1))
7373
const key = path[path.length - 1]
7474

75-
return parent.hasChild(key)
75+
if (parent) {
76+
return parent.hasChild(key)
77+
}
78+
79+
return false
7680
}
7781
}
7882

test/unit/module/module-collection.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ describe('ModuleCollection', () => {
8181
expect(collection.get(['a'])).toBe(undefined)
8282
})
8383

84+
it('isRegistered', () => {
85+
const collection = new ModuleCollection({})
86+
87+
collection.register(['a'], {
88+
state: { value: true }
89+
})
90+
91+
collection.register(['a', 'b'], {
92+
state: { value: false }
93+
})
94+
95+
expect(collection.isRegistered(['a'])).toBe(true)
96+
expect(collection.isRegistered(['a', 'b'])).toBe(true)
97+
expect(collection.isRegistered(['c'])).toBe(false)
98+
expect(collection.isRegistered(['c', 'd'])).toBe(false)
99+
})
100+
84101
it('does not unregister initial modules', () => {
85102
const collection = new ModuleCollection({
86103
modules: {

test/unit/modules.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ describe('Modules', () => {
9191
expect(store.hasModule('bonjour')).toBe(false)
9292
})
9393

94+
it('dynamic module existance test with nested modules', () => {
95+
const store = new Vuex.Store({})
96+
97+
store.registerModule('a', {})
98+
store.registerModule(['a', 'b'], {})
99+
100+
expect(store.hasModule(['a'])).toBe(true)
101+
expect(store.hasModule(['a', 'b'])).toBe(true)
102+
expect(store.hasModule(['c'])).toBe(false)
103+
expect(store.hasModule(['c', 'd'])).toBe(false)
104+
})
105+
94106
it('dynamic module registration preserving hydration', () => {
95107
const store = new Vuex.Store({})
96108
store.replaceState({ a: { foo: 'state' }})

0 commit comments

Comments
 (0)