watcher_spec.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. var Vue = require('../../../src/vue')
  2. var nextTick = Vue.nextTick
  3. var Watcher = require('../../../src/watcher')
  4. var _ = Vue.util
  5. var config = Vue.config
  6. describe('Watcher', function () {
  7. var vm, spy
  8. beforeEach(function () {
  9. vm = new Vue({
  10. filters: {},
  11. data: {
  12. a: 1,
  13. b: {
  14. c: 2,
  15. d: 4
  16. },
  17. c: 'c'
  18. }
  19. })
  20. spy = jasmine.createSpy('watcher')
  21. spyOn(_, 'warn')
  22. })
  23. it('simple path', function (done) {
  24. var watcher = new Watcher(vm, 'b.c', spy)
  25. expect(watcher.value).toBe(2)
  26. vm.b.c = 3
  27. nextTick(function () {
  28. expect(watcher.value).toBe(3)
  29. expect(spy).toHaveBeenCalledWith(3, 2)
  30. vm.b = { c: 4 } // swapping the object
  31. nextTick(function () {
  32. expect(watcher.value).toBe(4)
  33. expect(spy).toHaveBeenCalledWith(4, 3)
  34. done()
  35. })
  36. })
  37. })
  38. it('bracket access path', function (done) {
  39. var watcher = new Watcher(vm, 'b["c"]', spy)
  40. expect(watcher.value).toBe(2)
  41. vm.b.c = 3
  42. nextTick(function () {
  43. expect(watcher.value).toBe(3)
  44. expect(spy).toHaveBeenCalledWith(3, 2)
  45. vm.b = { c: 4 } // swapping the object
  46. nextTick(function () {
  47. expect(watcher.value).toBe(4)
  48. expect(spy).toHaveBeenCalledWith(4, 3)
  49. done()
  50. })
  51. })
  52. })
  53. it('dynamic path', function (done) {
  54. var watcher = new Watcher(vm, 'b[c]', spy)
  55. expect(watcher.value).toBe(2)
  56. vm.b.c = 3
  57. nextTick(function () {
  58. expect(watcher.value).toBe(3)
  59. expect(spy).toHaveBeenCalledWith(3, 2)
  60. vm.c = 'd' // changing the dynamic segment in path
  61. nextTick(function () {
  62. expect(watcher.value).toBe(4)
  63. expect(spy).toHaveBeenCalledWith(4, 3)
  64. done()
  65. })
  66. })
  67. })
  68. it('simple expression', function (done) {
  69. var watcher = new Watcher(vm, 'a + b.c', spy)
  70. expect(watcher.value).toBe(3)
  71. vm.b.c = 3
  72. nextTick(function () {
  73. expect(watcher.value).toBe(4)
  74. expect(spy.calls.count()).toBe(1)
  75. expect(spy).toHaveBeenCalledWith(4, 3)
  76. // change two dependencies at once
  77. vm.a = 2
  78. vm.b.c = 4
  79. nextTick(function () {
  80. expect(watcher.value).toBe(6)
  81. // should trigger only once callback,
  82. // because it was in the same event loop.
  83. expect(spy.calls.count()).toBe(2)
  84. expect(spy).toHaveBeenCalledWith(6, 4)
  85. done()
  86. })
  87. })
  88. })
  89. it('ternary expression', function (done) {
  90. // we're actually testing for the dependency re-calculation here
  91. var watcher = new Watcher(vm, 'a > 1 ? b.c : b.d', spy)
  92. expect(watcher.value).toBe(4)
  93. vm.a = 2
  94. nextTick(function () {
  95. expect(watcher.value).toBe(2)
  96. expect(spy).toHaveBeenCalledWith(2, 4)
  97. vm.b.c = 3
  98. nextTick(function () {
  99. expect(watcher.value).toBe(3)
  100. expect(spy).toHaveBeenCalledWith(3, 2)
  101. done()
  102. })
  103. })
  104. })
  105. it('meta properties', function (done) {
  106. vm._defineMeta('$index', 1)
  107. var watcher = new Watcher(vm, '$index + 1', spy)
  108. expect(watcher.value).toBe(2)
  109. vm.$index = 2
  110. nextTick(function () {
  111. expect(watcher.value).toBe(3)
  112. done()
  113. })
  114. })
  115. it('non-existent path, $add later', function (done) {
  116. var watcher = new Watcher(vm, 'd.e', spy)
  117. var watcher2 = new Watcher(vm, 'b.e', spy)
  118. expect(watcher.value).toBeUndefined()
  119. expect(watcher2.value).toBeUndefined()
  120. // check $add affecting children
  121. var child = vm.$addChild({
  122. inherit: true
  123. })
  124. var watcher3 = new Watcher(child, 'd.e', spy)
  125. var watcher4 = new Watcher(child, 'b.e', spy)
  126. // check $add should not affect isolated children
  127. var child2 = vm.$addChild()
  128. var watcher5 = new Watcher(child2, 'd.e', spy)
  129. expect(watcher5.value).toBeUndefined()
  130. vm.$add('d', { e: 123 })
  131. vm.b.$add('e', 234)
  132. nextTick(function () {
  133. expect(watcher.value).toBe(123)
  134. expect(watcher2.value).toBe(234)
  135. expect(watcher3.value).toBe(123)
  136. expect(watcher4.value).toBe(234)
  137. expect(watcher5.value).toBeUndefined()
  138. expect(spy.calls.count()).toBe(4)
  139. expect(spy).toHaveBeenCalledWith(123, undefined)
  140. expect(spy).toHaveBeenCalledWith(234, undefined)
  141. done()
  142. })
  143. })
  144. it('$delete', function (done) {
  145. var watcher = new Watcher(vm, 'b.c', spy)
  146. expect(watcher.value).toBe(2)
  147. vm.$delete('b')
  148. nextTick(function () {
  149. expect(watcher.value).toBeUndefined()
  150. expect(spy).toHaveBeenCalledWith(undefined, 2)
  151. done()
  152. })
  153. })
  154. it('swapping $data', function (done) {
  155. // existing path
  156. var watcher = new Watcher(vm, 'b.c', spy)
  157. var spy2 = jasmine.createSpy()
  158. // non-existing path
  159. var watcher2 = new Watcher(vm, 'e', spy2)
  160. expect(watcher.value).toBe(2)
  161. expect(watcher2.value).toBeUndefined()
  162. vm.$data = { b: { c: 3}, e: 4 }
  163. nextTick(function () {
  164. expect(watcher.value).toBe(3)
  165. expect(watcher2.value).toBe(4)
  166. expect(spy).toHaveBeenCalledWith(3, 2)
  167. expect(spy2).toHaveBeenCalledWith(4, undefined)
  168. done()
  169. })
  170. })
  171. it('path containing $data', function (done) {
  172. var watcher = new Watcher(vm, '$data.b.c', spy)
  173. expect(watcher.value).toBe(2)
  174. vm.b = { c: 3 }
  175. nextTick(function () {
  176. expect(watcher.value).toBe(3)
  177. expect(spy).toHaveBeenCalledWith(3, 2)
  178. vm.$data = { b: {c: 4}}
  179. nextTick(function () {
  180. expect(watcher.value).toBe(4)
  181. expect(spy).toHaveBeenCalledWith(4, 3)
  182. done()
  183. })
  184. })
  185. })
  186. it('watching $data', function (done) {
  187. var oldData = vm.$data
  188. var watcher = new Watcher(vm, '$data', spy)
  189. expect(watcher.value).toBe(oldData)
  190. var newData = {}
  191. vm.$data = newData
  192. nextTick(function() {
  193. expect(spy).toHaveBeenCalledWith(newData, oldData)
  194. expect(watcher.value).toBe(newData)
  195. done()
  196. })
  197. })
  198. it('watching parent scope properties', function (done) {
  199. var child = vm.$addChild({
  200. inherit: true
  201. })
  202. var spy2 = jasmine.createSpy('watch')
  203. var watcher1 = new Watcher(child, '$data', spy)
  204. var watcher2 = new Watcher(child, 'a', spy2)
  205. vm.a = 123
  206. nextTick(function () {
  207. // $data should only be called on self data change
  208. expect(watcher1.value).toBe(child.$data)
  209. expect(spy).not.toHaveBeenCalled()
  210. expect(watcher2.value).toBe(123)
  211. expect(spy2).toHaveBeenCalledWith(123, 1)
  212. done()
  213. })
  214. })
  215. it('filters', function (done) {
  216. vm.$options.filters.test = function (val, multi) {
  217. return val * multi
  218. }
  219. vm.$options.filters.test2 = function (val, str) {
  220. return val + str
  221. }
  222. var filters = _.resolveFilters(vm, [
  223. { name: 'test', args: [3] },
  224. { name: 'test2', args: ['yo']}
  225. ])
  226. var watcher = new Watcher(vm, 'b.c', spy, filters)
  227. expect(watcher.value).toBe('6yo')
  228. vm.b.c = 3
  229. nextTick(function () {
  230. expect(watcher.value).toBe('9yo')
  231. expect(spy).toHaveBeenCalledWith('9yo', '6yo')
  232. done()
  233. })
  234. })
  235. it('setter', function (done) {
  236. vm.$options.filters.test = {
  237. write: function (val, oldVal, arg) {
  238. return val > arg ? val : oldVal
  239. }
  240. }
  241. var filters = _.resolveFilters(vm, [
  242. { name: 'test', args: [5] }
  243. ])
  244. var watcher = new Watcher(vm, 'b["c"]', spy, filters, true)
  245. expect(watcher.value).toBe(2)
  246. watcher.set(4) // shoud not change the value
  247. nextTick(function () {
  248. expect(vm.b.c).toBe(2)
  249. expect(watcher.value).toBe(2)
  250. expect(spy).not.toHaveBeenCalled()
  251. watcher.set(6)
  252. nextTick(function () {
  253. expect(vm.b.c).toBe(6)
  254. expect(watcher.value).toBe(6)
  255. expect(spy).toHaveBeenCalledWith(6, 2)
  256. done()
  257. })
  258. })
  259. })
  260. it('set non-existent values', function (done) {
  261. var watcher = new Watcher(vm, 'd.e.f', spy)
  262. expect(watcher.value).toBeUndefined()
  263. watcher.set(123)
  264. nextTick(function () {
  265. expect(vm.d.e.f).toBe(123)
  266. expect(watcher.value).toBe(123)
  267. expect(spy).toHaveBeenCalledWith(123, undefined)
  268. done()
  269. })
  270. })
  271. it('deep watch', function (done) {
  272. var watcher = new Watcher(vm, 'b', spy, null, false, true)
  273. vm.b.c = 3
  274. nextTick(function () {
  275. expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
  276. var oldB = vm.b
  277. vm.b = { c: 4 }
  278. nextTick(function () {
  279. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  280. expect(spy.calls.count()).toBe(2)
  281. done()
  282. })
  283. })
  284. })
  285. it('add callback', function (done) {
  286. var watcher = new Watcher(vm, 'a', spy)
  287. var spy2 = jasmine.createSpy()
  288. watcher.addCb(spy2)
  289. vm.a = 99
  290. nextTick(function () {
  291. expect(spy).toHaveBeenCalledWith(99, 1)
  292. expect(spy2).toHaveBeenCalledWith(99, 1)
  293. done()
  294. })
  295. })
  296. it('remove callback', function (done) {
  297. // single, should equal teardown
  298. var fn = function () {}
  299. var watcher = new Watcher(vm, 'a', fn)
  300. watcher.removeCb(fn)
  301. expect(watcher.active).toBe(false)
  302. expect(watcher.vm).toBe(null)
  303. expect(watcher.cbs).toBe(null)
  304. // multiple
  305. watcher = new Watcher(vm, 'a', spy)
  306. var spy2 = jasmine.createSpy()
  307. watcher.addCb(spy2)
  308. watcher.removeCb(spy)
  309. vm.a = 234
  310. nextTick(function () {
  311. expect(spy).not.toHaveBeenCalled()
  312. expect(spy2).toHaveBeenCalledWith(234, 1)
  313. done()
  314. })
  315. })
  316. it('teardown', function (done) {
  317. var watcher = new Watcher(vm, 'b.c', spy)
  318. watcher.teardown()
  319. vm.b.c = 3
  320. nextTick(function () {
  321. expect(watcher.active).toBe(false)
  322. expect(watcher.vm).toBe(null)
  323. expect(watcher.cbs).toBe(null)
  324. expect(spy).not.toHaveBeenCalled()
  325. done()
  326. })
  327. })
  328. it('synchronous updates', function () {
  329. config.async = false
  330. var watcher = new Watcher(vm, 'a', spy)
  331. vm.a = 2
  332. vm.a = 3
  333. expect(spy.calls.count()).toBe(2)
  334. expect(spy).toHaveBeenCalledWith(2, 1)
  335. expect(spy).toHaveBeenCalledWith(3, 2)
  336. config.async = true
  337. })
  338. it('handle a cb that triggers removeCb', function () {
  339. var watcher = new Watcher(vm, 'a', spy)
  340. watcher.addCb(function () {
  341. watcher.removeCb(spy)
  342. })
  343. watcher.addCb(function () {})
  344. config.async = false
  345. expect(function () {
  346. vm.a = 2
  347. }).not.toThrow()
  348. config.async = true
  349. expect(spy).toHaveBeenCalled()
  350. expect(watcher.cbs.length).toBe(2)
  351. })
  352. it('warn getter errors', function () {
  353. var watcher = new Watcher(vm, 'd.e + c', spy)
  354. expect(_.warn).toHaveBeenCalled()
  355. })
  356. })