Skip to content

Commit b4dd0be

Browse files
committed
improve .use() test cases and make it track installation based on constructor id
1 parent f97ebd6 commit b4dd0be

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/core/global-api/use.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import { toArray } from '../util/index'
44

55
export function initUse (Vue: GlobalAPI) {
66
Vue.use = function (plugin: Function | Object) {
7-
/* istanbul ignore if */
8-
if (plugin.installed) {
7+
const cid = this.cid
8+
if (!plugin._installed) {
9+
plugin._installed = {}
10+
}
11+
if (plugin._installed[cid]) {
912
return this
1013
}
1114
// additional parameters
@@ -16,7 +19,7 @@ export function initUse (Vue: GlobalAPI) {
1619
} else if (typeof plugin === 'function') {
1720
plugin.apply(null, args)
1821
}
19-
plugin.installed = true
22+
plugin._installed[cid] = true
2023
return this
2124
}
2225
}

test/unit/features/global-api/use.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,23 @@ describe('Global API: use', () => {
1414
Vue.use(pluginStub, options)
1515
expect(Vue.options.directives['plugin-test']).toBe(def)
1616
delete Vue.options.directives['plugin-test']
17+
expect(Vue.options.directives['plugin-test']).toBeUndefined()
18+
19+
// should not double apply
20+
Vue.use(pluginStub, options)
21+
expect(Vue.options.directives['plugin-test']).toBeUndefined()
1722
})
1823

1924
it('should apply Function plugin', () => {
2025
Vue.use(pluginStub.install, options)
2126
expect(Vue.options.directives['plugin-test']).toBe(def)
2227
delete Vue.options.directives['plugin-test']
2328
})
29+
30+
it('should work on extended constructors without polluting the base', () => {
31+
const Ctor = Vue.extend({})
32+
Ctor.use(pluginStub, options)
33+
expect(Vue.options.directives['plugin-test']).toBeUndefined()
34+
expect(Ctor.options.directives['plugin-test']).toBe(def)
35+
})
2436
})

0 commit comments

Comments
 (0)