global_api_spec.js 3.5 KB

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