effectScope.spec.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. import { nextTick, watch, watchEffect } from '@vue/runtime-core'
  2. import {
  3. type ComputedRef,
  4. EffectScope,
  5. computed,
  6. effect,
  7. effectScope,
  8. getCurrentScope,
  9. onScopeDispose,
  10. reactive,
  11. ref,
  12. } from '../src'
  13. describe('reactivity/effect/scope', () => {
  14. it('should run', () => {
  15. const fnSpy = vi.fn(() => {})
  16. effectScope().run(fnSpy)
  17. expect(fnSpy).toHaveBeenCalledTimes(1)
  18. })
  19. it('should accept zero argument', () => {
  20. const scope = effectScope()
  21. expect(scope.effects.length).toBe(0)
  22. })
  23. it('should return run value', () => {
  24. expect(effectScope().run(() => 1)).toBe(1)
  25. })
  26. it('should work w/ active property', () => {
  27. const scope = effectScope()
  28. scope.run(() => 1)
  29. expect(scope.active).toBe(true)
  30. scope.stop()
  31. expect(scope.active).toBe(false)
  32. })
  33. it('should collect the effects', () => {
  34. const scope = effectScope()
  35. scope.run(() => {
  36. let dummy
  37. const counter = reactive({ num: 0 })
  38. effect(() => (dummy = counter.num))
  39. expect(dummy).toBe(0)
  40. counter.num = 7
  41. expect(dummy).toBe(7)
  42. })
  43. expect(scope.effects.length).toBe(1)
  44. })
  45. it('stop', () => {
  46. let dummy, doubled
  47. const counter = reactive({ num: 0 })
  48. const scope = effectScope()
  49. scope.run(() => {
  50. effect(() => (dummy = counter.num))
  51. effect(() => (doubled = counter.num * 2))
  52. })
  53. expect(scope.effects.length).toBe(2)
  54. expect(dummy).toBe(0)
  55. counter.num = 7
  56. expect(dummy).toBe(7)
  57. expect(doubled).toBe(14)
  58. scope.stop()
  59. counter.num = 6
  60. expect(dummy).toBe(7)
  61. expect(doubled).toBe(14)
  62. })
  63. it('should collect nested scope', () => {
  64. let dummy, doubled
  65. const counter = reactive({ num: 0 })
  66. const scope = effectScope()
  67. scope.run(() => {
  68. effect(() => (dummy = counter.num))
  69. // nested scope
  70. effectScope().run(() => {
  71. effect(() => (doubled = counter.num * 2))
  72. })
  73. })
  74. expect(scope.effects.length).toBe(1)
  75. expect(scope.scopes!.length).toBe(1)
  76. expect(scope.scopes![0]).toBeInstanceOf(EffectScope)
  77. expect(dummy).toBe(0)
  78. counter.num = 7
  79. expect(dummy).toBe(7)
  80. expect(doubled).toBe(14)
  81. // stop the nested scope as well
  82. scope.stop()
  83. counter.num = 6
  84. expect(dummy).toBe(7)
  85. expect(doubled).toBe(14)
  86. })
  87. it('nested scope can be escaped', () => {
  88. let dummy, doubled
  89. const counter = reactive({ num: 0 })
  90. const scope = effectScope()
  91. scope.run(() => {
  92. effect(() => (dummy = counter.num))
  93. // nested scope
  94. effectScope(true).run(() => {
  95. effect(() => (doubled = counter.num * 2))
  96. })
  97. })
  98. expect(scope.effects.length).toBe(1)
  99. expect(dummy).toBe(0)
  100. counter.num = 7
  101. expect(dummy).toBe(7)
  102. expect(doubled).toBe(14)
  103. scope.stop()
  104. counter.num = 6
  105. expect(dummy).toBe(7)
  106. // nested scope should not be stopped
  107. expect(doubled).toBe(12)
  108. })
  109. it('able to run the scope', () => {
  110. let dummy, doubled
  111. const counter = reactive({ num: 0 })
  112. const scope = effectScope()
  113. scope.run(() => {
  114. effect(() => (dummy = counter.num))
  115. })
  116. expect(scope.effects.length).toBe(1)
  117. scope.run(() => {
  118. effect(() => (doubled = counter.num * 2))
  119. })
  120. expect(scope.effects.length).toBe(2)
  121. counter.num = 7
  122. expect(dummy).toBe(7)
  123. expect(doubled).toBe(14)
  124. scope.stop()
  125. })
  126. it('can not run an inactive scope', () => {
  127. let dummy, doubled
  128. const counter = reactive({ num: 0 })
  129. const scope = effectScope()
  130. scope.run(() => {
  131. effect(() => (dummy = counter.num))
  132. })
  133. expect(scope.effects.length).toBe(1)
  134. scope.stop()
  135. scope.run(() => {
  136. effect(() => (doubled = counter.num * 2))
  137. })
  138. expect('[Vue warn] cannot run an inactive effect scope.').toHaveBeenWarned()
  139. expect(scope.effects.length).toBe(0)
  140. counter.num = 7
  141. expect(dummy).toBe(0)
  142. expect(doubled).toBe(undefined)
  143. })
  144. it('should fire onScopeDispose hook', () => {
  145. let dummy = 0
  146. const scope = effectScope()
  147. scope.run(() => {
  148. onScopeDispose(() => (dummy += 1))
  149. onScopeDispose(() => (dummy += 2))
  150. })
  151. scope.run(() => {
  152. onScopeDispose(() => (dummy += 4))
  153. })
  154. expect(dummy).toBe(0)
  155. scope.stop()
  156. expect(dummy).toBe(7)
  157. })
  158. it('should warn onScopeDispose() is called when there is no active effect scope', () => {
  159. const spy = vi.fn()
  160. const scope = effectScope()
  161. scope.run(() => {
  162. onScopeDispose(spy)
  163. })
  164. expect(spy).toHaveBeenCalledTimes(0)
  165. onScopeDispose(spy)
  166. expect(
  167. '[Vue warn] onScopeDispose() is called when there is no active effect scope to be associated with.',
  168. ).toHaveBeenWarned()
  169. scope.stop()
  170. expect(spy).toHaveBeenCalledTimes(1)
  171. })
  172. it('should dereference child scope from parent scope after stopping child scope (no memleaks)', () => {
  173. const parent = effectScope()
  174. const child = parent.run(() => effectScope())!
  175. expect(parent.scopes!.includes(child)).toBe(true)
  176. child.stop()
  177. expect(parent.scopes!.includes(child)).toBe(false)
  178. })
  179. it('test with higher level APIs', async () => {
  180. const r = ref(1)
  181. const computedSpy = vi.fn()
  182. const watchSpy = vi.fn()
  183. const watchEffectSpy = vi.fn()
  184. let c: ComputedRef
  185. const scope = effectScope()
  186. scope.run(() => {
  187. c = computed(() => {
  188. computedSpy()
  189. return r.value + 1
  190. })
  191. watch(r, watchSpy)
  192. watchEffect(() => {
  193. watchEffectSpy()
  194. r.value
  195. c.value
  196. })
  197. })
  198. expect(computedSpy).toHaveBeenCalledTimes(1)
  199. expect(watchSpy).toHaveBeenCalledTimes(0)
  200. expect(watchEffectSpy).toHaveBeenCalledTimes(1)
  201. r.value++
  202. await nextTick()
  203. expect(computedSpy).toHaveBeenCalledTimes(2)
  204. expect(watchSpy).toHaveBeenCalledTimes(1)
  205. expect(watchEffectSpy).toHaveBeenCalledTimes(2)
  206. scope.stop()
  207. r.value++
  208. await nextTick()
  209. // should not trigger anymore
  210. expect(computedSpy).toHaveBeenCalledTimes(2)
  211. expect(watchSpy).toHaveBeenCalledTimes(1)
  212. expect(watchEffectSpy).toHaveBeenCalledTimes(2)
  213. })
  214. it('getCurrentScope() stays valid when running a detached nested EffectScope', () => {
  215. const parentScope = effectScope()
  216. parentScope.run(() => {
  217. const currentScope = getCurrentScope()
  218. expect(currentScope).toBeDefined()
  219. const detachedScope = effectScope(true)
  220. detachedScope.run(() => {})
  221. expect(getCurrentScope()).toBe(currentScope)
  222. })
  223. })
  224. it('calling .off() of a detached scope inside an active scope should not break currentScope', () => {
  225. const parentScope = effectScope()
  226. parentScope.run(() => {
  227. const childScope = effectScope(true)
  228. childScope.on()
  229. childScope.off()
  230. expect(getCurrentScope()).toBe(parentScope)
  231. })
  232. })
  233. it('calling .off() out of order should unlink the scope from the active chain', () => {
  234. const parentScope = effectScope(true)
  235. const firstScope = effectScope(true)
  236. const secondScope = effectScope(true)
  237. parentScope.on()
  238. firstScope.on()
  239. secondScope.on()
  240. firstScope.off()
  241. expect(getCurrentScope()).toBe(secondScope)
  242. secondScope.off()
  243. expect(getCurrentScope()).toBe(parentScope)
  244. parentScope.off()
  245. expect(getCurrentScope()).toBeUndefined()
  246. })
  247. it('should pause/resume EffectScope', async () => {
  248. const counter = reactive({ num: 0 })
  249. const fnSpy = vi.fn(() => counter.num)
  250. const scope = new EffectScope()
  251. scope.run(() => {
  252. effect(fnSpy)
  253. })
  254. expect(fnSpy).toHaveBeenCalledTimes(1)
  255. counter.num++
  256. await nextTick()
  257. expect(fnSpy).toHaveBeenCalledTimes(2)
  258. scope.pause()
  259. counter.num++
  260. await nextTick()
  261. expect(fnSpy).toHaveBeenCalledTimes(2)
  262. counter.num++
  263. await nextTick()
  264. expect(fnSpy).toHaveBeenCalledTimes(2)
  265. scope.resume()
  266. expect(fnSpy).toHaveBeenCalledTimes(3)
  267. })
  268. test('removing a watcher while stopping its effectScope', async () => {
  269. const count = ref(0)
  270. const scope = effectScope()
  271. let watcherCalls = 0
  272. let cleanupCalls = 0
  273. scope.run(() => {
  274. const stop1 = watch(count, () => {
  275. watcherCalls++
  276. })
  277. watch(count, (val, old, onCleanup) => {
  278. watcherCalls++
  279. onCleanup(() => {
  280. cleanupCalls++
  281. stop1()
  282. })
  283. })
  284. watch(count, () => {
  285. watcherCalls++
  286. })
  287. })
  288. expect(watcherCalls).toBe(0)
  289. expect(cleanupCalls).toBe(0)
  290. count.value++
  291. await nextTick()
  292. expect(watcherCalls).toBe(3)
  293. expect(cleanupCalls).toBe(0)
  294. scope.stop()
  295. count.value++
  296. await nextTick()
  297. expect(watcherCalls).toBe(3)
  298. expect(cleanupCalls).toBe(1)
  299. expect(scope.effects.length).toBe(0)
  300. expect(scope.cleanups.length).toBe(0)
  301. })
  302. it('should still trigger updates after stopping scope stored in reactive object', () => {
  303. const rs = ref({
  304. stage: 0,
  305. scope: null as any,
  306. })
  307. let renderCount = 0
  308. effect(() => {
  309. renderCount++
  310. return rs.value.stage
  311. })
  312. const handleBegin = () => {
  313. const status = rs.value
  314. status.stage = 1
  315. status.scope = effectScope()
  316. status.scope.run(() => {
  317. watch([() => status.stage], () => {})
  318. })
  319. }
  320. const handleExit = () => {
  321. const status = rs.value
  322. status.stage = 0
  323. const watchScope = status.scope
  324. status.scope = null
  325. if (watchScope) {
  326. watchScope.stop()
  327. }
  328. }
  329. expect(rs.value.stage).toBe(0)
  330. expect(renderCount).toBe(1)
  331. // 1. Click begin
  332. handleBegin()
  333. expect(rs.value.stage).toBe(1)
  334. expect(renderCount).toBe(2)
  335. // 2. Click add
  336. rs.value.stage++
  337. expect(rs.value.stage).toBe(2)
  338. expect(renderCount).toBe(3)
  339. // 3. Click end
  340. handleExit()
  341. expect(rs.value.stage).toBe(0)
  342. expect(renderCount).toBe(4)
  343. handleBegin()
  344. expect(rs.value.stage).toBe(1)
  345. expect(renderCount).toBe(5)
  346. })
  347. })