global_api_spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. var t = new Test({
  22. a: 2
  23. })
  24. expect(t.$options.a).toBe(2)
  25. expect(t.$options.b).toBe(2)
  26. // inheritance
  27. var Test2 = Test.extend({
  28. a: 2
  29. })
  30. expect(Test2.options.a).toBe(2)
  31. expect(Test2.options.b).toBe(2)
  32. var t2 = new Test2({
  33. a: 3
  34. })
  35. expect(t2.$options.a).toBe(3)
  36. expect(t2.$options.b).toBe(2)
  37. })
  38. it('extend warn invalid names', function () {
  39. Vue.extend({ name: '123' })
  40. expect('Invalid component name: "123"').toHaveBeenWarned()
  41. Vue.extend({ name: '_fesf' })
  42. expect('Invalid component name: "_fesf"').toHaveBeenWarned()
  43. Vue.extend({ name: 'Some App' })
  44. expect('Invalid component name: "Some App"').toHaveBeenWarned()
  45. })
  46. it('use', function () {
  47. var def = {}
  48. var options = {}
  49. var pluginStub = {
  50. install: function (Vue, opts) {
  51. Vue.directive('plugin-test', def)
  52. expect(opts).toBe(options)
  53. }
  54. }
  55. Vue.use(pluginStub, options)
  56. expect(Vue.options.directives['plugin-test']).toBe(def)
  57. delete Vue.options.directives['plugin-test']
  58. // use a function
  59. Vue.use(pluginStub.install, options)
  60. expect(Vue.options.directives['plugin-test']).toBe(def)
  61. delete Vue.options.directives['plugin-test']
  62. })
  63. it('global mixin', function () {
  64. var options = Vue.options
  65. var spy = jasmine.createSpy('global mixin')
  66. Vue.mixin({
  67. created: function () {
  68. spy(this.$options.myOption)
  69. }
  70. })
  71. new Vue({
  72. myOption: 'hello'
  73. })
  74. expect(spy).toHaveBeenCalledWith('hello')
  75. Vue.options = options
  76. })
  77. describe('Asset registration', function () {
  78. var Test = Vue.extend()
  79. it('directive / elementDirective / filter / transition', function () {
  80. var assets = ['directive', 'elementDirective', 'filter', 'transition']
  81. assets.forEach(function (type) {
  82. var def = {}
  83. Test[type]('test', def)
  84. expect(Test.options[type + 's'].test).toBe(def)
  85. expect(Test[type]('test')).toBe(def)
  86. // extended registration should not pollute global
  87. expect(Vue.options[type + 's'].test).toBeUndefined()
  88. })
  89. })
  90. it('component', function () {
  91. var def = { a: 1 }
  92. Test.component('test', def)
  93. var component = Test.options.components.test
  94. expect(typeof component).toBe('function')
  95. expect(component.super).toBe(Vue)
  96. expect(component.options.a).toBe(1)
  97. expect(component.options.name).toBe('test')
  98. expect(Test.component('test')).toBe(component)
  99. // already extended
  100. Test.component('test2', component)
  101. expect(Test.component('test2')).toBe(component)
  102. // extended registration should not pollute global
  103. expect(Vue.options.components.test).toBeUndefined()
  104. })
  105. // GitHub issue #3039
  106. it('component with `name` option', function () {
  107. var def = { name: 'Component1' }
  108. Test.component('ns-tree', def)
  109. var component = Test.options.components['ns-tree']
  110. expect(typeof component).toBe('function')
  111. expect(component.options.name).toBe('Component1')
  112. })
  113. })
  114. })