watcher_spec.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. _.defineReactive(vm, '$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, $set 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 should not affect isolated children
  122. var child2 = vm.$addChild()
  123. var watcher3 = new Watcher(child2, 'd.e', spy)
  124. expect(watcher3.value).toBeUndefined()
  125. vm.$set('d', { e: 123 })
  126. vm.b.$set('e', 234)
  127. nextTick(function () {
  128. expect(watcher.value).toBe(123)
  129. expect(watcher2.value).toBe(234)
  130. expect(watcher3.value).toBeUndefined()
  131. expect(spy.calls.count()).toBe(2)
  132. expect(spy).toHaveBeenCalledWith(123, undefined)
  133. expect(spy).toHaveBeenCalledWith(234, undefined)
  134. done()
  135. })
  136. })
  137. it('$delete', function (done) {
  138. var watcher = new Watcher(vm, 'b.c', spy)
  139. expect(watcher.value).toBe(2)
  140. vm.$delete('b')
  141. nextTick(function () {
  142. expect(watcher.value).toBeUndefined()
  143. expect(spy).toHaveBeenCalledWith(undefined, 2)
  144. done()
  145. })
  146. })
  147. it('swapping $data', function (done) {
  148. // existing path
  149. var watcher = new Watcher(vm, 'b.c', spy)
  150. var spy2 = jasmine.createSpy()
  151. // non-existing path
  152. var watcher2 = new Watcher(vm, 'e', spy2)
  153. expect(watcher.value).toBe(2)
  154. expect(watcher2.value).toBeUndefined()
  155. vm.$data = { b: { c: 3}, e: 4 }
  156. nextTick(function () {
  157. expect(watcher.value).toBe(3)
  158. expect(watcher2.value).toBe(4)
  159. expect(spy).toHaveBeenCalledWith(3, 2)
  160. expect(spy2).toHaveBeenCalledWith(4, undefined)
  161. done()
  162. })
  163. })
  164. it('path containing $data', function (done) {
  165. var watcher = new Watcher(vm, '$data.b.c', spy)
  166. expect(watcher.value).toBe(2)
  167. vm.b = { c: 3 }
  168. nextTick(function () {
  169. expect(watcher.value).toBe(3)
  170. expect(spy).toHaveBeenCalledWith(3, 2)
  171. vm.$data = { b: {c: 4}}
  172. nextTick(function () {
  173. expect(watcher.value).toBe(4)
  174. expect(spy).toHaveBeenCalledWith(4, 3)
  175. done()
  176. })
  177. })
  178. })
  179. it('watching $data', function (done) {
  180. var oldData = vm.$data
  181. var watcher = new Watcher(vm, '$data', spy)
  182. expect(watcher.value).toBe(oldData)
  183. var newData = {}
  184. vm.$data = newData
  185. nextTick(function () {
  186. expect(spy).toHaveBeenCalledWith(newData, oldData)
  187. expect(watcher.value).toBe(newData)
  188. done()
  189. })
  190. })
  191. it('filters', function (done) {
  192. vm.$options.filters.test = function (val, multi) {
  193. return val * multi
  194. }
  195. vm.$options.filters.test2 = function (val, str) {
  196. return val + str
  197. }
  198. var watcher = new Watcher(vm, 'b.c', spy, {
  199. filters: [
  200. { name: 'test', args: [{value: 3, dynamic: false}]},
  201. { name: 'test2', args: [{value: 'msg', dynamic: true}]}
  202. ]
  203. })
  204. expect(watcher.value).toBe('6yo')
  205. vm.b.c = 3
  206. nextTick(function () {
  207. expect(watcher.value).toBe('9yo')
  208. expect(spy).toHaveBeenCalledWith('9yo', '6yo')
  209. done()
  210. })
  211. })
  212. it('setter', function (done) {
  213. vm.$options.filters.test = {
  214. write: function (val, oldVal, arg) {
  215. return val > arg ? val : oldVal
  216. }
  217. }
  218. var watcher = new Watcher(vm, 'b["c"]', spy, {
  219. filters: [
  220. { name: 'test', args: [{value: 5, dynamic: false}] }
  221. ],
  222. twoWay: true
  223. })
  224. expect(watcher.value).toBe(2)
  225. watcher.set(4) // shoud not change the value
  226. nextTick(function () {
  227. expect(vm.b.c).toBe(2)
  228. expect(watcher.value).toBe(2)
  229. expect(spy).not.toHaveBeenCalled()
  230. watcher.set(6)
  231. nextTick(function () {
  232. expect(vm.b.c).toBe(6)
  233. expect(watcher.value).toBe(6)
  234. expect(spy).toHaveBeenCalledWith(6, 2)
  235. done()
  236. })
  237. })
  238. })
  239. it('set non-existent values', function (done) {
  240. var watcher = new Watcher(vm, 'd.e.f', spy)
  241. expect(watcher.value).toBeUndefined()
  242. watcher.set(123)
  243. nextTick(function () {
  244. expect(vm.d.e.f).toBe(123)
  245. expect(watcher.value).toBe(123)
  246. expect(spy).toHaveBeenCalledWith(123, undefined)
  247. done()
  248. })
  249. })
  250. it('deep watch', function (done) {
  251. new Watcher(vm, 'b', spy, {
  252. deep: true
  253. })
  254. vm.b.c = { d: 4 }
  255. nextTick(function () {
  256. expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
  257. var oldB = vm.b
  258. vm.b = { c: [{a: 1}]}
  259. nextTick(function () {
  260. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  261. expect(spy.calls.count()).toBe(2)
  262. vm.b.c[0].a = 2
  263. nextTick(function () {
  264. expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
  265. expect(spy.calls.count()).toBe(3)
  266. done()
  267. })
  268. })
  269. })
  270. })
  271. it('watch function', function (done) {
  272. var watcher = new Watcher(vm, function () {
  273. return this.a + this.b.d
  274. }, spy)
  275. expect(watcher.value).toBe(5)
  276. vm.a = 2
  277. nextTick(function () {
  278. expect(spy).toHaveBeenCalledWith(6, 5)
  279. vm.b = { d: 2 }
  280. nextTick(function () {
  281. expect(spy).toHaveBeenCalledWith(4, 6)
  282. done()
  283. })
  284. })
  285. })
  286. it('lazy mode', function (done) {
  287. var watcher = new Watcher(vm, function () {
  288. return this.a + this.b.d
  289. }, null, { lazy: true })
  290. expect(watcher.lazy).toBe(true)
  291. expect(watcher.value).toBeUndefined()
  292. expect(watcher.dirty).toBe(true)
  293. watcher.evaluate()
  294. expect(watcher.value).toBe(5)
  295. expect(watcher.dirty).toBe(false)
  296. vm.a = 2
  297. nextTick(function () {
  298. expect(watcher.value).toBe(5)
  299. expect(watcher.dirty).toBe(true)
  300. watcher.evaluate()
  301. expect(watcher.value).toBe(6)
  302. expect(watcher.dirty).toBe(false)
  303. done()
  304. })
  305. })
  306. it('teardown', function (done) {
  307. var watcher = new Watcher(vm, 'b.c', spy)
  308. watcher.teardown()
  309. vm.b.c = 3
  310. nextTick(function () {
  311. expect(watcher.active).toBe(false)
  312. expect(watcher.vm).toBe(null)
  313. expect(watcher.cb).toBe(null)
  314. expect(spy).not.toHaveBeenCalled()
  315. done()
  316. })
  317. })
  318. it('synchronous updates', function () {
  319. config.async = false
  320. new Watcher(vm, 'a', spy)
  321. vm.a = 2
  322. vm.a = 3
  323. expect(spy.calls.count()).toBe(2)
  324. expect(spy).toHaveBeenCalledWith(2, 1)
  325. expect(spy).toHaveBeenCalledWith(3, 2)
  326. config.async = true
  327. })
  328. it('warn getter errors', function () {
  329. new Watcher(vm, 'd.e + c', spy)
  330. expect(hasWarned(_, 'Error when evaluating expression')).toBe(true)
  331. })
  332. it('warn setter errors', function () {
  333. var watcher = new Watcher(vm, 'a + b', spy)
  334. watcher.set(123)
  335. expect(hasWarned(_, 'Error when evaluating setter')).toBe(true)
  336. })
  337. })