observer_spec.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * Test data observation.
  3. */
  4. var Observer = require('../../src/observe/observer')
  5. // internal emitter has fixed 3 arguments
  6. // so we need to fill up the assetions with undefined
  7. var u = undefined
  8. Observer.pathDelimiter = '.'
  9. describe('Observer', function () {
  10. var spy
  11. beforeEach(function () {
  12. spy = jasmine.createSpy('observer')
  13. })
  14. it('get', function () {
  15. Observer.emitGet = true
  16. var obj = {
  17. a: 1,
  18. b: {
  19. c: 2
  20. }
  21. }
  22. var ob = Observer.create(obj)
  23. ob.on('get', spy)
  24. var t = obj.a
  25. expect(spy).toHaveBeenCalledWith('a', u, u)
  26. expect(spy.callCount).toBe(1)
  27. t = obj.b.c
  28. expect(spy).toHaveBeenCalledWith('b', u, u)
  29. expect(spy).toHaveBeenCalledWith('b.c', u, u)
  30. expect(spy.callCount).toBe(3)
  31. Observer.emitGet = false
  32. })
  33. it('set', function () {
  34. var obj = {
  35. a: 1,
  36. b: {
  37. c: 2
  38. }
  39. }
  40. var ob = Observer.create(obj)
  41. ob.on('set', spy)
  42. obj.a = 3
  43. expect(spy).toHaveBeenCalledWith('a', 3, u)
  44. expect(spy.callCount).toBe(1)
  45. obj.b.c = 4
  46. expect(spy).toHaveBeenCalledWith('b.c', 4, u)
  47. expect(spy.callCount).toBe(2)
  48. // swap set
  49. var newB = { c: 5 }
  50. obj.b = newB
  51. expect(spy).toHaveBeenCalledWith('b', newB, u)
  52. expect(spy.callCount).toBe(3)
  53. // same value set should not emit events
  54. obj.a = 3
  55. expect(spy.callCount).toBe(3)
  56. })
  57. it('array get', function () {
  58. Observer.emitGet = true
  59. var obj = {
  60. arr: [{a:1}, {a:2}]
  61. }
  62. var ob = Observer.create(obj)
  63. ob.on('get', spy)
  64. var t = obj.arr[0].a
  65. expect(spy).toHaveBeenCalledWith('arr', u, u)
  66. expect(spy).toHaveBeenCalledWith('arr.0.a', u, u)
  67. expect(spy.callCount).toBe(2)
  68. Observer.emitGet = false
  69. })
  70. it('array set', function () {
  71. var obj = {
  72. arr: [{a:1}, {a:2}]
  73. }
  74. var ob = Observer.create(obj)
  75. ob.on('set', spy)
  76. obj.arr[0].a = 2
  77. expect(spy).toHaveBeenCalledWith('arr.0.a', 2, u)
  78. // set events after mutation
  79. obj.arr.reverse()
  80. obj.arr[0].a = 3
  81. expect(spy).toHaveBeenCalledWith('arr.0.a', 3, u)
  82. })
  83. it('array push', function () {
  84. var arr = [{a:1}, {a:2}]
  85. var ob = Observer.create(arr)
  86. ob.on('mutate', spy)
  87. arr.push({a:3})
  88. expect(spy.mostRecentCall.args[0]).toBe('')
  89. expect(spy.mostRecentCall.args[1]).toBe(arr)
  90. var mutation = spy.mostRecentCall.args[2]
  91. expect(mutation).toBeDefined()
  92. expect(mutation.method).toBe('push')
  93. expect(mutation.index).toBe(2)
  94. expect(mutation.removed.length).toBe(0)
  95. expect(mutation.inserted.length).toBe(1)
  96. expect(mutation.inserted[0]).toBe(arr[2])
  97. // test index update after mutation
  98. ob.on('set', spy)
  99. arr[2].a = 4
  100. expect(spy).toHaveBeenCalledWith('2.a', 4, u)
  101. })
  102. it('array pop', function () {
  103. var arr = [{a:1}, {a:2}]
  104. var popped = arr[1]
  105. var ob = Observer.create(arr)
  106. ob.on('mutate', spy)
  107. arr.pop()
  108. expect(spy.mostRecentCall.args[0]).toBe('')
  109. expect(spy.mostRecentCall.args[1]).toBe(arr)
  110. var mutation = spy.mostRecentCall.args[2]
  111. expect(mutation).toBeDefined()
  112. expect(mutation.method).toBe('pop')
  113. expect(mutation.index).toBe(1)
  114. expect(mutation.inserted.length).toBe(0)
  115. expect(mutation.removed.length).toBe(1)
  116. expect(mutation.removed[0]).toBe(popped)
  117. })
  118. it('array shift', function () {
  119. var arr = [{a:1}, {a:2}]
  120. var shifted = arr[0]
  121. var ob = Observer.create(arr)
  122. ob.on('mutate', spy)
  123. arr.shift()
  124. expect(spy.mostRecentCall.args[0]).toBe('')
  125. expect(spy.mostRecentCall.args[1]).toBe(arr)
  126. var mutation = spy.mostRecentCall.args[2]
  127. expect(mutation).toBeDefined()
  128. expect(mutation.method).toBe('shift')
  129. expect(mutation.index).toBe(0)
  130. expect(mutation.inserted.length).toBe(0)
  131. expect(mutation.removed.length).toBe(1)
  132. expect(mutation.removed[0]).toBe(shifted)
  133. // test index update after mutation
  134. ob.on('set', spy)
  135. arr[0].a = 4
  136. expect(spy).toHaveBeenCalledWith('0.a', 4, u)
  137. })
  138. it('array unshift', function () {
  139. var arr = [{a:1}, {a:2}]
  140. var unshifted = {a:3}
  141. var ob = Observer.create(arr)
  142. ob.on('mutate', spy)
  143. arr.unshift(unshifted)
  144. expect(spy.mostRecentCall.args[0]).toBe('')
  145. expect(spy.mostRecentCall.args[1]).toBe(arr)
  146. var mutation = spy.mostRecentCall.args[2]
  147. expect(mutation).toBeDefined()
  148. expect(mutation.method).toBe('unshift')
  149. expect(mutation.index).toBe(0)
  150. expect(mutation.removed.length).toBe(0)
  151. expect(mutation.inserted.length).toBe(1)
  152. expect(mutation.inserted[0]).toBe(unshifted)
  153. // test index update after mutation
  154. ob.on('set', spy)
  155. arr[1].a = 4
  156. expect(spy).toHaveBeenCalledWith('1.a', 4, u)
  157. })
  158. it('array splice', function () {
  159. var arr = [{a:1}, {a:2}]
  160. var inserted = {a:3}
  161. var removed = arr[1]
  162. var ob = Observer.create(arr)
  163. ob.on('mutate', spy)
  164. arr.splice(1, 1, inserted)
  165. expect(spy.mostRecentCall.args[0]).toBe('')
  166. expect(spy.mostRecentCall.args[1]).toBe(arr)
  167. var mutation = spy.mostRecentCall.args[2]
  168. expect(mutation).toBeDefined()
  169. expect(mutation.method).toBe('splice')
  170. expect(mutation.index).toBe(1)
  171. expect(mutation.removed.length).toBe(1)
  172. expect(mutation.inserted.length).toBe(1)
  173. expect(mutation.removed[0]).toBe(removed)
  174. expect(mutation.inserted[0]).toBe(inserted)
  175. // test index update after mutation
  176. ob.on('set', spy)
  177. arr[1].a = 4
  178. expect(spy).toHaveBeenCalledWith('1.a', 4, u)
  179. })
  180. it('array sort', function () {
  181. var arr = [{a:1}, {a:2}]
  182. var ob = Observer.create(arr)
  183. ob.on('mutate', spy)
  184. arr.sort(function (a, b) {
  185. return a.a < b.a ? 1 : -1
  186. })
  187. expect(spy.mostRecentCall.args[0]).toBe('')
  188. expect(spy.mostRecentCall.args[1]).toBe(arr)
  189. var mutation = spy.mostRecentCall.args[2]
  190. expect(mutation).toBeDefined()
  191. expect(mutation.method).toBe('sort')
  192. expect(mutation.index).toBeUndefined()
  193. expect(mutation.removed.length).toBe(0)
  194. expect(mutation.inserted.length).toBe(0)
  195. // test index update after mutation
  196. ob.on('set', spy)
  197. arr[1].a = 4
  198. expect(spy).toHaveBeenCalledWith('1.a', 4, u)
  199. })
  200. it('array reverse', function () {
  201. var arr = [{a:1}, {a:2}]
  202. var ob = Observer.create(arr)
  203. ob.on('mutate', spy)
  204. arr.reverse()
  205. expect(spy.mostRecentCall.args[0]).toBe('')
  206. expect(spy.mostRecentCall.args[1]).toBe(arr)
  207. var mutation = spy.mostRecentCall.args[2]
  208. expect(mutation).toBeDefined()
  209. expect(mutation.method).toBe('reverse')
  210. expect(mutation.index).toBeUndefined()
  211. expect(mutation.removed.length).toBe(0)
  212. expect(mutation.inserted.length).toBe(0)
  213. // test index update after mutation
  214. ob.on('set', spy)
  215. arr[1].a = 4
  216. expect(spy).toHaveBeenCalledWith('1.a', 4, u)
  217. })
  218. it('object.$add', function () {
  219. var obj = {a:{b:1}}
  220. var ob = Observer.create(obj)
  221. ob.on('add', spy)
  222. // add event
  223. var add = {d:2}
  224. obj.a.$add('c', add)
  225. expect(spy).toHaveBeenCalledWith('a.c', add, u)
  226. // check if add object is properly observed
  227. ob.on('set', spy)
  228. obj.a.c.d = 3
  229. expect(spy).toHaveBeenCalledWith('a.c.d', 3, u)
  230. })
  231. it('object.$delete', function () {
  232. var obj = {a:{b:1}}
  233. var ob = Observer.create(obj)
  234. ob.on('delete', spy)
  235. obj.a.$delete('b')
  236. expect(spy).toHaveBeenCalledWith('a.b', u, u)
  237. })
  238. it('array.$set', function () {
  239. var arr = [{a:1}, {a:2}]
  240. var ob = Observer.create(arr)
  241. ob.on('mutate', spy)
  242. var inserted = {a:3}
  243. var removed = arr[1]
  244. arr.$set(1, inserted)
  245. expect(spy.mostRecentCall.args[0]).toBe('')
  246. expect(spy.mostRecentCall.args[1]).toBe(arr)
  247. var mutation = spy.mostRecentCall.args[2]
  248. expect(mutation).toBeDefined()
  249. expect(mutation.method).toBe('splice')
  250. expect(mutation.index).toBe(1)
  251. expect(mutation.removed.length).toBe(1)
  252. expect(mutation.inserted.length).toBe(1)
  253. expect(mutation.removed[0]).toBe(removed)
  254. expect(mutation.inserted[0]).toBe(inserted)
  255. ob.on('set', spy)
  256. arr[1].a = 4
  257. expect(spy).toHaveBeenCalledWith('1.a', 4, u)
  258. })
  259. it('array.$remove', function () {
  260. var arr = [{a:1}, {a:2}]
  261. var ob = Observer.create(arr)
  262. ob.on('mutate', spy)
  263. var removed = arr.$remove(0)
  264. expect(spy.mostRecentCall.args[0]).toBe('')
  265. expect(spy.mostRecentCall.args[1]).toBe(arr)
  266. var mutation = spy.mostRecentCall.args[2]
  267. expect(mutation).toBeDefined()
  268. expect(mutation.method).toBe('splice')
  269. expect(mutation.index).toBe(0)
  270. expect(mutation.removed.length).toBe(1)
  271. expect(mutation.inserted.length).toBe(0)
  272. expect(mutation.removed[0]).toBe(removed)
  273. ob.on('set', spy)
  274. arr[0].a = 3
  275. expect(spy).toHaveBeenCalledWith('0.a', 3, u)
  276. })
  277. })