global_spec.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. describe('Asset registration', function () {
  56. var Test = Vue.extend()
  57. it('directive / elementDirective / filter / transition', function () {
  58. var assets = ['directive', 'elementDirective', 'filter', 'transition']
  59. assets.forEach(function (type) {
  60. var def = {}
  61. Test[type]('test', def)
  62. expect(Test.options[type + 's'].test).toBe(def)
  63. expect(Test[type]('test')).toBe(def)
  64. // extended registration should not pollute global
  65. expect(Vue.options[type + 's'].test).toBeUndefined()
  66. })
  67. })
  68. it('component', function () {
  69. var def = { a: 1 }
  70. Test.component('test', def)
  71. var component = Test.options.components.test
  72. expect(typeof component).toBe('function')
  73. expect(component.super).toBe(Vue)
  74. expect(component.options.a).toBe(1)
  75. expect(component.options.name).toBe('test')
  76. expect(Test.component('test')).toBe(component)
  77. // already extended
  78. Test.component('test2', component)
  79. expect(Test.component('test2')).toBe(component)
  80. // extended registration should not pollute global
  81. expect(Vue.options.components.test).toBeUndefined()
  82. })
  83. })
  84. })