global_spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. var t = new Test({
  20. a: 2
  21. })
  22. expect(t.$options.a).toBe(2)
  23. expect(t.$options.b).toBe(2)
  24. // inheritance
  25. var Test2 = Test.extend({
  26. a: 2
  27. })
  28. expect(Test2.options.a).toBe(2)
  29. expect(Test2.options.b).toBe(2)
  30. var t2 = new Test2({
  31. a: 3
  32. })
  33. expect(t2.$options.a).toBe(3)
  34. expect(t2.$options.b).toBe(2)
  35. })
  36. it('use', function () {
  37. var def = {}
  38. var options = {}
  39. var pluginStub = {
  40. install: function (Vue, opts) {
  41. Vue.directive('plugin-test', def)
  42. expect(opts).toBe(options)
  43. }
  44. }
  45. Vue.use(pluginStub, options)
  46. expect(Vue.options.directives['plugin-test']).toBe(def)
  47. delete Vue.options.directives['plugin-test']
  48. // use a function
  49. Vue.use(pluginStub.install, options)
  50. expect(Vue.options.directives['plugin-test']).toBe(def)
  51. delete Vue.options.directives['plugin-test']
  52. })
  53. it('global mixin', function () {
  54. var options = Vue.options
  55. var spy = jasmine.createSpy('global mixin')
  56. Vue.mixin({
  57. created: function () {
  58. spy(this.$options.myOption)
  59. }
  60. })
  61. new Vue({
  62. myOption: 'hello'
  63. })
  64. expect(spy).toHaveBeenCalledWith('hello')
  65. Vue.options = options
  66. })
  67. describe('Asset registration', function () {
  68. var Test = Vue.extend()
  69. it('directive / elementDirective / filter / transition', function () {
  70. var assets = ['directive', 'elementDirective', 'filter', 'transition']
  71. assets.forEach(function (type) {
  72. var def = {}
  73. Test[type]('test', def)
  74. expect(Test.options[type + 's'].test).toBe(def)
  75. expect(Test[type]('test')).toBe(def)
  76. // extended registration should not pollute global
  77. expect(Vue.options[type + 's'].test).toBeUndefined()
  78. })
  79. })
  80. it('component', function () {
  81. var def = { a: 1 }
  82. Test.component('test', def)
  83. var component = Test.options.components.test
  84. expect(typeof component).toBe('function')
  85. expect(component.super).toBe(Vue)
  86. expect(component.options.a).toBe(1)
  87. expect(component.options.name).toBe('test')
  88. expect(Test.component('test')).toBe(component)
  89. // already extended
  90. Test.component('test2', component)
  91. expect(Test.component('test2')).toBe(component)
  92. // extended registration should not pollute global
  93. expect(Vue.options.components.test).toBeUndefined()
  94. })
  95. })
  96. })