/
mixins.spec.js
144 lines (131 loc) 路 2.91 KB
/
mixins.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import Vue from 'vue'
import { mergeOptions } from 'core/util/index'
describe('Options mixins', () => {
it('vm should have options from mixin', () => {
const mixin = {
directives: {
c: {}
},
methods: {
a: function () {}
}
}
const vm = new Vue({
mixins: [mixin],
methods: {
b: function () {}
}
})
expect(vm.a).toBeDefined()
expect(vm.b).toBeDefined()
expect(vm.$options.directives.c).toBeDefined()
})
it('should call hooks from mixins first', () => {
const a = {}
const b = {}
const c = {}
const f1 = function () {}
const f2 = function () {}
const f3 = function () {}
const mixinA = {
a: 1,
template: 'foo',
directives: {
a: a
},
created: f1
}
const mixinB = {
b: 1,
directives: {
b: b
},
created: f2
}
const result = mergeOptions({}, {
directives: {
c: c
},
template: 'bar',
mixins: [mixinA, mixinB],
created: f3
})
expect(result.a).toBe(1)
expect(result.b).toBe(1)
expect(result.directives.a).toBe(a)
expect(result.directives.b).toBe(b)
expect(result.directives.c).toBe(c)
expect(result.created[0]).toBe(f1)
expect(result.created[1]).toBe(f2)
expect(result.created[2]).toBe(f3)
expect(result.template).toBe('bar')
})
it('mixin methods should not override defined method', () => {
const f1 = function () {}
const f2 = function () {}
const f3 = function () {}
const mixinA = {
methods: {
xyz: f1
}
}
const mixinB = {
methods: {
xyz: f2
}
}
const result = mergeOptions({}, {
mixins: [mixinA, mixinB],
methods: {
xyz: f3
}
})
expect(result.methods.xyz).toBe(f3)
})
it('should accept constructors as mixins', () => {
const mixin = Vue.extend({
directives: {
c: {}
},
methods: {
a: function () {}
}
})
const vm = new Vue({
mixins: [mixin],
methods: {
b: function () {}
}
})
expect(vm.a).toBeDefined()
expect(vm.b).toBeDefined()
expect(vm.$options.directives.c).toBeDefined()
})
it('should accept further extended constructors as mixins', () => {
const spy1 = jasmine.createSpy('mixinA')
const spy2 = jasmine.createSpy('mixinB')
const mixinA = Vue.extend({
created: spy1,
directives: {
c: {}
},
methods: {
a: function () {}
}
})
const mixinB = mixinA.extend({
created: spy2
})
const vm = new Vue({
mixins: [mixinB],
methods: {
b: function () {}
}
})
expect(spy1).toHaveBeenCalledTimes(1)
expect(spy2).toHaveBeenCalledTimes(1)
expect(vm.a).toBeDefined()
expect(vm.b).toBeDefined()
expect(vm.$options.directives.c).toBeDefined()
})
})