8000 fix: warn when unregistering non existing module by kiaking · Pull Request #1786 · vuejs/vuex · GitHub
[go: up one dir, main page]

Skip to content

fix: warn when unregistering non existing module #1786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: warn when unregistering non existing module
  • Loading branch information
kiaking committed Jun 29, 2020
commit 054cee89e4e0c02f135b629de35f75af79149ba9
15 changes: 14 additions & 1 deletion src/module/module-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we store the return value of getChild in a variable so that we reuse it with the next if condition?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah good point! Thanks. I've done it, and rebased (to the latest dev) and pushed 👍

if (__DEV__) {
console.warn(
`[vuex] trying to unregister module '${key}', which is ` +
`not registered`
)
}
return
}

if (!parent.getChild(key).runtime) {
return
}

parent.removeChild(key)
}
Expand Down
8 changes: 8 additions & 0 deletions test/unit/module/module-collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
0