watcher_spec.js 9.4 KB

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