global-api.spec.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. template: '<div><foo></foo><bar></bar></div>',
  46. components: { foo, bar }
  47. }).$mount()
  48. expect(vm.$el.innerHTML).toBe('<span>foo</span><span>bar</span>')
  49. })
  50. it('global mixin', () => {
  51. const options = Vue.options
  52. const spy = jasmine.createSpy('global mixin')
  53. Vue.mixin({
  54. created: function () {
  55. spy(this.$options.myOption)
  56. }
  57. })
  58. new Vue({
  59. myOption: 'hello'
  60. })
  61. expect(spy).toHaveBeenCalledWith('hello')
  62. Vue.options = options
  63. })
  64. it('use', () => {
  65. const def = {}
  66. const options = {}
  67. const pluginStub = {
  68. install: (Vue, opts) => {
  69. Vue.directive('plugin-test', def)
  70. expect(opts).toBe(options)
  71. }
  72. }
  73. Vue.use(pluginStub, options)
  74. expect(Vue.options.directives['plugin-test']).toBe(def)
  75. delete Vue.options.directives['plugin-test']
  76. // use a function
  77. Vue.use(pluginStub.install, options)
  78. expect(Vue.options.directives['plugin-test']).toBe(def)
  79. delete Vue.options.directives['plugin-test']
  80. })
  81. it('compile', () => {
  82. const res = Vue.compile('<div><span>{{ msg }}</span></div>')
  83. const vm = new Vue({
  84. data: {
  85. msg: 'hello'
  86. },
  87. render: res.render,
  88. staticRenderFns: res.staticRenderFns
  89. }).$mount()
  90. expect(vm.$el.innerHTML).toContain('<span>hello</span>')
  91. })
  92. })