Explorar el Código

tests for global api

Evan You hace 11 años
padre
commit
4ab7fc2491
Se han modificado 2 ficheros con 99 adiciones y 10 borrados
  1. 5 10
      src/api/global.js
  2. 94 0
      test/unit/specs/api/global_spec.js

+ 5 - 10
src/api/global.js

@@ -8,7 +8,6 @@ var mergeOptions = require('../util/merge-option')
 exports.util       = _
 exports.nextTick   = _.nextTick
 exports.config     = require('../config')
-exports.transition = require('../transition')
 
 /**
  * Each instance constructor, including Vue, has a unique
@@ -33,7 +32,10 @@ exports.extend = function (extendOptions) {
   Sub.prototype = Object.create(Super.prototype)
   Sub.prototype.constructor = Sub
   Sub.cid = cid++
-  Sub.options = mergeOptions(Super.options, extendOptions)
+  Sub.options = mergeOptions(
+    Super.options,
+    extendOptions || {}
+  )
   Sub.super = Super
   // allow further extension
   Sub.extend = Super.extend
@@ -46,17 +48,10 @@ exports.extend = function (extendOptions) {
 /**
  * Plugin system
  *
- * @param {String|Object} plugin
+ * @param {Object} plugin
  */
 
 exports.use = function (plugin) {
-  if (typeof plugin === 'string') {
-    try {
-      plugin = require(plugin)
-    } catch (e) {
-      _.warn('Cannot load plugin: ' + plugin)
-    }
-  }
   // additional parameters
   var args = _.toArray(arguments, 1)
   args.unshift(this)

+ 94 - 0
test/unit/specs/api/global_spec.js

@@ -0,0 +1,94 @@
+var Vue = require('../../../../src/vue')
+var _ = require('../../../../src/util')
+var config = require('../../../../src/config')
+
+describe('Global API', function () {
+
+  it('exposed utilities', function () {
+    expect(Vue.util).toBe(_)
+    expect(Vue.nextTick).toBe(_.nextTick)
+    expect(Vue.config).toBe(config)
+  })
+
+  it('extend', function () {
+    var Test = Vue.extend({
+      a: 1,
+      b: 2
+    })
+    expect(Test.options.a).toBe(1)
+    expect(Test.options.b).toBe(2)
+    expect(Test.super).toBe(Vue)
+    var t = new Test({
+      a: 2
+    })
+    expect(t.$options.a).toBe(2)
+    expect(t.$options.b).toBe(2)
+    // inheritance
+    var Test2 = Test.extend({
+      a: 2
+    })
+    expect(Test2.options.a).toBe(2)
+    expect(Test2.options.b).toBe(2)
+    var t2 = new Test2({
+      a: 3
+    })
+    expect(t2.$options.a).toBe(3)
+    expect(t2.$options.b).toBe(2)
+  })
+
+  it('use', function () {
+    var def = {}
+    var options = {}
+    var pluginStub = {
+      install: function (Vue, opts) {
+        Vue.directive('plugin-test', def)
+        expect(opts).toBe(options)
+      }
+    }
+    Vue.use(pluginStub, options)
+    expect(Vue.options.directives['plugin-test']).toBe(def)
+    delete Vue.options.directives['plugin-test']
+    // use a function
+    Vue.use(pluginStub.install, options)
+    expect(Vue.options.directives['plugin-test']).toBe(def)
+    delete Vue.options.directives['plugin-test']
+  })
+
+  describe('Asset registration', function () {
+
+    var Test = Vue.extend()
+    
+    it('directive / filter / partial / transition', function () {
+      [
+        'directive',
+        'filter',
+        'partial',
+        'transition'
+      ].forEach(function (type) {
+        var def = {}
+        Test[type]('test', def)
+        expect(Test.options[type + 's'].test).toBe(def)
+        expect(Test[type]('test')).toBe(def)
+        // extended registration should not pollute global
+        expect(Vue.options[type + 's'].test).toBeUndefined()
+      })
+    })
+
+    it('component', function () {
+      var def = { a: 1 }
+      Test.component('test', def)
+      var component = Test.options.components.test
+      expect(typeof component).toBe('function')
+      expect(component.super).toBe(Vue)
+      expect(component.options.a).toBe(1)
+      expect(Test.component('test')).toBe(component)
+      // already extended
+      Test.component('test2', component)
+      expect(Test.component('test2')).toBe(component)
+      // extended registration should not pollute global
+      expect(Vue.options.components.test).toBeUndefined()
+    })
+
+  })
+
+})