watcher_spec.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. var Vue = require('../../../src/vue')
  2. var nextTick = Vue.nextTick
  3. var Watcher = require('../../../src/watcher')
  4. var _ = Vue.util
  5. describe('Watcher', function () {
  6. var vm, spy
  7. beforeEach(function () {
  8. vm = new Vue({
  9. filters: {},
  10. data: {
  11. a: 1,
  12. b: {
  13. c: 2,
  14. d: 4
  15. },
  16. c: 'c'
  17. }
  18. })
  19. spy = jasmine.createSpy('watcher')
  20. })
  21. it('simple path', function (done) {
  22. var watcher = new Watcher(vm, 'b.c', spy)
  23. expect(watcher.value).toBe(2)
  24. vm.b.c = 3
  25. nextTick(function () {
  26. expect(watcher.value).toBe(3)
  27. expect(spy).toHaveBeenCalledWith(3, 2)
  28. vm.b = { c: 4 } // swapping the object
  29. nextTick(function () {
  30. expect(watcher.value).toBe(4)
  31. expect(spy).toHaveBeenCalledWith(4, 3)
  32. done()
  33. })
  34. })
  35. })
  36. it('bracket access path', function (done) {
  37. var watcher = new Watcher(vm, 'b["c"]', spy)
  38. expect(watcher.value).toBe(2)
  39. vm.b.c = 3
  40. nextTick(function () {
  41. expect(watcher.value).toBe(3)
  42. expect(spy).toHaveBeenCalledWith(3, 2)
  43. vm.b = { c: 4 } // swapping the object
  44. nextTick(function () {
  45. expect(watcher.value).toBe(4)
  46. expect(spy).toHaveBeenCalledWith(4, 3)
  47. done()
  48. })
  49. })
  50. })
  51. it('dynamic path', function (done) {
  52. var watcher = new Watcher(vm, 'b[c]', spy)
  53. expect(watcher.value).toBe(2)
  54. vm.b.c = 3
  55. nextTick(function () {
  56. expect(watcher.value).toBe(3)
  57. expect(spy).toHaveBeenCalledWith(3, 2)
  58. vm.c = 'd' // changing the dynamic segment in path
  59. nextTick(function () {
  60. expect(watcher.value).toBe(4)
  61. expect(spy).toHaveBeenCalledWith(4, 3)
  62. done()
  63. })
  64. })
  65. })
  66. it('simple expression', function (done) {
  67. var watcher = new Watcher(vm, 'a + b.c', spy)
  68. expect(watcher.value).toBe(3)
  69. vm.b.c = 3
  70. nextTick(function () {
  71. expect(watcher.value).toBe(4)
  72. expect(spy.calls.count()).toBe(1)
  73. expect(spy).toHaveBeenCalledWith(4, 3)
  74. // change two dependencies at once
  75. vm.a = 2
  76. vm.b.c = 4
  77. nextTick(function () {
  78. expect(watcher.value).toBe(6)
  79. // should trigger only once callback,
  80. // because it was in the same event loop.
  81. expect(spy.calls.count()).toBe(2)
  82. expect(spy).toHaveBeenCalledWith(6, 4)
  83. done()
  84. })
  85. })
  86. })
  87. it('ternary expression', function (done) {
  88. // we're actually testing for the dependency re-calculation here
  89. var watcher = new Watcher(vm, 'a > 1 ? b.c : b.d', spy)
  90. expect(watcher.value).toBe(4)
  91. vm.a = 2
  92. nextTick(function () {
  93. expect(watcher.value).toBe(2)
  94. expect(spy).toHaveBeenCalledWith(2, 4)
  95. vm.b.c = 3
  96. nextTick(function () {
  97. expect(watcher.value).toBe(3)
  98. expect(spy).toHaveBeenCalledWith(3, 2)
  99. done()
  100. })
  101. })
  102. })
  103. it('non-existent path, $add later', function (done) {
  104. var watcher = new Watcher(vm, 'd.e', spy)
  105. var watcher2 = new Watcher(vm, 'b.e', spy)
  106. expect(watcher.value).toBeUndefined()
  107. expect(watcher2.value).toBeUndefined()
  108. vm.$add('d', { e: 123 })
  109. vm.b.$add('e', 234)
  110. nextTick(function () {
  111. expect(watcher.value).toBe(123)
  112. expect(watcher2.value).toBe(234)
  113. expect(spy).toHaveBeenCalledWith(123, undefined)
  114. expect(spy).toHaveBeenCalledWith(234, undefined)
  115. done()
  116. })
  117. })
  118. it('$delete', function (done) {
  119. var watcher = new Watcher(vm, 'b.c', spy)
  120. expect(watcher.value).toBe(2)
  121. vm.$delete('b')
  122. nextTick(function () {
  123. expect(watcher.value).toBeUndefined()
  124. expect(spy).toHaveBeenCalledWith(undefined, 2)
  125. done()
  126. })
  127. })
  128. it('swapping $data', function (done) {
  129. var watcher = new Watcher(vm, 'b.c', spy)
  130. expect(watcher.value).toBe(2)
  131. vm.$data = { b: { c: 3}}
  132. nextTick(function () {
  133. expect(watcher.value).toBe(3)
  134. expect(spy).toHaveBeenCalledWith(3, 2)
  135. done()
  136. })
  137. })
  138. it('path containing $data', function (done) {
  139. var watcher = new Watcher(vm, '$data.b.c', spy)
  140. expect(watcher.value).toBe(2)
  141. vm.b = { c: 3 }
  142. nextTick(function () {
  143. expect(watcher.value).toBe(3)
  144. expect(spy).toHaveBeenCalledWith(3, 2)
  145. vm.$data = { b: {c: 4}}
  146. nextTick(function () {
  147. expect(watcher.value).toBe(4)
  148. expect(spy).toHaveBeenCalledWith(4, 3)
  149. done()
  150. })
  151. })
  152. })
  153. it('watching $data', function (done) {
  154. var oldData = vm.$data
  155. var watcher = new Watcher(vm, '$data', spy)
  156. expect(watcher.value).toBe(oldData)
  157. vm.a = 2
  158. nextTick(function () {
  159. expect(spy).toHaveBeenCalledWith(oldData, oldData)
  160. var newData = {}
  161. vm.$data = newData
  162. nextTick(function() {
  163. expect(spy).toHaveBeenCalledWith(newData, oldData)
  164. expect(watcher.value).toBe(newData)
  165. done()
  166. })
  167. })
  168. })
  169. it('watching parent scope properties', function (done) {
  170. var child = vm.$addChild()
  171. var spy2 = jasmine.createSpy('watch')
  172. var watcher1 = new Watcher(child, '$data', spy)
  173. var watcher2 = new Watcher(child, 'a', spy2)
  174. vm.a = 123
  175. nextTick(function () {
  176. // $data should only be called on self data change
  177. expect(watcher1.value).toBe(child.$data)
  178. expect(spy.calls.count()).toBe(0)
  179. expect(watcher2.value).toBe(123)
  180. expect(spy2).toHaveBeenCalledWith(123, 1)
  181. done()
  182. })
  183. })
  184. it('filters', function (done) {
  185. vm.$options.filters.test = function (val, multi) {
  186. return val * multi
  187. }
  188. vm.$options.filters.test2 = function (val, str) {
  189. return val + str
  190. }
  191. var filters = _.resolveFilters(vm, [
  192. { name: 'test', args: [3] },
  193. { name: 'test2', args: ['yo']}
  194. ])
  195. var watcher = new Watcher(vm, 'b.c', spy, filters)
  196. expect(watcher.value).toBe('6yo')
  197. vm.b.c = 3
  198. nextTick(function () {
  199. expect(watcher.value).toBe('9yo')
  200. expect(spy).toHaveBeenCalledWith('9yo', '6yo')
  201. done()
  202. })
  203. })
  204. it('setter', function (done) {
  205. vm.$options.filters.test = {
  206. write: function (val, oldVal, arg) {
  207. return val > arg ? val : oldVal
  208. }
  209. }
  210. var filters = _.resolveFilters(vm, [
  211. { name: 'test', args: [5] }
  212. ])
  213. var watcher = new Watcher(vm, 'b["c"]', spy, filters, true)
  214. expect(watcher.value).toBe(2)
  215. watcher.set(4) // shoud not change the value
  216. nextTick(function () {
  217. expect(vm.b.c).toBe(2)
  218. expect(watcher.value).toBe(2)
  219. expect(spy.calls.count()).toBe(0)
  220. watcher.set(6)
  221. nextTick(function () {
  222. expect(vm.b.c).toBe(6)
  223. expect(watcher.value).toBe(6)
  224. expect(spy).toHaveBeenCalledWith(6, 2)
  225. done()
  226. })
  227. })
  228. })
  229. it('set non-existent values', function (done) {
  230. var watcher = new Watcher(vm, 'd.e.f', spy)
  231. expect(watcher.value).toBeUndefined()
  232. watcher.set(123)
  233. nextTick(function () {
  234. expect(vm.d.e.f).toBe(123)
  235. expect(watcher.value).toBe(123)
  236. expect(spy).toHaveBeenCalledWith(123, undefined)
  237. done()
  238. })
  239. })
  240. it('add callback', function (done) {
  241. var watcher = new Watcher(vm, 'a', spy)
  242. var spy2 = jasmine.createSpy()
  243. watcher.addCb(spy2)
  244. vm.a = 99
  245. nextTick(function () {
  246. expect(spy).toHaveBeenCalledWith(99, 1)
  247. expect(spy2).toHaveBeenCalledWith(99, 1)
  248. done()
  249. })
  250. })
  251. it('remove callback', function (done) {
  252. // single, should equal teardown
  253. var fn = function () {}
  254. var watcher = new Watcher(vm, 'a', fn)
  255. watcher.removeCb(fn)
  256. expect(watcher.active).toBe(false)
  257. expect(watcher.vm).toBe(null)
  258. expect(watcher.cbs).toBe(null)
  259. // multiple
  260. watcher = new Watcher(vm, 'a', spy)
  261. var spy2 = jasmine.createSpy()
  262. watcher.addCb(spy2)
  263. watcher.removeCb(spy)
  264. vm.a = 234
  265. nextTick(function () {
  266. expect(spy.calls.count()).toBe(0)
  267. expect(spy2).toHaveBeenCalledWith(234, 1)
  268. done()
  269. })
  270. })
  271. it('teardown', function (done) {
  272. var watcher = new Watcher(vm, 'b.c', spy)
  273. watcher.teardown()
  274. vm.b.c = 3
  275. nextTick(function () {
  276. expect(watcher.active).toBe(false)
  277. expect(watcher.vm).toBe(null)
  278. expect(watcher.cbs).toBe(null)
  279. expect(spy.calls.count()).toBe(0)
  280. done()
  281. })
  282. })
  283. })