watcher_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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, {
  227. filters: filters
  228. })
  229. expect(watcher.value).toBe('6yo')
  230. vm.b.c = 3
  231. nextTick(function () {
  232. expect(watcher.value).toBe('9yo')
  233. expect(spy).toHaveBeenCalledWith('9yo', '6yo')
  234. done()
  235. })
  236. })
  237. it('setter', function (done) {
  238. vm.$options.filters.test = {
  239. write: function (val, oldVal, arg) {
  240. return val > arg ? val : oldVal
  241. }
  242. }
  243. var filters = _.resolveFilters(vm, [
  244. { name: 'test', args: [5] }
  245. ])
  246. var watcher = new Watcher(vm, 'b["c"]', spy, {
  247. filters: filters,
  248. twoWay: true
  249. })
  250. expect(watcher.value).toBe(2)
  251. watcher.set(4) // shoud not change the value
  252. nextTick(function () {
  253. expect(vm.b.c).toBe(2)
  254. expect(watcher.value).toBe(2)
  255. expect(spy).not.toHaveBeenCalled()
  256. watcher.set(6)
  257. nextTick(function () {
  258. expect(vm.b.c).toBe(6)
  259. expect(watcher.value).toBe(6)
  260. expect(spy).toHaveBeenCalledWith(6, 2)
  261. done()
  262. })
  263. })
  264. })
  265. it('set non-existent values', function (done) {
  266. var watcher = new Watcher(vm, 'd.e.f', spy)
  267. expect(watcher.value).toBeUndefined()
  268. watcher.set(123)
  269. nextTick(function () {
  270. expect(vm.d.e.f).toBe(123)
  271. expect(watcher.value).toBe(123)
  272. expect(spy).toHaveBeenCalledWith(123, undefined)
  273. done()
  274. })
  275. })
  276. it('deep watch', function (done) {
  277. var watcher = new Watcher(vm, 'b', spy, {
  278. deep: true
  279. })
  280. vm.b.c = { d: 4 }
  281. nextTick(function () {
  282. expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
  283. var oldB = vm.b
  284. vm.b = { c: [{a:1}] }
  285. nextTick(function () {
  286. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  287. expect(spy.calls.count()).toBe(2)
  288. vm.b.c[0].a = 2
  289. nextTick(function () {
  290. expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
  291. expect(spy.calls.count()).toBe(3)
  292. done()
  293. })
  294. })
  295. })
  296. })
  297. it('add callback', function (done) {
  298. var watcher = new Watcher(vm, 'a', spy)
  299. var spy2 = jasmine.createSpy()
  300. watcher.addCb(spy2)
  301. vm.a = 99
  302. nextTick(function () {
  303. expect(spy).toHaveBeenCalledWith(99, 1)
  304. expect(spy2).toHaveBeenCalledWith(99, 1)
  305. done()
  306. })
  307. })
  308. it('remove callback', function (done) {
  309. // single, should equal teardown
  310. var fn = function () {}
  311. var watcher = new Watcher(vm, 'a', fn)
  312. watcher.removeCb(fn)
  313. expect(watcher.active).toBe(false)
  314. expect(watcher.vm).toBe(null)
  315. expect(watcher.cbs).toBe(null)
  316. // multiple
  317. watcher = new Watcher(vm, 'a', spy)
  318. var spy2 = jasmine.createSpy()
  319. watcher.addCb(spy2)
  320. watcher.removeCb(spy)
  321. vm.a = 234
  322. nextTick(function () {
  323. expect(spy).not.toHaveBeenCalled()
  324. expect(spy2).toHaveBeenCalledWith(234, 1)
  325. done()
  326. })
  327. })
  328. it('teardown', function (done) {
  329. var watcher = new Watcher(vm, 'b.c', spy)
  330. watcher.teardown()
  331. vm.b.c = 3
  332. nextTick(function () {
  333. expect(watcher.active).toBe(false)
  334. expect(watcher.vm).toBe(null)
  335. expect(watcher.cbs).toBe(null)
  336. expect(spy).not.toHaveBeenCalled()
  337. done()
  338. })
  339. })
  340. it('synchronous updates', function () {
  341. config.async = false
  342. var watcher = new Watcher(vm, 'a', spy)
  343. vm.a = 2
  344. vm.a = 3
  345. expect(spy.calls.count()).toBe(2)
  346. expect(spy).toHaveBeenCalledWith(2, 1)
  347. expect(spy).toHaveBeenCalledWith(3, 2)
  348. config.async = true
  349. })
  350. it('handle a cb that triggers removeCb', function () {
  351. var watcher = new Watcher(vm, 'a', spy)
  352. watcher.addCb(function () {
  353. watcher.removeCb(spy)
  354. })
  355. watcher.addCb(function () {})
  356. config.async = false
  357. expect(function () {
  358. vm.a = 2
  359. }).not.toThrow()
  360. config.async = true
  361. expect(spy).toHaveBeenCalled()
  362. expect(watcher.cbs.length).toBe(2)
  363. })
  364. it('warn getter errors', function () {
  365. var watcher = new Watcher(vm, 'd.e + c', spy)
  366. expect(_.warn).toHaveBeenCalled()
  367. })
  368. it('warn setter errors', function () {
  369. var watcher = new Watcher(vm, 'a + b', spy)
  370. watcher.set(123)
  371. expect(_.warn).toHaveBeenCalled()
  372. })
  373. })