watcher_spec.js 10 KB

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