observer_spec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. var Observer = require('../../../src/observer')
  2. var _ = require('../../../src/util')
  3. describe('Observer', function () {
  4. var spy
  5. beforeEach(function () {
  6. spy = jasmine.createSpy('observer')
  7. })
  8. it('create on non-observables', function () {
  9. // skip primitive value
  10. var ob = Observer.create(1)
  11. expect(ob).toBeUndefined()
  12. // avoid vue instance
  13. ob = Observer.create(new _.Vue())
  14. expect(ob).toBeUndefined()
  15. })
  16. it('create on object', function () {
  17. // on object
  18. var obj = {
  19. a: {},
  20. b: {}
  21. }
  22. ob = Observer.create(obj)
  23. expect(ob instanceof Observer).toBe(true)
  24. expect(ob.active).toBe(true)
  25. expect(ob.value).toBe(obj)
  26. expect(obj.__ob__).toBe(ob)
  27. // should've walked children
  28. expect(obj.a.__ob__ instanceof Observer).toBe(true)
  29. expect(obj.b.__ob__ instanceof Observer).toBe(true)
  30. // should return existing ob on already observed objects
  31. var ob2 = Observer.create(obj)
  32. expect(ob2).toBe(ob)
  33. })
  34. it('create on array', function () {
  35. // on object
  36. var arr = [{}, {}]
  37. ob = Observer.create(arr)
  38. expect(ob instanceof Observer).toBe(true)
  39. expect(ob.active).toBe(true)
  40. expect(ob.value).toBe(arr)
  41. expect(arr.__ob__).toBe(ob)
  42. // should've walked children
  43. expect(arr[0].__ob__ instanceof Observer).toBe(true)
  44. expect(arr[1].__ob__ instanceof Observer).toBe(true)
  45. })
  46. it('observing object prop change', function () {
  47. var obj = { a: { b: 2 } }
  48. Observer.create(obj)
  49. // mock a watcher!
  50. var watcher = {
  51. deps: [],
  52. addDep: function (binding) {
  53. this.deps.push(binding)
  54. binding.addSub(this)
  55. },
  56. update: jasmine.createSpy()
  57. }
  58. // collect dep
  59. Observer.target = watcher
  60. obj.a.b
  61. Observer.target = null
  62. expect(watcher.deps.length).toBe(2)
  63. obj.a.b = 3
  64. expect(watcher.update.calls.count()).toBe(1)
  65. // swap object
  66. obj.a = { b: 4 }
  67. expect(watcher.update.calls.count()).toBe(2)
  68. // recollect dep
  69. var oldDeps = watcher.deps
  70. watcher.deps = []
  71. Observer.target = watcher
  72. obj.a.b
  73. Observer.target = null
  74. expect(watcher.deps.length).toBe(2)
  75. // make sure we picked up the new bindings
  76. expect(watcher.deps[0]).not.toBe(oldDeps[0])
  77. expect(watcher.deps[1]).not.toBe(oldDeps[1])
  78. // set on the swapped object
  79. obj.a.b = 5
  80. expect(watcher.update.calls.count()).toBe(3)
  81. })
  82. it('observing $add/$delete', function () {
  83. var obj = { a: 1 }
  84. var ob = Observer.create(obj)
  85. var binding = ob.binding
  86. spyOn(binding, 'notify')
  87. obj.$add('b', 2)
  88. expect(obj.b).toBe(2)
  89. expect(binding.notify.calls.count()).toBe(1)
  90. obj.$delete('a')
  91. expect(obj.hasOwnProperty('a')).toBe(false)
  92. expect(binding.notify.calls.count()).toBe(2)
  93. // should ignore adding an existing key
  94. obj.$add('b', 3)
  95. expect(obj.b).toBe(2)
  96. expect(binding.notify.calls.count()).toBe(2)
  97. // should ignore deleting non-existing key
  98. obj.$delete('a')
  99. expect(binding.notify.calls.count()).toBe(2)
  100. })
  101. it('observing array mutation', function () {
  102. var arr = []
  103. var ob = Observer.create(arr)
  104. var binding = ob.binding
  105. spyOn(binding, 'notify')
  106. var objs = [{}, {}, {}]
  107. arr.push(objs[0])
  108. arr.pop()
  109. arr.unshift(objs[1])
  110. arr.shift()
  111. arr.splice(0, 0, objs[2])
  112. arr.sort()
  113. arr.reverse()
  114. expect(binding.notify.calls.count()).toBe(7)
  115. // inserted elements should be observed
  116. objs.forEach(function (obj) {
  117. expect(obj.__ob__ instanceof Observer).toBe(true)
  118. })
  119. })
  120. it('array $set', function () {
  121. var arr = [1]
  122. var ob = Observer.create(arr)
  123. var binding = ob.binding
  124. spyOn(binding, 'notify')
  125. arr.$set(0, 2)
  126. expect(arr[0]).toBe(2)
  127. expect(binding.notify.calls.count()).toBe(1)
  128. // setting out of bound index
  129. arr.$set(2, 3)
  130. expect(arr[2]).toBe(3)
  131. expect(binding.notify.calls.count()).toBe(2)
  132. })
  133. it('array $remove', function () {
  134. var arr = [{}, {}]
  135. var obj1 = arr[0]
  136. var obj2 = arr[1]
  137. var ob = Observer.create(arr)
  138. var binding = ob.binding
  139. spyOn(binding, 'notify')
  140. // remove by index
  141. arr.$remove(0)
  142. expect(arr.length).toBe(1)
  143. expect(arr[0]).toBe(obj2)
  144. expect(binding.notify.calls.count()).toBe(1)
  145. // remove by identity, not in array
  146. arr.$remove(obj1)
  147. expect(arr.length).toBe(1)
  148. expect(arr[0]).toBe(obj2)
  149. expect(binding.notify.calls.count()).toBe(1)
  150. // remove by identity, in array
  151. arr.$remove(obj2)
  152. expect(arr.length).toBe(0)
  153. expect(binding.notify.calls.count()).toBe(2)
  154. })
  155. })