From 054cee89e4e0c02f135b629de35f75af79149ba9 Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Mon, 29 Jun 2020 23:24:35 +0900 Subject: [PATCH 1/2] fix: warn when unregistering non existing module --- src/module/module-collection.js | 15 ++++++++++++++- test/unit/module/module-collection.spec.js | 8 ++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/module/module-collection.js b/src/module/module-collection.js index ee1a887ba..a6420c953 100644 --- a/src/module/module-collection.js +++ b/src/module/module-collection.js @@ -49,7 +49,20 @@ export default class ModuleCollection { unregister (path) { const parent = this.get(path.slice(0, -1)) const key = path[path.length - 1] - if (!parent.getChild(key).runtime) return + + if (!parent.getChild(key)) { + if (__DEV__) { + console.warn( + `[vuex] trying to unregister module '${key}', which is ` + + `not registered` + ) + } + return + } + + if (!parent.getChild(key).runtime) { + return + } parent.removeChild(key) } diff --git a/test/unit/module/module-collection.spec.js b/test/unit/module/module-collection.spec.js index 007989af2..3e8081ce3 100644 --- a/test/unit/module/module-collection.spec.js +++ b/test/unit/module/module-collection.spec.js @@ -92,4 +92,12 @@ describe('ModuleCollection', () => { collection.unregister(['a']) expect(collection.get(['a']).state.value).toBe(true) }) + + it('warns when unregistering non existing module', () => { + const spy = jest.spyOn(console, 'warn').mockImplementation() + + const collection = new ModuleCollection({}) + collection.unregister(['a']) + expect(spy).toHaveBeenCalled() + }) }) From 4fae6220389bbb66a01a3b6178bb09993abe389a Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Mon, 29 Jun 2020 23:44:36 +0900 Subject: [PATCH 2/2] refactor: store child as a variable and reuse it --- src/module/module-collection.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/module/module-collection.js b/src/module/module-collection.js index a6420c953..095872882 100644 --- a/src/module/module-collection.js +++ b/src/module/module-collection.js @@ -49,8 +49,9 @@ export default class ModuleCollection { unregister (path) { const parent = this.get(path.slice(0, -1)) const key = path[path.length - 1] + const child = parent.getChild(key) - if (!parent.getChild(key)) { + if (!child) { if (__DEV__) { console.warn( `[vuex] trying to unregister module '${key}', which is ` + @@ -60,7 +61,7 @@ export default class ModuleCollection { return } - if (!parent.getChild(key).runtime) { + if (!child.runtime) { return }