apiWatch.spec.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import {
  2. watch,
  3. reactive,
  4. computed,
  5. nextTick,
  6. ref,
  7. h,
  8. OperationTypes
  9. } from '../src/index'
  10. import { render, nodeOps, serializeInner } from '@vue/runtime-test'
  11. import { ITERATE_KEY, DebuggerEvent } from '@vue/reactivity'
  12. // reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
  13. describe('api: watch', () => {
  14. it('basic usage', async () => {
  15. const state = reactive({ count: 0 })
  16. let dummy
  17. watch(() => {
  18. dummy = state.count
  19. })
  20. await nextTick()
  21. expect(dummy).toBe(0)
  22. state.count++
  23. await nextTick()
  24. expect(dummy).toBe(1)
  25. })
  26. it('watching single source: getter', async () => {
  27. const state = reactive({ count: 0 })
  28. let dummy
  29. watch(
  30. () => state.count,
  31. (count, prevCount) => {
  32. dummy = [count, prevCount]
  33. }
  34. )
  35. await nextTick()
  36. expect(dummy).toMatchObject([0, undefined])
  37. state.count++
  38. await nextTick()
  39. expect(dummy).toMatchObject([1, 0])
  40. })
  41. it('watching single source: ref', async () => {
  42. const count = ref(0)
  43. let dummy
  44. watch(count, (count, prevCount) => {
  45. dummy = [count, prevCount]
  46. })
  47. await nextTick()
  48. expect(dummy).toMatchObject([0, undefined])
  49. count.value++
  50. await nextTick()
  51. expect(dummy).toMatchObject([1, 0])
  52. })
  53. it('watching single source: computed ref', async () => {
  54. const count = ref(0)
  55. const plus = computed(() => count.value + 1)
  56. let dummy
  57. watch(plus, (count, prevCount) => {
  58. dummy = [count, prevCount]
  59. })
  60. await nextTick()
  61. expect(dummy).toMatchObject([1, undefined])
  62. count.value++
  63. await nextTick()
  64. expect(dummy).toMatchObject([2, 1])
  65. })
  66. it('watching multiple sources', async () => {
  67. const state = reactive({ count: 1 })
  68. const count = ref(1)
  69. const plus = computed(() => count.value + 1)
  70. let dummy
  71. watch([() => state.count, count, plus], (vals, oldVals) => {
  72. dummy = [vals, oldVals]
  73. })
  74. await nextTick()
  75. expect(dummy).toMatchObject([[1, 1, 2], []])
  76. state.count++
  77. count.value++
  78. await nextTick()
  79. expect(dummy).toMatchObject([[2, 2, 3], [1, 1, 2]])
  80. })
  81. it('stopping the watcher', async () => {
  82. const state = reactive({ count: 0 })
  83. let dummy
  84. const stop = watch(() => {
  85. dummy = state.count
  86. })
  87. await nextTick()
  88. expect(dummy).toBe(0)
  89. stop()
  90. state.count++
  91. await nextTick()
  92. // should not update
  93. expect(dummy).toBe(0)
  94. })
  95. it('cleanup registration (basic)', async () => {
  96. const state = reactive({ count: 0 })
  97. const cleanup = jest.fn()
  98. let dummy
  99. const stop = watch(onCleanup => {
  100. onCleanup(cleanup)
  101. dummy = state.count
  102. })
  103. await nextTick()
  104. expect(dummy).toBe(0)
  105. state.count++
  106. await nextTick()
  107. expect(cleanup).toHaveBeenCalledTimes(1)
  108. expect(dummy).toBe(1)
  109. stop()
  110. expect(cleanup).toHaveBeenCalledTimes(2)
  111. })
  112. it('cleanup registration (with source)', async () => {
  113. const count = ref(0)
  114. const cleanup = jest.fn()
  115. let dummy
  116. const stop = watch(count, (count, prevCount, onCleanup) => {
  117. onCleanup(cleanup)
  118. dummy = count
  119. })
  120. await nextTick()
  121. expect(dummy).toBe(0)
  122. count.value++
  123. await nextTick()
  124. expect(cleanup).toHaveBeenCalledTimes(1)
  125. expect(dummy).toBe(1)
  126. stop()
  127. expect(cleanup).toHaveBeenCalledTimes(2)
  128. })
  129. it('flush timing: post', async () => {
  130. const count = ref(0)
  131. const assertion = jest.fn(count => {
  132. expect(serializeInner(root)).toBe(`${count}`)
  133. })
  134. const Comp = {
  135. setup() {
  136. watch(() => {
  137. assertion(count.value)
  138. })
  139. return () => count.value
  140. }
  141. }
  142. const root = nodeOps.createElement('div')
  143. render(h(Comp), root)
  144. await nextTick()
  145. expect(assertion).toHaveBeenCalledTimes(1)
  146. count.value++
  147. await nextTick()
  148. expect(assertion).toHaveBeenCalledTimes(2)
  149. })
  150. it('flush timing: pre', async () => {
  151. const count = ref(0)
  152. const count2 = ref(0)
  153. let callCount = 0
  154. const assertion = jest.fn((count, count2Value) => {
  155. callCount++
  156. // on mount, the watcher callback should be called before DOM render
  157. // on update, should be called before the count is updated
  158. const expectedDOM = callCount === 1 ? `` : `${count - 1}`
  159. expect(serializeInner(root)).toBe(expectedDOM)
  160. // in a pre-flush callback, all state should have been updated
  161. const expectedState = callCount === 1 ? 0 : 1
  162. expect(count2Value).toBe(expectedState)
  163. })
  164. const Comp = {
  165. setup() {
  166. watch(
  167. () => {
  168. assertion(count.value, count2.value)
  169. },
  170. {
  171. flush: 'pre'
  172. }
  173. )
  174. return () => count.value
  175. }
  176. }
  177. const root = nodeOps.createElement('div')
  178. render(h(Comp), root)
  179. await nextTick()
  180. expect(assertion).toHaveBeenCalledTimes(1)
  181. count.value++
  182. count2.value++
  183. await nextTick()
  184. // two mutations should result in 1 callback execution
  185. expect(assertion).toHaveBeenCalledTimes(2)
  186. })
  187. it('flush timing: sync', async () => {
  188. const count = ref(0)
  189. const count2 = ref(0)
  190. let callCount = 0
  191. const assertion = jest.fn(count => {
  192. callCount++
  193. // on mount, the watcher callback should be called before DOM render
  194. // on update, should be called before the count is updated
  195. const expectedDOM = callCount === 1 ? `` : `${count - 1}`
  196. expect(serializeInner(root)).toBe(expectedDOM)
  197. // in a sync callback, state mutation on the next line should not have
  198. // executed yet on the 2nd call, but will be on the 3rd call.
  199. const expectedState = callCount < 3 ? 0 : 1
  200. expect(count2.value).toBe(expectedState)
  201. })
  202. const Comp = {
  203. setup() {
  204. watch(
  205. () => {
  206. assertion(count.value)
  207. },
  208. {
  209. flush: 'sync'
  210. }
  211. )
  212. return () => count.value
  213. }
  214. }
  215. const root = nodeOps.createElement('div')
  216. render(h(Comp), root)
  217. await nextTick()
  218. expect(assertion).toHaveBeenCalledTimes(1)
  219. count.value++
  220. count2.value++
  221. await nextTick()
  222. expect(assertion).toHaveBeenCalledTimes(3)
  223. })
  224. it('deep', async () => {
  225. const state = reactive({
  226. nested: {
  227. count: ref(0)
  228. },
  229. array: [1, 2, 3],
  230. map: new Map([['a', 1], ['b', 2]]),
  231. set: new Set([1, 2, 3])
  232. })
  233. let dummy
  234. watch(
  235. () => state,
  236. state => {
  237. dummy = [
  238. state.nested.count,
  239. state.array[0],
  240. state.map.get('a'),
  241. state.set.has(1)
  242. ]
  243. },
  244. { deep: true }
  245. )
  246. await nextTick()
  247. expect(dummy).toEqual([0, 1, 1, true])
  248. state.nested.count++
  249. await nextTick()
  250. expect(dummy).toEqual([1, 1, 1, true])
  251. // nested array mutation
  252. state.array[0] = 2
  253. await nextTick()
  254. expect(dummy).toEqual([1, 2, 1, true])
  255. // nested map mutation
  256. state.map.set('a', 2)
  257. await nextTick()
  258. expect(dummy).toEqual([1, 2, 2, true])
  259. // nested set mutation
  260. state.set.delete(1)
  261. await nextTick()
  262. expect(dummy).toEqual([1, 2, 2, false])
  263. })
  264. it('lazy', async () => {
  265. const count = ref(0)
  266. const cb = jest.fn()
  267. watch(count, cb, { lazy: true })
  268. await nextTick()
  269. expect(cb).not.toHaveBeenCalled()
  270. count.value++
  271. await nextTick()
  272. expect(cb).toHaveBeenCalled()
  273. })
  274. it('onTrack', async () => {
  275. const events: DebuggerEvent[] = []
  276. let dummy
  277. const onTrack = jest.fn((e: DebuggerEvent) => {
  278. events.push(e)
  279. })
  280. const obj = reactive({ foo: 1, bar: 2 })
  281. watch(
  282. () => {
  283. dummy = [obj.foo, 'bar' in obj, Object.keys(obj)]
  284. },
  285. { onTrack }
  286. )
  287. await nextTick()
  288. expect(dummy).toEqual([1, true, ['foo', 'bar']])
  289. expect(onTrack).toHaveBeenCalledTimes(3)
  290. expect(events).toMatchObject([
  291. {
  292. target: obj,
  293. type: OperationTypes.GET,
  294. key: 'foo'
  295. },
  296. {
  297. target: obj,
  298. type: OperationTypes.HAS,
  299. key: 'bar'
  300. },
  301. {
  302. target: obj,
  303. type: OperationTypes.ITERATE,
  304. key: ITERATE_KEY
  305. }
  306. ])
  307. })
  308. it('onTrigger', async () => {
  309. const events: DebuggerEvent[] = []
  310. let dummy
  311. const onTrigger = jest.fn((e: DebuggerEvent) => {
  312. events.push(e)
  313. })
  314. const obj = reactive({ foo: 1 })
  315. watch(
  316. () => {
  317. dummy = obj.foo
  318. },
  319. { onTrigger }
  320. )
  321. await nextTick()
  322. expect(dummy).toBe(1)
  323. obj.foo++
  324. await nextTick()
  325. expect(dummy).toBe(2)
  326. expect(onTrigger).toHaveBeenCalledTimes(1)
  327. expect(events[0]).toMatchObject({
  328. type: OperationTypes.SET,
  329. key: 'foo',
  330. oldValue: 1,
  331. newValue: 2
  332. })
  333. delete obj.foo
  334. await nextTick()
  335. expect(dummy).toBeUndefined()
  336. expect(onTrigger).toHaveBeenCalledTimes(2)
  337. expect(events[1]).toMatchObject({
  338. type: OperationTypes.DELETE,
  339. key: 'foo',
  340. oldValue: 2
  341. })
  342. })
  343. })