watcher_spec.js 10 KB

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