vue2/test/unit/specs/observer_spec.js

398 lines
11 KiB
JavaScript
Raw Normal View History

2014-07-21 04:42:14 +08:00
/**
* Test data observation.
*/
var Observer = require('../../../src/observe/observer')
var _ = require('../../../src/util')
2014-07-11 13:59:40 +08:00
Observer.pathDelimiter = '.'
2014-07-10 07:08:01 +08:00
2014-07-11 08:57:34 +08:00
describe('Observer', function () {
2014-07-10 10:33:39 +08:00
2014-07-11 08:57:34 +08:00
var spy
2014-07-10 10:33:39 +08:00
beforeEach(function () {
2014-07-11 13:59:40 +08:00
spy = jasmine.createSpy('observer')
2014-07-11 08:57:34 +08:00
})
it('get', function () {
2014-07-11 13:59:40 +08:00
Observer.emitGet = true
2014-07-11 08:57:34 +08:00
var obj = {
2014-07-10 10:33:39 +08:00
a: 1,
b: {
c: 2
}
}
2014-07-11 08:57:34 +08:00
var ob = Observer.create(obj)
ob.on('get', spy)
var t = obj.a
expect(spy).toHaveBeenCalledWith('a', undefined, undefined, undefined)
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(1)
2014-07-11 08:57:34 +08:00
t = obj.b.c
expect(spy).toHaveBeenCalledWith('b', undefined, undefined, undefined)
expect(spy).toHaveBeenCalledWith('b.c', undefined, undefined, undefined)
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(3)
2014-07-11 13:59:40 +08:00
Observer.emitGet = false
2014-07-10 10:33:39 +08:00
})
2014-07-11 08:57:34 +08:00
it('set', function () {
var obj = {
a: 1,
b: {
c: 2
}
}
var ob = Observer.create(obj)
2014-07-10 10:33:39 +08:00
ob.on('set', spy)
2014-07-11 08:57:34 +08:00
2014-07-10 10:33:39 +08:00
obj.a = 3
expect(spy).toHaveBeenCalledWith('a', 3, undefined, undefined)
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(1)
2014-07-11 08:57:34 +08:00
2014-07-10 10:33:39 +08:00
obj.b.c = 4
expect(spy).toHaveBeenCalledWith('b.c', 4, undefined, undefined)
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(2)
2014-07-11 08:57:34 +08:00
2014-07-12 00:49:19 +08:00
// swap set
2014-07-11 08:57:34 +08:00
var newB = { c: 5 }
obj.b = newB
expect(spy).toHaveBeenCalledWith('b', newB, undefined, undefined)
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(3)
2014-07-11 13:59:40 +08:00
// same value set should not emit events
obj.a = 3
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(3)
2014-07-11 08:57:34 +08:00
})
2014-07-31 05:03:22 +08:00
it('ignore prefix', function () {
var obj = {
_test: 123,
$test: 234
}
var ob = Observer.create(obj)
ob.on('set', spy)
obj._test = 234
obj.$test = 345
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(0)
2014-07-31 05:03:22 +08:00
})
it('warn duplicate value', function () {
spyOn(_, 'warn')
var obj = {
a: { b: 123 },
b: null
}
var ob = Observer.create(obj)
obj.b = obj.a
expect(_.warn).toHaveBeenCalled()
})
2014-07-11 08:57:34 +08:00
it('array get', function () {
2014-07-11 13:59:40 +08:00
Observer.emitGet = true
2014-07-11 08:57:34 +08:00
var obj = {
arr: [{a:1}, {a:2}]
}
var ob = Observer.create(obj)
ob.on('get', spy)
var t = obj.arr[0].a
expect(spy).toHaveBeenCalledWith('arr', undefined, undefined, undefined)
expect(spy).toHaveBeenCalledWith('arr.0.a', undefined, undefined, undefined)
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(2)
2014-07-11 13:59:40 +08:00
Observer.emitGet = false
2014-07-11 08:57:34 +08:00
})
it('array set', function () {
2014-07-11 13:59:40 +08:00
var obj = {
arr: [{a:1}, {a:2}]
}
var ob = Observer.create(obj)
ob.on('set', spy)
obj.arr[0].a = 2
expect(spy).toHaveBeenCalledWith('arr.0.a', 2, undefined, undefined)
2014-07-11 13:59:40 +08:00
// set events after mutation
obj.arr.reverse()
obj.arr[0].a = 3
expect(spy).toHaveBeenCalledWith('arr.0.a', 3, undefined, undefined)
2014-07-11 08:57:34 +08:00
})
2014-07-12 00:49:19 +08:00
it('array push', function () {
2014-07-11 13:59:40 +08:00
var arr = [{a:1}, {a:2}]
var ob = Observer.create(arr)
ob.on('mutate', spy)
arr.push({a:3})
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
2014-07-11 13:59:40 +08:00
expect(mutation).toBeDefined()
2014-07-26 23:29:56 +08:00
expect(mutation.method).toBe('push')
expect(mutation.index).toBe(2)
expect(mutation.removed.length).toBe(0)
expect(mutation.inserted.length).toBe(1)
expect(mutation.inserted[0]).toBe(arr[2])
2014-07-12 00:49:19 +08:00
// test index update after mutation
ob.on('set', spy)
arr[2].a = 4
expect(spy).toHaveBeenCalledWith('2.a', 4, undefined, undefined)
2014-07-11 13:59:40 +08:00
})
2014-07-12 00:49:19 +08:00
it('array pop', function () {
2014-07-11 13:59:40 +08:00
var arr = [{a:1}, {a:2}]
2014-07-12 00:49:19 +08:00
var popped = arr[1]
2014-07-11 13:59:40 +08:00
var ob = Observer.create(arr)
2014-07-12 00:49:19 +08:00
ob.on('mutate', spy)
arr.pop()
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
2014-07-12 00:49:19 +08:00
expect(mutation).toBeDefined()
2014-07-26 23:29:56 +08:00
expect(mutation.method).toBe('pop')
expect(mutation.index).toBe(1)
expect(mutation.inserted.length).toBe(0)
expect(mutation.removed.length).toBe(1)
expect(mutation.removed[0]).toBe(popped)
2014-07-12 00:49:19 +08:00
})
it('array shift', function () {
var arr = [{a:1}, {a:2}]
var shifted = arr[0]
var ob = Observer.create(arr)
ob.on('mutate', spy)
arr.shift()
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
2014-07-12 00:49:19 +08:00
expect(mutation).toBeDefined()
2014-07-26 23:29:56 +08:00
expect(mutation.method).toBe('shift')
expect(mutation.index).toBe(0)
expect(mutation.inserted.length).toBe(0)
expect(mutation.removed.length).toBe(1)
expect(mutation.removed[0]).toBe(shifted)
2014-07-12 00:49:19 +08:00
// test index update after mutation
2014-07-11 13:59:40 +08:00
ob.on('set', spy)
2014-07-12 00:49:19 +08:00
arr[0].a = 4
expect(spy).toHaveBeenCalledWith('0.a', 4, undefined, undefined)
2014-07-12 00:49:19 +08:00
})
it('array unshift', function () {
var arr = [{a:1}, {a:2}]
var unshifted = {a:3}
var ob = Observer.create(arr)
ob.on('mutate', spy)
arr.unshift(unshifted)
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
2014-07-12 00:49:19 +08:00
expect(mutation).toBeDefined()
2014-07-26 23:29:56 +08:00
expect(mutation.method).toBe('unshift')
expect(mutation.index).toBe(0)
expect(mutation.removed.length).toBe(0)
expect(mutation.inserted.length).toBe(1)
expect(mutation.inserted[0]).toBe(unshifted)
2014-07-12 00:49:19 +08:00
// test index update after mutation
ob.on('set', spy)
arr[1].a = 4
expect(spy).toHaveBeenCalledWith('1.a', 4, undefined, undefined)
2014-07-12 00:49:19 +08:00
})
it('array splice', function () {
var arr = [{a:1}, {a:2}]
var inserted = {a:3}
var removed = arr[1]
var ob = Observer.create(arr)
ob.on('mutate', spy)
arr.splice(1, 1, inserted)
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
2014-07-12 00:49:19 +08:00
expect(mutation).toBeDefined()
2014-07-26 23:29:56 +08:00
expect(mutation.method).toBe('splice')
expect(mutation.index).toBe(1)
expect(mutation.removed.length).toBe(1)
expect(mutation.inserted.length).toBe(1)
expect(mutation.removed[0]).toBe(removed)
expect(mutation.inserted[0]).toBe(inserted)
2014-07-12 00:49:19 +08:00
// test index update after mutation
ob.on('set', spy)
arr[1].a = 4
expect(spy).toHaveBeenCalledWith('1.a', 4, undefined, undefined)
2014-07-12 00:49:19 +08:00
})
it('array sort', function () {
var arr = [{a:1}, {a:2}]
var ob = Observer.create(arr)
ob.on('mutate', spy)
arr.sort(function (a, b) {
return a.a < b.a ? 1 : -1
})
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
2014-07-12 00:49:19 +08:00
expect(mutation).toBeDefined()
2014-07-26 23:29:56 +08:00
expect(mutation.method).toBe('sort')
2014-07-12 00:49:19 +08:00
expect(mutation.index).toBeUndefined()
2014-07-26 23:29:56 +08:00
expect(mutation.removed.length).toBe(0)
expect(mutation.inserted.length).toBe(0)
2014-07-12 00:49:19 +08:00
// test index update after mutation
ob.on('set', spy)
arr[1].a = 4
expect(spy).toHaveBeenCalledWith('1.a', 4, undefined, undefined)
2014-07-12 00:49:19 +08:00
})
it('array reverse', function () {
var arr = [{a:1}, {a:2}]
var ob = Observer.create(arr)
ob.on('mutate', spy)
arr.reverse()
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
2014-07-12 00:49:19 +08:00
expect(mutation).toBeDefined()
2014-07-26 23:29:56 +08:00
expect(mutation.method).toBe('reverse')
2014-07-12 00:49:19 +08:00
expect(mutation.index).toBeUndefined()
2014-07-26 23:29:56 +08:00
expect(mutation.removed.length).toBe(0)
expect(mutation.inserted.length).toBe(0)
2014-07-12 00:49:19 +08:00
// test index update after mutation
ob.on('set', spy)
arr[1].a = 4
expect(spy).toHaveBeenCalledWith('1.a', 4, undefined, undefined)
2014-07-11 08:57:34 +08:00
})
it('object.$add', function () {
2014-07-12 00:49:19 +08:00
var obj = {a:{b:1}}
var ob = Observer.create(obj)
2014-07-18 01:36:45 +08:00
ob.on('add', spy)
2014-07-12 00:49:19 +08:00
// ignore existing keys
obj.$add('a', 123)
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(0)
2014-07-12 00:49:19 +08:00
// add event
2014-07-18 01:36:45 +08:00
var add = {d:2}
obj.a.$add('c', add)
expect(spy).toHaveBeenCalledWith('a.c', add, undefined, undefined)
2014-07-12 00:49:19 +08:00
2014-07-18 01:36:45 +08:00
// check if add object is properly observed
2014-07-12 00:49:19 +08:00
ob.on('set', spy)
obj.a.c.d = 3
expect(spy).toHaveBeenCalledWith('a.c.d', 3, undefined, undefined)
2014-07-10 07:08:01 +08:00
})
2014-07-11 08:57:34 +08:00
it('object.$delete', function () {
2014-07-12 00:49:19 +08:00
var obj = {a:{b:1}}
var ob = Observer.create(obj)
2014-07-18 01:36:45 +08:00
ob.on('delete', spy)
2014-07-12 00:49:19 +08:00
// ignore non-present key
obj.$delete('c')
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(0)
2014-07-12 00:49:19 +08:00
obj.a.$delete('b')
expect(spy).toHaveBeenCalledWith('a.b', undefined, undefined, undefined)
2014-07-11 05:24:35 +08:00
})
2014-07-11 08:57:34 +08:00
it('array.$set', function () {
2014-07-12 00:49:19 +08:00
var arr = [{a:1}, {a:2}]
var ob = Observer.create(arr)
ob.on('mutate', spy)
var inserted = {a:3}
var removed = arr[1]
arr.$set(1, inserted)
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
2014-07-12 00:49:19 +08:00
expect(mutation).toBeDefined()
2014-07-26 23:29:56 +08:00
expect(mutation.method).toBe('splice')
expect(mutation.index).toBe(1)
expect(mutation.removed.length).toBe(1)
expect(mutation.inserted.length).toBe(1)
expect(mutation.removed[0]).toBe(removed)
expect(mutation.inserted[0]).toBe(inserted)
2014-07-12 00:49:19 +08:00
ob.on('set', spy)
arr[1].a = 4
expect(spy).toHaveBeenCalledWith('1.a', 4, undefined, undefined)
2014-07-11 05:24:35 +08:00
})
it('array.$set with out of bound length', function () {
var arr = [{a:1}, {a:2}]
var ob = Observer.create(arr)
var inserted = {a:3}
arr.$set(3, inserted)
expect(arr.length).toBe(4)
expect(arr[2]).toBeUndefined()
expect(arr[3]).toBe(inserted)
})
2014-07-11 08:57:34 +08:00
it('array.$remove', function () {
2014-07-12 00:49:19 +08:00
var arr = [{a:1}, {a:2}]
var ob = Observer.create(arr)
ob.on('mutate', spy)
var removed = arr.$remove(0)
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
2014-07-12 00:49:19 +08:00
expect(mutation).toBeDefined()
2014-07-26 23:29:56 +08:00
expect(mutation.method).toBe('splice')
expect(mutation.index).toBe(0)
expect(mutation.removed.length).toBe(1)
expect(mutation.inserted.length).toBe(0)
expect(mutation.removed[0]).toBe(removed)
2014-07-12 00:49:19 +08:00
ob.on('set', spy)
arr[0].a = 3
expect(spy).toHaveBeenCalledWith('0.a', 3, undefined, undefined)
2014-07-11 05:24:35 +08:00
})
it('array.$remove object', function () {
var arr = [{a:1}, {a:2}]
var ob = Observer.create(arr)
ob.on('mutate', spy)
var removed = arr.$remove(arr[0])
2014-08-09 07:02:25 +08:00
expect(spy.calls.mostRecent().args[0]).toBe('')
expect(spy.calls.mostRecent().args[1]).toBe(arr)
var mutation = spy.calls.mostRecent().args[2]
expect(mutation).toBeDefined()
expect(mutation.method).toBe('splice')
expect(mutation.index).toBe(0)
expect(mutation.removed.length).toBe(1)
expect(mutation.inserted.length).toBe(0)
expect(mutation.removed[0]).toBe(removed)
ob.on('set', spy)
arr[0].a = 3
expect(spy).toHaveBeenCalledWith('0.a', 3, undefined, undefined)
})
2014-08-06 06:35:39 +08:00
it('shared observe', function () {
var obj = { a: 1 }
var parentA = { child1: obj }
var parentB = { child2: obj }
var obA = Observer.create(parentA)
var obB = Observer.create(parentB)
obA.on('set', spy)
obB.on('set', spy)
obj.a = 2
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(2)
expect(spy).toHaveBeenCalledWith('child1.a', 2, undefined, undefined)
expect(spy).toHaveBeenCalledWith('child2.a', 2, undefined, undefined)
2014-08-06 06:35:39 +08:00
// test unobserve
parentA.child1 = null
obj.a = 3
2014-08-09 07:02:25 +08:00
expect(spy.calls.count()).toBe(4)
expect(spy).toHaveBeenCalledWith('child1', null, undefined, undefined)
expect(spy).toHaveBeenCalledWith('child2.a', 3, undefined, undefined)
2014-08-06 06:35:39 +08:00
})
2014-07-10 07:08:01 +08:00
})