-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
/
Copy pathobserver.spec.js
400 lines (367 loc) · 10.8 KB
/
observer.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import Vue from 'vue'
import {
Observer,
observe,
set as setProp,
del as delProp
} from 'core/observer/index'
import Dep from 'core/observer/dep'
import { hasOwn } from 'core/util/index'
describe('Observer', () => {
it('create on non-observables', () => {
// skip primitive value
const ob1 = observe(1)
expect(ob1).toBeUndefined()
// avoid vue instance
const ob2 = observe(new Vue())
expect(ob2).toBeUndefined()
// avoid frozen objects
const ob3 = observe(Object.freeze({}))
expect(ob3).toBeUndefined()
})
it('create on object', () => {
// on object
const obj = {
a: {},
b: {}
}
const ob1 = observe(obj)
expect(ob1 instanceof Observer).toBe(true)
expect(ob1.value).toBe(obj)
expect(obj.__ob__).toBe(ob1)
// should've walked children
expect(obj.a.__ob__ instanceof Observer).toBe(true)
expect(obj.b.__ob__ instanceof Observer).toBe(true)
// should return existing ob on already observed objects
const ob2 = observe(obj)
expect(ob2).toBe(ob1)
})
it('create on null', () => {
// on null
const obj = Object.create(null)
obj.a = {}
obj.b = {}
const ob1 = observe(obj)
expect(ob1 instanceof Observer).toBe(true)
expect(ob1.value).toBe(obj)
expect(obj.__ob__).toBe(ob1)
// should've walked children
expect(obj.a.__ob__ instanceof Observer).toBe(true)
expect(obj.b.__ob__ instanceof Observer).toBe(true)
// should return existing ob on already observed objects
const ob2 = observe(obj)
expect(ob2).toBe(ob1)
})
it('create on already observed object', () => {
// on object
const obj = {}
let val = 0
let getCount = 0
Object.defineProperty(obj, 'a', {
configurable: true,
enumerable: true,
get () {
getCount++
return val
},
set (v) { val = v }
})
const ob1 = observe(obj)
expect(ob1 instanceof Observer).toBe(true)
expect(ob1.value).toBe(obj)
expect(obj.__ob__).toBe(ob1)
getCount = 0
// Each read of 'a' should result in only one get underlying get call
obj.a
expect(getCount).toBe(1)
obj.a
expect(getCount).toBe(2)
// should return existing ob on already observed objects
const ob2 = observe(obj)
expect(ob2).toBe(ob1)
// should call underlying setter
obj.a = 10
expect(val).toBe(10)
})
it('create on property with only getter', () => {
// on object
const obj = {}
Object.defineProperty(obj, 'a', {
configurable: true,
enumerable: true,
get () { return 123 }
})
const ob1 = observe(obj)
expect(ob1 instanceof Observer).toBe(true)
expect(ob1.value).toBe(obj)
expect(obj.__ob__).toBe(ob1)
// should be able to read
expect(obj.a).toBe(123)
// should return existing ob on already observed objects
const ob2 = observe(obj)
expect(ob2).toBe(ob1)
// since there is no setter, you shouldn't be able to write to it
// PhantomJS throws when a property with no setter is set
// but other real browsers don't
try {
obj.a = 101
} catch (e) {}
expect(obj.a).toBe(123)
})
it('create on property with only setter', () => {
// on object
const obj = {}
let val = 10
Object.defineProperty(obj, 'a', { // eslint-disable-line accessor-pairs
configurable: true,
enumerable: true,
set (v) { val = v }
})
const ob1 = observe(obj)
expect(ob1 instanceof Observer).toBe(true)
expect(ob1.value).toBe(obj)
expect(obj.__ob__).toBe(ob1)
// reads should return undefined
expect(obj.a).toBe(undefined)
// should return existing ob on already observed objects
const ob2 = observe(obj)
expect(ob2).toBe(ob1)
// writes should call the set function
obj.a = 100
expect(val).toBe(100)
})
it('create on property which is marked not configurable', () => {
// on object
const obj = {}
Object.defineProperty(obj, 'a', {
configurable: false,
enumerable: true,
val: 10
})
const ob1 = observe(obj)
expect(ob1 instanceof Observer).toBe(true)
expect(ob1.value).toBe(obj)
expect(obj.__ob__).toBe(ob1)
})
it('create on array', () => {
// on object
const arr = [{}, {}]
const ob1 = observe(arr)
expect(ob1 instanceof Observer).toBe(true)
expect(ob1.value).toBe(arr)
expect(arr.__ob__).toBe(ob1)
// should've walked children
expect(arr[0].__ob__ instanceof Observer).toBe(true)
expect(arr[1].__ob__ instanceof Observer).toBe(true)
})
it('observing object prop change', () => {
const obj = { a: { b: 2 }, c: NaN }
observe(obj)
// mock a watcher!
const watcher = {
deps: [],
addDep (dep) {
this.deps.push(dep)
dep.addSub(this)
},
update: jasmine.createSpy()
}
// collect dep
Dep.target = watcher
obj.a.b
Dep.target = null
expect(watcher.deps.length).toBe(3) // obj.a + a + a.b
obj.a.b = 3
expect(watcher.update.calls.count()).toBe(1)
// swap object
obj.a = { b: 4 }
expect(watcher.update.calls.count()).toBe(2)
watcher.deps = []
Dep.target = watcher
obj.a.b
obj.c
Dep.target = null
expect(watcher.deps.length).toBe(4)
// set on the swapped object
obj.a.b = 5
expect(watcher.update.calls.count()).toBe(3)
// should not trigger on NaN -> NaN set
obj.c = NaN
expect(watcher.update.calls.count()).toBe(3)
})
it('observing object prop change on defined property', () => {
const obj = { val: 2 }
Object.defineProperty(obj, 'a', {
configurable: true,
enumerable: true,
get () { return this.val },
set (v) {
this.val = v
return this.val
}
})
observe(obj)
// mock a watcher!
const watcher = {
deps: [],
addDep: function (dep) {
this.deps.push(dep)
dep.addSub(this)
},
update: jasmine.createSpy()
}
// collect dep
Dep.target = watcher
expect(obj.a).toBe(2) // Make sure 'this' is preserved
Dep.target = null
obj.a = 3
expect(obj.val).toBe(3) // make sure 'setter' was called
obj.val = 5
expect(obj.a).toBe(5) // make sure 'getter' was called
})
it('observing set/delete', () => {
const obj1 = { a: 1 }
const ob1 = observe(obj1)
const dep1 = ob1.dep
spyOn(dep1, 'notify')
setProp(obj1, 'b', 2)
expect(obj1.b).toBe(2)
expect(dep1.notify.calls.count()).toBe(1)
delProp(obj1, 'a')
expect(hasOwn(obj1, 'a')).toBe(false)
expect(dep1.notify.calls.count()).toBe(2)
// set existing key, should be a plain set and not
// trigger own ob's notify
setProp(obj1, 'b', 3)
expect(obj1.b).toBe(3)
expect(dep1.notify.calls.count()).toBe(2)
// set non-existing key
setProp(obj1, 'c', 1)
expect(obj1.c).toBe(1)
expect(dep1.notify.calls.count()).toBe(3)
// should ignore deleting non-existing key
delProp(obj1, 'a')
expect(dep1.notify.calls.count()).toBe(3)
// should work on non-observed objects
const obj2 = { a: 1 }
delProp(obj2, 'a')
expect(hasOwn(obj2, 'a')).toBe(false)
// should work on Object.create(null)
const obj3 = Object.create(null)
obj3.a = 1
const ob3 = observe(obj3)
const dep3 = ob3.dep
spyOn(dep3, 'notify')
setProp(obj3, 'b', 2)
expect(obj3.b).toBe(2)
expect(dep3.notify.calls.count()).toBe(1)
delProp(obj3, 'a')
expect(hasOwn(obj3, 'a')).toBe(false)
expect(dep3.notify.calls.count()).toBe(2)
// set and delete non-numeric key on array
const arr2 = ['a']
const ob2 = observe(arr2)
const dep2 = ob2.dep
spyOn(dep2, 'notify')
setProp(arr2, 'b', 2)
expect(arr2.b).toBe(2)
expect(dep2.notify.calls.count()).toBe(1)
delProp(arr2, 'b')
expect(hasOwn(arr2, 'b')).toBe(false)
expect(dep2.notify.calls.count()).toBe(2)
})
it('warning set/delete on a Vue instance', done => {
const vm = new Vue({
template: '<div>{{a}}</div>',
data: { a: 1 }
}).$mount()
expect(vm.$el.outerHTML).toBe('<div>1</div>')
Vue.set(vm, 'a', 2)
waitForUpdate(() => {
expect(vm.$el.outerHTML).toBe('<div>2</div>')
expect('Avoid adding reactive properties to a Vue instance').not.toHaveBeenWarned()
Vue.delete(vm, 'a')
}).then(() => {
expect('Avoid deleting properties on a Vue instance').toHaveBeenWarned()
expect(vm.$el.outerHTML).toBe('<div>2</div>')
Vue.set(vm, 'b', 123)
expect('Avoid adding reactive properties to a Vue instance').toHaveBeenWarned()
}).then(done)
})
it('Cannot enable reactivity on a property that is already defined', done => {
const vm = new Vue({
template: '<div></div>',
data: {
person: {
age: 32
}
}
}).$mount()
vm.person.job = 'Programmer'
Vue.set(vm.person, 'job', 'Educator')
waitForUpdate(() => {
expect(`Cannot enable reactivity on a property that is already defined`).toHaveBeenWarned()
}).then(done)
})
it('warning set/delete on Vue instance root $data', done => {
const data = { a: 1 }
const vm = new Vue({
template: '<div>{{a}}</div>',
data
}).$mount()
expect(vm.$el.outerHTML).toBe('<div>1</div>')
expect(Vue.set(data, 'a', 2)).toBe(2)
waitForUpdate(() => {
expect(vm.$el.outerHTML).toBe('<div>2</div>')
expect('Avoid adding reactive properties to a Vue instance').not.toHaveBeenWarned()
Vue.delete(data, 'a')
}).then(() => {
expect('Avoid deleting properties on a Vue instance').toHaveBeenWarned()
expect(vm.$el.outerHTML).toBe('<div>2</div>')
expect(Vue.set(data, 'b', 123)).toBe(123)
expect('Avoid adding reactive properties to a Vue instance').toHaveBeenWarned()
}).then(done)
})
it('observing array mutation', () => {
const arr = []
const ob = observe(arr)
const dep = ob.dep
spyOn(dep, 'notify')
const objs = [{}, {}, {}]
arr.push(objs[0])
arr.pop()
arr.unshift(objs[1])
arr.shift()
arr.splice(0, 0, objs[2])
arr.sort()
arr.reverse()
expect(dep.notify.calls.count()).toBe(7)
// inserted elements should be observed
objs.forEach(obj => {
expect(obj.__ob__ instanceof Observer).toBe(true)
})
})
it('warn set/delete on non valid values', () => {
try {
setProp(null, 'foo', 1)
} catch (e) {}
expect(`Cannot set reactive property on undefined, null, or primitive value`).toHaveBeenWarned()
try {
delProp(null, 'foo')
} catch (e) {}
expect(`Cannot delete reactive property on undefined, null, or primitive value`).toHaveBeenWarned()
})
it('should lazy invoke existing getters', () => {
const obj = {}
let called = false
Object.defineProperty(obj, 'getterProp', {
enumerable: true,
get: () => {
called = true
return 'some value'
}
})
observe(obj)
expect(called).toBe(false)
})
})