global_api_spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var Vue = require('src')
  2. var _ = require('src/util')
  3. var config = require('src/config')
  4. var transition = require('src/transition')
  5. describe('Global API', function () {
  6. it('exposed utilities', function () {
  7. expect(Vue.util).toBe(_)
  8. expect(Vue.nextTick).toBe(_.nextTick)
  9. expect(Vue.config).toBe(config)
  10. expect(Vue.transition.applyTransition).toBe(transition.applyTransition)
  11. })
  12. it('extend', function () {
  13. var Test = Vue.extend({
  14. name: 'test',
  15. a: 1,
  16. b: 2
  17. })
  18. expect(Test.options.a).toBe(1)
  19. expect(Test.options.b).toBe(2)
  20. expect(Test.super).toBe(Vue)
  21. // function.name is not available in IE
  22. expect(Test.toString().match(/^function Test\s?\(/)).toBeTruthy()
  23. var t = new Test({
  24. a: 2
  25. })
  26. expect(t.$options.a).toBe(2)
  27. expect(t.$options.b).toBe(2)
  28. // inheritance
  29. var Test2 = Test.extend({
  30. a: 2
  31. })
  32. expect(Test2.options.a).toBe(2)
  33. expect(Test2.options.b).toBe(2)
  34. var t2 = new Test2({
  35. a: 3
  36. })
  37. expect(t2.$options.a).toBe(3)
  38. expect(t2.$options.b).toBe(2)
  39. })
  40. it('extend warn invalid names', function () {
  41. Vue.extend({ name: '123' })
  42. expect('Invalid component name: "123"').toHaveBeenWarned()
  43. Vue.extend({ name: '_fesf' })
  44. expect('Invalid component name: "_fesf"').toHaveBeenWarned()
  45. Vue.extend({ name: 'Some App' })
  46. expect('Invalid component name: "Some App"').toHaveBeenWarned()
  47. })
  48. it('use', function () {
  49. var def = {}
  50. var options = {}
  51. var pluginStub = {
  52. install: function (Vue, opts) {
  53. Vue.directive('plugin-test', def)
  54. expect(opts).toBe(options)
  55. }
  56. }
  57. Vue.use(pluginStub, options)
  58. expect(Vue.options.directives['plugin-test']).toBe(def)
  59. delete Vue.options.directives['plugin-test']
  60. // use a function
  61. Vue.use(pluginStub.install, options)
  62. expect(Vue.options.directives['plugin-test']).toBe(def)
  63. delete Vue.options.directives['plugin-test']
  64. })
  65. it('global mixin', function () {
  66. var options = Vue.options
  67. var spy = jasmine.createSpy('global mixin')
  68. Vue.mixin({
  69. created: function () {
  70. spy(this.$options.myOption)
  71. }
  72. })
  73. new Vue({
  74. myOption: 'hello'
  75. })
  76. expect(spy).toHaveBeenCalledWith('hello')
  77. Vue.options = options
  78. })
  79. describe('Asset registration', function () {
  80. var Test = Vue.extend()
  81. it('directive / elementDirective / filter / transition', function () {
  82. var assets = ['directive', 'elementDirective', 'filter', 'transition']
  83. assets.forEach(function (type) {
  84. var def = {}
  85. Test[type]('test', def)
  86. expect(Test.options[type + 's'].test).toBe(def)
  87. expect(Test[type]('test')).toBe(def)
  88. // extended registration should not pollute global
  89. expect(Vue.options[type + 's'].test).toBeUndefined()
  90. })
  91. })
  92. it('component', function () {
  93. var def = { a: 1 }
  94. Test.component('test', def)
  95. var component = Test.options.components.test
  96. expect(typeof component).toBe('function')
  97. expect(component.super).toBe(Vue)
  98. expect(component.options.a).toBe(1)
  99. expect(component.options.name).toBe('test')
  100. expect(Test.component('test')).toBe(component)
  101. // already extended
  102. Test.component('test2', component)
  103. expect(Test.component('test2')).toBe(component)
  104. // extended registration should not pollute global
  105. expect(Vue.options.components.test).toBeUndefined()
  106. })
  107. })
  108. })