effectScope.spec.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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('should pause/resume EffectScope', async () => {
  234. const counter = reactive({ num: 0 })
  235. const fnSpy = vi.fn(() => counter.num)
  236. const scope = new EffectScope()
  237. scope.run(() => {
  238. effect(fnSpy)
  239. })
  240. expect(fnSpy).toHaveBeenCalledTimes(1)
  241. counter.num++
  242. await nextTick()
  243. expect(fnSpy).toHaveBeenCalledTimes(2)
  244. scope.pause()
  245. counter.num++
  246. await nextTick()
  247. expect(fnSpy).toHaveBeenCalledTimes(2)
  248. counter.num++
  249. await nextTick()
  250. expect(fnSpy).toHaveBeenCalledTimes(2)
  251. scope.resume()
  252. expect(fnSpy).toHaveBeenCalledTimes(3)
  253. })
  254. test('removing a watcher while stopping its effectScope', async () => {
  255. const count = ref(0)
  256. const scope = effectScope()
  257. let watcherCalls = 0
  258. let cleanupCalls = 0
  259. scope.run(() => {
  260. const stop1 = watch(count, () => {
  261. watcherCalls++
  262. })
  263. watch(count, (val, old, onCleanup) => {
  264. watcherCalls++
  265. onCleanup(() => {
  266. cleanupCalls++
  267. stop1()
  268. })
  269. })
  270. watch(count, () => {
  271. watcherCalls++
  272. })
  273. })
  274. expect(watcherCalls).toBe(0)
  275. expect(cleanupCalls).toBe(0)
  276. count.value++
  277. await nextTick()
  278. expect(watcherCalls).toBe(3)
  279. expect(cleanupCalls).toBe(0)
  280. scope.stop()
  281. count.value++
  282. await nextTick()
  283. expect(watcherCalls).toBe(3)
  284. expect(cleanupCalls).toBe(1)
  285. expect(scope.effects.length).toBe(0)
  286. expect(scope.cleanups.length).toBe(0)
  287. })
  288. it('should still trigger updates after stopping scope stored in reactive object', () => {
  289. const rs = ref({
  290. stage: 0,
  291. scope: null as any,
  292. })
  293. let renderCount = 0
  294. effect(() => {
  295. renderCount++
  296. return rs.value.stage
  297. })
  298. const handleBegin = () => {
  299. const status = rs.value
  300. status.stage = 1
  301. status.scope = effectScope()
  302. status.scope.run(() => {
  303. watch([() => status.stage], () => {})
  304. })
  305. }
  306. const handleExit = () => {
  307. const status = rs.value
  308. status.stage = 0
  309. const watchScope = status.scope
  310. status.scope = null
  311. if (watchScope) {
  312. watchScope.stop()
  313. }
  314. }
  315. expect(rs.value.stage).toBe(0)
  316. expect(renderCount).toBe(1)
  317. // 1. Click begin
  318. handleBegin()
  319. expect(rs.value.stage).toBe(1)
  320. expect(renderCount).toBe(2)
  321. // 2. Click add
  322. rs.value.stage++
  323. expect(rs.value.stage).toBe(2)
  324. expect(renderCount).toBe(3)
  325. // 3. Click end
  326. handleExit()
  327. expect(rs.value.stage).toBe(0)
  328. expect(renderCount).toBe(4)
  329. handleBegin()
  330. expect(rs.value.stage).toBe(1)
  331. expect(renderCount).toBe(5)
  332. })
  333. })