Pārlūkot izejas kodu

improve .use() test cases and make it track installation based on constructor id

Evan You 9 gadi atpakaļ
vecāks
revīzija
b4dd0be4fc

+ 6 - 3
src/core/global-api/use.js

@@ -4,8 +4,11 @@ import { toArray } from '../util/index'
 
 export function initUse (Vue: GlobalAPI) {
   Vue.use = function (plugin: Function | Object) {
-    /* istanbul ignore if */
-    if (plugin.installed) {
+    const cid = this.cid
+    if (!plugin._installed) {
+      plugin._installed = {}
+    }
+    if (plugin._installed[cid]) {
       return this
     }
     // additional parameters
@@ -16,7 +19,7 @@ export function initUse (Vue: GlobalAPI) {
     } else if (typeof plugin === 'function') {
       plugin.apply(null, args)
     }
-    plugin.installed = true
+    plugin._installed[cid] = true
     return this
   }
 }

+ 12 - 0
test/unit/features/global-api/use.spec.js

@@ -14,6 +14,11 @@ describe('Global API: use', () => {
     Vue.use(pluginStub, options)
     expect(Vue.options.directives['plugin-test']).toBe(def)
     delete Vue.options.directives['plugin-test']
+    expect(Vue.options.directives['plugin-test']).toBeUndefined()
+
+    // should not double apply
+    Vue.use(pluginStub, options)
+    expect(Vue.options.directives['plugin-test']).toBeUndefined()
   })
 
   it('should apply Function plugin', () => {
@@ -21,4 +26,11 @@ describe('Global API: use', () => {
     expect(Vue.options.directives['plugin-test']).toBe(def)
     delete Vue.options.directives['plugin-test']
   })
+
+  it('should work on extended constructors without polluting the base', () => {
+    const Ctor = Vue.extend({})
+    Ctor.use(pluginStub, options)
+    expect(Vue.options.directives['plugin-test']).toBeUndefined()
+    expect(Ctor.options.directives['plugin-test']).toBe(def)
+  })
 })