watcher_spec.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. var Vue = require('../../../src/index')
  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. spyWarns()
  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 = new Vue({ parent: vm })
  123. var watcher3 = new Watcher(child2, 'd.e', spy)
  124. expect(watcher3.value).toBeUndefined()
  125. vm.$set('d', { e: 123 })
  126. _.set(vm.b, '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. twoWay: true
  242. })
  243. expect(watcher.value).toBeUndefined()
  244. watcher.set(123)
  245. nextTick(function () {
  246. expect(vm.d.e.f).toBe(123)
  247. expect(watcher.value).toBe(123)
  248. expect(spy).toHaveBeenCalledWith(123, undefined)
  249. done()
  250. })
  251. })
  252. it('deep watch', function (done) {
  253. new Watcher(vm, 'b', spy, {
  254. deep: true
  255. })
  256. vm.b.c = { d: 4 }
  257. nextTick(function () {
  258. expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
  259. var oldB = vm.b
  260. vm.b = { c: [{a: 1}]}
  261. nextTick(function () {
  262. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  263. expect(spy.calls.count()).toBe(2)
  264. vm.b.c[0].a = 2
  265. nextTick(function () {
  266. expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
  267. expect(spy.calls.count()).toBe(3)
  268. done()
  269. })
  270. })
  271. })
  272. })
  273. it('fire change for prop addition/deletion in non-deep mode', function (done) {
  274. new Watcher(vm, 'b', spy)
  275. Vue.set(vm.b, 'e', 123)
  276. nextTick(function () {
  277. expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
  278. expect(spy.calls.count()).toBe(1)
  279. Vue.delete(vm.b, 'e')
  280. nextTick(function () {
  281. expect(spy.calls.count()).toBe(2)
  282. done()
  283. })
  284. })
  285. })
  286. it('watch function', function (done) {
  287. var watcher = new Watcher(vm, function () {
  288. return this.a + this.b.d
  289. }, spy)
  290. expect(watcher.value).toBe(5)
  291. vm.a = 2
  292. nextTick(function () {
  293. expect(spy).toHaveBeenCalledWith(6, 5)
  294. vm.b = { d: 2 }
  295. nextTick(function () {
  296. expect(spy).toHaveBeenCalledWith(4, 6)
  297. done()
  298. })
  299. })
  300. })
  301. it('lazy mode', function (done) {
  302. var watcher = new Watcher(vm, function () {
  303. return this.a + this.b.d
  304. }, null, { lazy: true })
  305. expect(watcher.lazy).toBe(true)
  306. expect(watcher.value).toBeUndefined()
  307. expect(watcher.dirty).toBe(true)
  308. watcher.evaluate()
  309. expect(watcher.value).toBe(5)
  310. expect(watcher.dirty).toBe(false)
  311. vm.a = 2
  312. nextTick(function () {
  313. expect(watcher.value).toBe(5)
  314. expect(watcher.dirty).toBe(true)
  315. watcher.evaluate()
  316. expect(watcher.value).toBe(6)
  317. expect(watcher.dirty).toBe(false)
  318. done()
  319. })
  320. })
  321. it('teardown', function (done) {
  322. var watcher = new Watcher(vm, 'b.c', spy)
  323. watcher.teardown()
  324. vm.b.c = 3
  325. nextTick(function () {
  326. expect(watcher.active).toBe(false)
  327. expect(watcher.vm).toBe(null)
  328. expect(watcher.cb).toBe(null)
  329. expect(spy).not.toHaveBeenCalled()
  330. done()
  331. })
  332. })
  333. it('synchronous updates', function () {
  334. config.async = false
  335. new Watcher(vm, 'a', spy)
  336. vm.a = 2
  337. vm.a = 3
  338. expect(spy.calls.count()).toBe(2)
  339. expect(spy).toHaveBeenCalledWith(2, 1)
  340. expect(spy).toHaveBeenCalledWith(3, 2)
  341. config.async = true
  342. })
  343. it('warn getter errors', function () {
  344. new Watcher(vm, 'd.e + c', spy)
  345. expect(hasWarned('Error when evaluating expression')).toBe(true)
  346. })
  347. it('warn setter errors', function () {
  348. var watcher = new Watcher(vm, 'a + b', spy)
  349. watcher.set(123)
  350. expect(hasWarned('Error when evaluating setter')).toBe(true)
  351. })
  352. })