소스 검색

fix: warn when unregistering non existing module (#1786)

* fix: warn when unregistering non existing module

* refactor: store child as a variable and reuse it
Kia King Ishii 4 년 전
부모
커밋
7cec79d339
2개의 변경된 파일23개의 추가작업 그리고 1개의 파일을 삭제
  1. 15 1
      src/module/module-collection.js
  2. 8 0
      test/unit/module/module-collection.spec.js

+ 15 - 1
src/module/module-collection.js

@@ -49,7 +49,21 @@ 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
+    const child = parent.getChild(key)
+
+    if (!child) {
+      if (__DEV__) {
+        console.warn(
+          `[vuex] trying to unregister module '${key}', which is ` +
+          `not registered`
+        )
+      }
+      return
+    }
+
+    if (!child.runtime) {
+      return
+    }
 
     parent.removeChild(key)
   }

+ 8 - 0
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()
+  })
 })