mixins.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { Component, ComponentClass, mixins } from '@vue/runtime-core'
  2. import { createInstance } from '@vue/runtime-test'
  3. const calls: string[] = []
  4. beforeEach(() => {
  5. calls.length = 0
  6. })
  7. class ClassMixinA extends Component<{ p1: string }, { d11: number }> {
  8. // props
  9. static props = {
  10. p1: String
  11. }
  12. // data
  13. d1 = 1
  14. data() {
  15. return {
  16. d11: 2
  17. }
  18. }
  19. // computed
  20. get c1() {
  21. return this.d1 + this.d11
  22. }
  23. // lifecycle
  24. created() {
  25. calls.push('created from mixin A')
  26. }
  27. // methods
  28. foo() {
  29. return this.d1
  30. }
  31. }
  32. class ClassMixinB extends Component<{ p2: string }, { d21: number }> {
  33. // props
  34. static props = {
  35. p2: String
  36. }
  37. // data
  38. d2 = 1
  39. data() {
  40. return {
  41. d21: 2
  42. }
  43. }
  44. get c2() {
  45. return this.d2 + this.d21
  46. }
  47. // lifecycle
  48. created() {
  49. calls.push('created from mixin B')
  50. }
  51. // methods
  52. bar() {
  53. return this.d2
  54. }
  55. }
  56. const ObjectMixinA = {
  57. props: {
  58. p1: String
  59. },
  60. data() {
  61. return {
  62. d1: 1,
  63. d11: 2
  64. }
  65. },
  66. computed: {
  67. c1() {
  68. return this.d1 + this.d11
  69. }
  70. },
  71. created() {
  72. calls.push('created from mixin A')
  73. },
  74. methods: {
  75. foo() {
  76. return this.d1
  77. }
  78. }
  79. }
  80. const ObjectMixinB = {
  81. props: {
  82. p2: String
  83. },
  84. data() {
  85. return {
  86. d2: 1,
  87. d21: 2
  88. }
  89. },
  90. computed: {
  91. c2() {
  92. return this.d2 + this.d21
  93. }
  94. },
  95. created() {
  96. calls.push('created from mixin B')
  97. },
  98. methods: {
  99. bar() {
  100. return this.d2
  101. }
  102. }
  103. }
  104. function assertMixins(Test: any) {
  105. const instance = createInstance(Test, {
  106. p1: '1',
  107. p2: '2',
  108. p3: '3'
  109. }) as any
  110. // data
  111. expect(instance.d1).toBe(1)
  112. expect(instance.d11).toBe(2)
  113. expect(instance.d2).toBe(1)
  114. expect(instance.d21).toBe(2)
  115. expect(instance.d3).toBe(1)
  116. expect(instance.d31).toBe(2)
  117. // props
  118. expect(instance.p1).toBe('1')
  119. expect(instance.p2).toBe('2')
  120. expect(instance.p3).toBe('3')
  121. expect(instance.$props.p1).toBe('1')
  122. expect(instance.$props.p2).toBe('2')
  123. expect(instance.$props.p3).toBe('3')
  124. // computed
  125. expect(instance.c1).toBe(3)
  126. expect(instance.c2).toBe(3)
  127. expect(instance.c3).toBe(3)
  128. // lifecycle
  129. expect(calls).toEqual([
  130. 'created from mixin A',
  131. 'created from mixin B',
  132. 'created from Test'
  133. ])
  134. // methods
  135. expect(instance.foo()).toBe(1)
  136. expect(instance.bar()).toBe(1)
  137. expect(instance.baz()).toBe(1)
  138. }
  139. describe('mixins', () => {
  140. it('should work with classes', () => {
  141. class Test extends mixins(ClassMixinA, ClassMixinB)<
  142. { p3: string },
  143. { d31: number }
  144. > {
  145. static props = {
  146. p3: String
  147. }
  148. d3 = 1
  149. data(): any {
  150. return {
  151. d31: 2
  152. }
  153. }
  154. get c3() {
  155. return this.d3 + this.d31
  156. }
  157. created() {
  158. calls.push('created from Test')
  159. }
  160. baz() {
  161. return this.d3
  162. }
  163. }
  164. const instance = createInstance(Test, {
  165. p1: '1',
  166. p2: '2',
  167. p3: '3'
  168. })
  169. // we duplicate the assertions because they serve as type tests as well
  170. // data
  171. expect(instance.d1).toBe(1)
  172. expect(instance.d11).toBe(2)
  173. expect(instance.d2).toBe(1)
  174. expect(instance.d21).toBe(2)
  175. expect(instance.d3).toBe(1)
  176. expect(instance.d31).toBe(2)
  177. // props
  178. expect(instance.p1).toBe('1')
  179. expect(instance.p2).toBe('2')
  180. expect(instance.p3).toBe('3')
  181. expect(instance.$props.p1).toBe('1')
  182. expect(instance.$props.p2).toBe('2')
  183. expect(instance.$props.p3).toBe('3')
  184. // computed
  185. expect(instance.c1).toBe(3)
  186. expect(instance.c2).toBe(3)
  187. expect(instance.c3).toBe(3)
  188. // lifecycle
  189. expect(calls).toEqual([
  190. 'created from mixin A',
  191. 'created from mixin B',
  192. 'created from Test'
  193. ])
  194. // methods
  195. expect(instance.foo()).toBe(1)
  196. expect(instance.bar()).toBe(1)
  197. expect(instance.baz()).toBe(1)
  198. })
  199. it('should work with objects', () => {
  200. class Test extends ((mixins as any)(
  201. ObjectMixinA,
  202. ObjectMixinB
  203. ) as ComponentClass)<{ p3: string }, { d31: number }> {
  204. static props = {
  205. p3: String
  206. }
  207. d3 = 1
  208. data(): any {
  209. return {
  210. d31: 2
  211. }
  212. }
  213. get c3() {
  214. return this.d3 + this.d31
  215. }
  216. created() {
  217. calls.push('created from Test')
  218. }
  219. baz() {
  220. return this.d3
  221. }
  222. }
  223. assertMixins(Test)
  224. })
  225. it('should work with a mix of objects and classes', () => {
  226. class Test extends ((mixins as any)(
  227. ClassMixinA,
  228. ObjectMixinB
  229. ) as ComponentClass)<{ p3: string }, { d31: number }> {
  230. static props = {
  231. p3: String
  232. }
  233. d3 = 1
  234. data(): any {
  235. return {
  236. d31: 2
  237. }
  238. }
  239. get c3() {
  240. return this.d3 + this.d31
  241. }
  242. created() {
  243. calls.push('created from Test')
  244. }
  245. baz() {
  246. return this.d3
  247. }
  248. }
  249. assertMixins(Test)
  250. })
  251. })