global-api.spec.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import Vue from 'vue'
  2. describe('Global API', () => {
  3. it('extend', () => {
  4. const Test = Vue.extend({
  5. name: 'test',
  6. a: 1,
  7. b: 2
  8. })
  9. expect(Test.options.a).toBe(1)
  10. expect(Test.options.b).toBe(2)
  11. expect(Test.super).toBe(Vue)
  12. const t = new Test({
  13. a: 2
  14. })
  15. expect(t.$options.a).toBe(2)
  16. expect(t.$options.b).toBe(2)
  17. // inheritance
  18. const Test2 = Test.extend({
  19. a: 2
  20. })
  21. expect(Test2.options.a).toBe(2)
  22. expect(Test2.options.b).toBe(2)
  23. const t2 = new Test2({
  24. a: 3
  25. })
  26. expect(t2.$options.a).toBe(3)
  27. expect(t2.$options.b).toBe(2)
  28. })
  29. it('extend warn invalid names', () => {
  30. Vue.extend({ name: '123' })
  31. expect('Invalid component name: "123"').toHaveBeenWarned()
  32. Vue.extend({ name: '_fesf' })
  33. expect('Invalid component name: "_fesf"').toHaveBeenWarned()
  34. Vue.extend({ name: 'Some App' })
  35. expect('Invalid component name: "Some App"').toHaveBeenWarned()
  36. })
  37. it('Vue.extend works', () => {
  38. const foo = Vue.extend({
  39. template: '<span>foo</span>'
  40. })
  41. const bar = Vue.extend({
  42. template: '<span>bar</span>'
  43. })
  44. const vm = new Vue({
  45. el: document.createElement('div'),
  46. template: '<div><foo></foo><bar></bar></div>',
  47. components: { foo, bar }
  48. })
  49. expect(vm.$el.innerHTML).toBe('<span>foo</span><span>bar</span>')
  50. })
  51. it('global mixin', () => {
  52. const options = Vue.options
  53. const spy = jasmine.createSpy('global mixin')
  54. Vue.mixin({
  55. created: function () {
  56. spy(this.$options.myOption)
  57. }
  58. })
  59. new Vue({
  60. myOption: 'hello'
  61. })
  62. expect(spy).toHaveBeenCalledWith('hello')
  63. Vue.options = options
  64. })
  65. it('use', () => {
  66. const def = {}
  67. const options = {}
  68. const pluginStub = {
  69. install: (Vue, opts) => {
  70. Vue.directive('plugin-test', def)
  71. expect(opts).toBe(options)
  72. }
  73. }
  74. Vue.use(pluginStub, options)
  75. expect(Vue.options.directives['plugin-test']).toBe(def)
  76. delete Vue.options.directives['plugin-test']
  77. // use a function
  78. Vue.use(pluginStub.install, options)
  79. expect(Vue.options.directives['plugin-test']).toBe(def)
  80. delete Vue.options.directives['plugin-test']
  81. })
  82. it('compile', () => {
  83. const res = Vue.compile('<div><span>{{ msg }}</span></div>')
  84. const vm = new Vue({
  85. data: {
  86. msg: 'hello'
  87. },
  88. render: res.render,
  89. staticRenderFns: res.staticRenderFns
  90. })
  91. vm.$mount()
  92. expect(vm.$el.innerHTML).toContain('<span>hello</span>')
  93. })
  94. })