global_spec.js 3.4 KB

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