observer_spec.js 5.4 KB

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