global_spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var Vue = require('../../../../src/vue')
  2. var _ = require('../../../../src/util')
  3. var config = require('../../../../src/config')
  4. describe('Global API', function () {
  5. it('exposed utilities', function () {
  6. expect(Vue.util).toBe(_)
  7. expect(Vue.nextTick).toBe(_.nextTick)
  8. expect(Vue.config).toBe(config)
  9. })
  10. it('extend', function () {
  11. var Test = Vue.extend({
  12. name: 'test',
  13. a: 1,
  14. b: 2
  15. })
  16. expect(Test.options.a).toBe(1)
  17. expect(Test.options.b).toBe(2)
  18. expect(Test.super).toBe(Vue)
  19. // function.name is not available in IE
  20. expect(Test.toString().match(/^function Test\s?\(/)).toBeTruthy()
  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('use', function () {
  39. var def = {}
  40. var options = {}
  41. var pluginStub = {
  42. install: function (Vue, opts) {
  43. Vue.directive('plugin-test', def)
  44. expect(opts).toBe(options)
  45. }
  46. }
  47. Vue.use(pluginStub, options)
  48. expect(Vue.options.directives['plugin-test']).toBe(def)
  49. delete Vue.options.directives['plugin-test']
  50. // use a function
  51. Vue.use(pluginStub.install, options)
  52. expect(Vue.options.directives['plugin-test']).toBe(def)
  53. delete Vue.options.directives['plugin-test']
  54. })
  55. it('global mixin', function () {
  56. var options = Vue.options
  57. var spy = jasmine.createSpy('global mixin')
  58. Vue.mixin({
  59. created: function () {
  60. spy(this.$options.myOption)
  61. }
  62. })
  63. new Vue({
  64. myOption: 'hello'
  65. })
  66. expect(spy).toHaveBeenCalledWith('hello')
  67. Vue.options = options
  68. })
  69. describe('Asset registration', function () {
  70. var Test = Vue.extend()
  71. it('directive / elementDirective / filter / transition', function () {
  72. var assets = ['directive', 'elementDirective', 'filter', 'transition']
  73. assets.forEach(function (type) {
  74. var def = {}
  75. Test[type]('test', def)
  76. expect(Test.options[type + 's'].test).toBe(def)
  77. expect(Test[type]('test')).toBe(def)
  78. // extended registration should not pollute global
  79. expect(Vue.options[type + 's'].test).toBeUndefined()
  80. })
  81. })
  82. it('component', function () {
  83. var def = { a: 1 }
  84. Test.component('test', def)
  85. var component = Test.options.components.test
  86. expect(typeof component).toBe('function')
  87. expect(component.super).toBe(Vue)
  88. expect(component.options.a).toBe(1)
  89. expect(component.options.name).toBe('test')
  90. expect(Test.component('test')).toBe(component)
  91. // already extended
  92. Test.component('test2', component)
  93. expect(Test.component('test2')).toBe(component)
  94. // extended registration should not pollute global
  95. expect(Vue.options.components.test).toBeUndefined()
  96. })
  97. })
  98. })