mixins.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import Vue from 'vue'
  2. import { mergeOptions } from 'core/util/index'
  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. })
  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. {},
  47. {
  48. directives: {
  49. c: c
  50. },
  51. template: 'bar',
  52. mixins: [mixinA, mixinB],
  53. created: f3
  54. }
  55. )
  56. expect(result.a).toBe(1)
  57. expect(result.b).toBe(1)
  58. expect(result.directives?.a).toBe(a)
  59. expect(result.directives?.b).toBe(b)
  60. expect(result.directives?.c).toBe(c)
  61. expect(result.created?.[0]).toBe(f1)
  62. expect(result.created?.[1]).toBe(f2)
  63. expect(result.created?.[2]).toBe(f3)
  64. expect(result.template).toBe('bar')
  65. })
  66. it('mixin methods should not override defined method', () => {
  67. const f1 = function () {}
  68. const f2 = function () {}
  69. const f3 = function () {}
  70. const mixinA = {
  71. methods: {
  72. xyz: f1
  73. }
  74. }
  75. const mixinB = {
  76. methods: {
  77. xyz: f2
  78. }
  79. }
  80. const result = mergeOptions(
  81. {},
  82. {
  83. mixins: [mixinA, mixinB],
  84. methods: {
  85. xyz: f3
  86. }
  87. }
  88. )
  89. expect(result.methods?.xyz).toBe(f3)
  90. })
  91. it('should accept constructors as mixins', () => {
  92. const mixin = Vue.extend({
  93. directives: {
  94. c: {}
  95. },
  96. methods: {
  97. a: function () {}
  98. }
  99. })
  100. const vm = new Vue({
  101. mixins: [mixin],
  102. methods: {
  103. b: function () {}
  104. }
  105. })
  106. expect(vm.a).toBeDefined()
  107. expect(vm.b).toBeDefined()
  108. expect(vm.$options.directives.c).toBeDefined()
  109. })
  110. it('should accept further extended constructors as mixins', () => {
  111. const spy1 = vi.fn()
  112. const spy2 = vi.fn()
  113. const mixinA = Vue.extend({
  114. created: spy1,
  115. directives: {
  116. c: {}
  117. },
  118. methods: {
  119. a: function () {}
  120. }
  121. })
  122. const mixinB = mixinA.extend({
  123. created: spy2
  124. })
  125. const vm = new Vue({
  126. mixins: [mixinB],
  127. methods: {
  128. b: function () {}
  129. }
  130. })
  131. expect(spy1).toHaveBeenCalledTimes(1)
  132. expect(spy2).toHaveBeenCalledTimes(1)
  133. expect(vm.a).toBeDefined()
  134. expect(vm.b).toBeDefined()
  135. expect(vm.$options.directives.c).toBeDefined()
  136. })
  137. })