mixins.spec.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import Vue from 'vue'
  2. const mergeOptions = Vue.util.mergeOptions
  3. describe('Options mixins', () => {
  4. it('vm should have options from mixin', () => {
  5. const mixin = {
  6. directives: {
  7. c: {}
  8. },
  9. methods: {
  10. a: function () {}
  11. }
  12. }
  13. const vm = new Vue({
  14. mixins: [mixin],
  15. methods: {
  16. b: function () {}
  17. }
  18. }).$mount()
  19. expect(vm.a).toBeDefined()
  20. expect(vm.b).toBeDefined()
  21. expect(vm.$options.directives.c).toBeDefined()
  22. })
  23. it('should call hooks from mixins first', () => {
  24. const a = {}
  25. const b = {}
  26. const c = {}
  27. const f1 = function () {}
  28. const f2 = function () {}
  29. const f3 = function () {}
  30. const mixinA = {
  31. a: 1,
  32. template: 'foo',
  33. directives: {
  34. a: a
  35. },
  36. created: f1
  37. }
  38. const mixinB = {
  39. b: 1,
  40. directives: {
  41. b: b
  42. },
  43. created: f2
  44. }
  45. const result = mergeOptions({}, {
  46. directives: {
  47. c: c
  48. },
  49. template: 'bar',
  50. mixins: [mixinA, mixinB],
  51. created: f3
  52. })
  53. expect(result.a).toBe(1)
  54. expect(result.b).toBe(1)
  55. expect(result.directives.a).toBe(a)
  56. expect(result.directives.b).toBe(b)
  57. expect(result.directives.c).toBe(c)
  58. expect(result.created[0]).toBe(f1)
  59. expect(result.created[1]).toBe(f2)
  60. expect(result.created[2]).toBe(f3)
  61. expect(result.template).toBe('bar')
  62. })
  63. it('mixin methods should not override defined method', () => {
  64. const f1 = function () {}
  65. const f2 = function () {}
  66. const f3 = function () {}
  67. const mixinA = {
  68. methods: {
  69. xyz: f1
  70. }
  71. }
  72. const mixinB = {
  73. methods: {
  74. xyz: f2
  75. }
  76. }
  77. const result = mergeOptions({}, {
  78. mixins: [mixinA, mixinB],
  79. methods: {
  80. xyz: f3
  81. }
  82. })
  83. expect(result.methods.xyz).toBe(f3)
  84. })
  85. it('should accept constructors as mixins', () => {
  86. const mixin = Vue.extend({
  87. directives: {
  88. c: {}
  89. },
  90. methods: {
  91. a: function () {}
  92. }
  93. })
  94. const vm = new Vue({
  95. mixins: [mixin],
  96. methods: {
  97. b: function () {}
  98. }
  99. }).$mount()
  100. expect(vm.a).toBeDefined()
  101. expect(vm.b).toBeDefined()
  102. expect(vm.$options.directives.c).toBeDefined()
  103. })
  104. })