-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_spec.js
275 lines (245 loc) · 5.89 KB
/
state_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var Vue = require('../../../../src/vue')
describe('Instance state initialization', function () {
describe('data proxy', function () {
var data = {
a: 0,
b: 0
}
var vm = new Vue({
data: data
})
it('initial', function () {
expect(vm.a).toBe(data.a)
expect(vm.b).toBe(data.b)
})
it('vm => data', function () {
vm.a = 1
expect(data.a).toBe(1)
expect(vm.a).toBe(data.a)
})
it('data => vm', function () {
data.b = 2
expect(vm.b).toBe(2)
expect(vm.b).toBe(data.b)
})
})
describe('$data', function () {
it('should initialize props', function () {
var vm = new Vue({
el: document.createElement('div'),
props: ['c']
})
expect(vm.hasOwnProperty('c')).toBe(true)
})
it('should use default prop value if prop not provided', function () {
var vm = new Vue({
el: document.createElement('div'),
props: ['c'],
data: {
c: 1
}
})
expect(vm.c).toBe(1)
})
it('external prop should overwrite default value', function () {
var el = document.createElement('div')
el.setAttribute('v-bind:c', '2')
el.textContent = '{{c}}'
var vm = new Vue({
el: el,
props: ['c'],
data: {
c: 1
}
})
expect(vm.c).toBe(2)
expect(el.textContent).toBe('2')
})
it('props should be available in data() and create()', function () {
var el = document.createElement('div')
el.setAttribute(':c', '2')
var vm = new Vue({
el: el,
props: ['c'],
data: function () {
expect(this.c).toBe(2)
expect(this._data.c).toBe(2)
return {
d: this.c + 1
}
},
created: function () {
expect(this.c).toBe(2)
expect(this._data.c).toBe(2)
}
})
expect(vm.d).toBe(3)
})
it('replace $data', function () {
var vm = new Vue({
data: {
a: 1
}
})
vm.$data = { b: 2 }
// proxy new key
expect(vm.b).toBe(2)
// unproxy old key that's no longer present
expect(vm.hasOwnProperty('a')).toBe(false)
})
})
describe('computed', function () {
var spyE = jasmine.createSpy('computed e')
var spyF = jasmine.createSpy('cached computed f')
var spyCachedWatcher = jasmine.createSpy('cached computed watcher')
var Test = Vue.extend({
computed: {
// uncached
c: {
cache: false,
get: function () {
return this.a + this.b
}
},
// with setter
d: {
get: function () {
return this.a + this.b
},
set: function (newVal) {
var vals = newVal.split(' ')
this.a = vals[0]
this.b = vals[1]
}
},
// chained computed
e: function () {
return this.c + 'e'
},
// cached
f: {
get: function () {
spyF()
return this.ff
}
},
// chained cached
g: function () {
return this.f + 1
},
// another cached, for watcher test
h: {
get: function () {
return this.hh
}
}
}
})
var vm = new Test({
data: {
a: 'a',
b: 'b',
ff: 0,
hh: 0
},
watch: {
e: spyE,
h: spyCachedWatcher
}
})
it('get', function () {
expect(vm.c).toBe('ab')
expect(vm.d).toBe('ab')
expect(vm.e).toBe('abe')
})
it('set', function (done) {
vm.c = 123 // should do nothing
vm.d = 'c d'
expect(vm.a).toBe('c')
expect(vm.b).toBe('d')
expect(vm.c).toBe('cd')
expect(vm.d).toBe('cd')
expect(vm.e).toBe('cde')
Vue.nextTick(function () {
expect(spyE).toHaveBeenCalledWith('cde', 'abe')
done()
})
})
it('cached computed', function () {
expect(spyF).not.toHaveBeenCalled()
var f = vm.f
var g = vm.g
expect(spyF.calls.count()).toBe(1)
expect(f).toBe(0)
expect(g).toBe(1)
// get again
f = vm.f
g = vm.g
// should not be evaluated again
expect(spyF.calls.count()).toBe(1)
expect(f).toBe(0)
expect(g).toBe(1)
// update dep
vm.ff = 1
f = vm.f
g = vm.g
expect(spyF.calls.count()).toBe(2)
expect(f).toBe(1)
expect(g).toBe(2)
})
it('watching cached computed', function (done) {
expect(spyCachedWatcher).not.toHaveBeenCalled()
vm.hh = 2
Vue.nextTick(function () {
expect(spyCachedWatcher).toHaveBeenCalledWith(2, 0)
done()
})
})
it('same definition object bound to different instance', function () {
var vm = new Test({
data: {
a: 'A',
b: 'B'
}
})
expect(vm.c).toBe('AB')
expect(vm.d).toBe('AB')
vm.d = 'C D'
expect(vm.a).toBe('C')
expect(vm.b).toBe('D')
expect(vm.c).toBe('CD')
expect(vm.d).toBe('CD')
expect(vm.e).toBe('CDe')
})
})
describe('methods', function () {
it('should work and have correct context', function () {
var vm = new Vue({
data: {
a: 1
},
methods: {
test: function () {
expect(this instanceof Vue).toBe(true)
return this.a
}
}
})
expect(vm.test()).toBe(1)
})
})
describe('meta', function () {
var vm = new Vue({
_meta: {
$index: 0,
$value: 'test'
}
})
it('should define metas only on vm', function () {
expect(vm.$index).toBe(0)
expect(vm.$value).toBe('test')
expect('$index' in vm.$data).toBe(false)
expect('$value' in vm.$data).toBe(false)
})
})
})