| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import { Component, ComponentClass, mixins } from '@vue/runtime-core'
- import { createInstance } from '@vue/runtime-test'
- const calls: string[] = []
- beforeEach(() => {
- calls.length = 0
- })
- class ClassMixinA extends Component<{ p1: string }, { d11: number }> {
- // props
- static props = {
- p1: String
- }
- // data
- d1 = 1
- data() {
- return {
- d11: 2
- }
- }
- // computed
- get c1() {
- return this.d1 + this.d11
- }
- // lifecycle
- created() {
- calls.push('created from mixin A')
- }
- // methods
- foo() {
- return this.d1
- }
- }
- class ClassMixinB extends Component<{ p2: string }, { d21: number }> {
- // props
- static props = {
- p2: String
- }
- // data
- d2 = 1
- data() {
- return {
- d21: 2
- }
- }
- get c2() {
- return this.d2 + this.d21
- }
- // lifecycle
- created() {
- calls.push('created from mixin B')
- }
- // methods
- bar() {
- return this.d2
- }
- }
- const ObjectMixinA = {
- props: {
- p1: String
- },
- data() {
- return {
- d1: 1,
- d11: 2
- }
- },
- computed: {
- c1() {
- return this.d1 + this.d11
- }
- },
- created() {
- calls.push('created from mixin A')
- },
- methods: {
- foo() {
- return this.d1
- }
- }
- }
- const ObjectMixinB = {
- props: {
- p2: String
- },
- data() {
- return {
- d2: 1,
- d21: 2
- }
- },
- computed: {
- c2() {
- return this.d2 + this.d21
- }
- },
- created() {
- calls.push('created from mixin B')
- },
- methods: {
- bar() {
- return this.d2
- }
- }
- }
- function assertMixins(Test: any) {
- const instance = createInstance(Test, {
- p1: '1',
- p2: '2',
- p3: '3'
- }) as any
- // data
- expect(instance.d1).toBe(1)
- expect(instance.d11).toBe(2)
- expect(instance.d2).toBe(1)
- expect(instance.d21).toBe(2)
- expect(instance.d3).toBe(1)
- expect(instance.d31).toBe(2)
- // props
- expect(instance.p1).toBe('1')
- expect(instance.p2).toBe('2')
- expect(instance.p3).toBe('3')
- expect(instance.$props.p1).toBe('1')
- expect(instance.$props.p2).toBe('2')
- expect(instance.$props.p3).toBe('3')
- // computed
- expect(instance.c1).toBe(3)
- expect(instance.c2).toBe(3)
- expect(instance.c3).toBe(3)
- // lifecycle
- expect(calls).toEqual([
- 'created from mixin A',
- 'created from mixin B',
- 'created from Test'
- ])
- // methods
- expect(instance.foo()).toBe(1)
- expect(instance.bar()).toBe(1)
- expect(instance.baz()).toBe(1)
- }
- describe('mixins', () => {
- it('should work with classes', () => {
- class Test extends mixins(ClassMixinA, ClassMixinB)<
- { p3: string },
- { d31: number }
- > {
- static props = {
- p3: String
- }
- d3 = 1
- data(): any {
- return {
- d31: 2
- }
- }
- get c3() {
- return this.d3 + this.d31
- }
- created() {
- calls.push('created from Test')
- }
- baz() {
- return this.d3
- }
- }
- const instance = createInstance(Test, {
- p1: '1',
- p2: '2',
- p3: '3'
- })
- // we duplicate the assertions because they serve as type tests as well
- // data
- expect(instance.d1).toBe(1)
- expect(instance.d11).toBe(2)
- expect(instance.d2).toBe(1)
- expect(instance.d21).toBe(2)
- expect(instance.d3).toBe(1)
- expect(instance.d31).toBe(2)
- // props
- expect(instance.p1).toBe('1')
- expect(instance.p2).toBe('2')
- expect(instance.p3).toBe('3')
- expect(instance.$props.p1).toBe('1')
- expect(instance.$props.p2).toBe('2')
- expect(instance.$props.p3).toBe('3')
- // computed
- expect(instance.c1).toBe(3)
- expect(instance.c2).toBe(3)
- expect(instance.c3).toBe(3)
- // lifecycle
- expect(calls).toEqual([
- 'created from mixin A',
- 'created from mixin B',
- 'created from Test'
- ])
- // methods
- expect(instance.foo()).toBe(1)
- expect(instance.bar()).toBe(1)
- expect(instance.baz()).toBe(1)
- })
- it('should work with objects', () => {
- class Test extends ((mixins as any)(
- ObjectMixinA,
- ObjectMixinB
- ) as ComponentClass)<{ p3: string }, { d31: number }> {
- static props = {
- p3: String
- }
- d3 = 1
- data(): any {
- return {
- d31: 2
- }
- }
- get c3() {
- return this.d3 + this.d31
- }
- created() {
- calls.push('created from Test')
- }
- baz() {
- return this.d3
- }
- }
- assertMixins(Test)
- })
- it('should work with a mix of objects and classes', () => {
- class Test extends ((mixins as any)(
- ClassMixinA,
- ObjectMixinB
- ) as ComponentClass)<{ p3: string }, { d31: number }> {
- static props = {
- p3: String
- }
- d3 = 1
- data(): any {
- return {
- d31: 2
- }
- }
- get c3() {
- return this.d3 + this.d31
- }
- created() {
- calls.push('created from Test')
- }
- baz() {
- return this.d3
- }
- }
- assertMixins(Test)
- })
- })
|