global-api.spec.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // function.name is not available in IE
  13. expect(Test.toString().match(/^function Test\s?\(/)).toBeTruthy()
  14. const t = new Test({
  15. a: 2
  16. })
  17. expect(t.$options.a).toBe(2)
  18. expect(t.$options.b).toBe(2)
  19. // inheritance
  20. const Test2 = Test.extend({
  21. a: 2
  22. })
  23. expect(Test2.options.a).toBe(2)
  24. expect(Test2.options.b).toBe(2)
  25. const t2 = new Test2({
  26. a: 3
  27. })
  28. expect(t2.$options.a).toBe(3)
  29. expect(t2.$options.b).toBe(2)
  30. })
  31. it('extend warn invalid names', () => {
  32. Vue.extend({ name: '123' })
  33. expect('Invalid component name: "123"').toHaveBeenWarned()
  34. Vue.extend({ name: '_fesf' })
  35. expect('Invalid component name: "_fesf"').toHaveBeenWarned()
  36. Vue.extend({ name: 'Some App' })
  37. expect('Invalid component name: "Some App"').toHaveBeenWarned()
  38. })
  39. it('Vue.extend works', () => {
  40. const foo = Vue.extend({
  41. template: '<span>foo</span>'
  42. })
  43. const bar = Vue.extend({
  44. template: '<span>bar</span>'
  45. })
  46. const vm = new Vue({
  47. el: document.createElement('div'),
  48. template: '<div><foo></foo><bar></bar></div>',
  49. components: { foo, bar }
  50. })
  51. expect(vm.$el.innerHTML).toBe('<span>foo</span><span>bar</span>')
  52. })
  53. it('global mixin', () => {
  54. const options = Vue.options
  55. const 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. it('use', () => {
  68. const def = {}
  69. const options = {}
  70. const pluginStub = {
  71. install: (Vue, opts) => {
  72. Vue.directive('plugin-test', def)
  73. expect(opts).toBe(options)
  74. }
  75. }
  76. Vue.use(pluginStub, options)
  77. expect(Vue.options.directives['plugin-test']).toBe(def)
  78. delete Vue.options.directives['plugin-test']
  79. // use a function
  80. Vue.use(pluginStub.install, options)
  81. expect(Vue.options.directives['plugin-test']).toBe(def)
  82. delete Vue.options.directives['plugin-test']
  83. })
  84. })