vue2/test/unit/specs/global_api_spec.js

117 lines
3.4 KiB
JavaScript
Raw Normal View History

var Vue = require('src')
var _ = require('src/util')
var config = require('src/config')
2016-02-25 00:33:54 +08:00
var transition = require('src/transition')
2014-09-05 22:03:44 +08:00
describe('Global API', function () {
it('exposed utilities', function () {
expect(Vue.util).toBe(_)
expect(Vue.nextTick).toBe(_.nextTick)
expect(Vue.config).toBe(config)
2016-02-25 00:33:54 +08:00
expect(Vue.transition.applyTransition).toBe(transition.applyTransition)
2014-09-05 22:03:44 +08:00
})
it('extend', function () {
var Test = Vue.extend({
name: 'test',
2014-09-05 22:03:44 +08:00
a: 1,
b: 2
})
expect(Test.options.a).toBe(1)
expect(Test.options.b).toBe(2)
expect(Test.super).toBe(Vue)
2014-09-27 09:17:03 +08:00
// function.name is not available in IE
expect(Test.toString().match(/^function Test\s?\(/)).toBeTruthy()
2014-09-05 22:03:44 +08:00
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('extend warn invalid names', function () {
Vue.extend({ name: '123' })
2016-03-10 06:32:05 +08:00
expect('Invalid component name: "123"').toHaveBeenWarned()
Vue.extend({ name: '_fesf' })
2016-03-10 06:32:05 +08:00
expect('Invalid component name: "_fesf"').toHaveBeenWarned()
Vue.extend({ name: 'Some App' })
2016-03-10 06:32:05 +08:00
expect('Invalid component name: "Some App"').toHaveBeenWarned()
})
2014-09-05 22:03:44 +08:00
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']
})
2015-09-23 22:31:48 +08:00
it('global mixin', function () {
var options = Vue.options
var spy = jasmine.createSpy('global mixin')
Vue.mixin({
created: function () {
spy(this.$options.myOption)
}
})
new Vue({
myOption: 'hello'
})
expect(spy).toHaveBeenCalledWith('hello')
Vue.options = options
})
2014-09-05 22:03:44 +08:00
describe('Asset registration', function () {
var Test = Vue.extend()
2015-07-02 20:59:20 +08:00
2015-05-16 01:44:27 +08:00
it('directive / elementDirective / filter / transition', function () {
2015-08-10 05:38:02 +08:00
var assets = ['directive', 'elementDirective', 'filter', 'transition']
assets.forEach(function (type) {
2014-09-05 22:03:44 +08:00
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(component.options.name).toBe('test')
2014-09-05 22:03:44 +08:00
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()
})
})
2015-07-02 20:59:20 +08:00
})