observer_spec.js 5.5 KB

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