effectScope.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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(1)
  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. })
  196. })
  197. c!.value // computed is lazy so trigger collection
  198. expect(computedSpy).toHaveBeenCalledTimes(1)
  199. expect(watchSpy).toHaveBeenCalledTimes(0)
  200. expect(watchEffectSpy).toHaveBeenCalledTimes(1)
  201. r.value++
  202. c!.value
  203. await nextTick()
  204. expect(computedSpy).toHaveBeenCalledTimes(2)
  205. expect(watchSpy).toHaveBeenCalledTimes(1)
  206. expect(watchEffectSpy).toHaveBeenCalledTimes(2)
  207. scope.stop()
  208. r.value++
  209. c!.value
  210. await nextTick()
  211. // should not trigger anymore
  212. expect(computedSpy).toHaveBeenCalledTimes(2)
  213. expect(watchSpy).toHaveBeenCalledTimes(1)
  214. expect(watchEffectSpy).toHaveBeenCalledTimes(2)
  215. })
  216. it('getCurrentScope() stays valid when running a detached nested EffectScope', () => {
  217. const parentScope = effectScope()
  218. parentScope.run(() => {
  219. const currentScope = getCurrentScope()
  220. expect(currentScope).toBeDefined()
  221. const detachedScope = effectScope(true)
  222. detachedScope.run(() => {})
  223. expect(getCurrentScope()).toBe(currentScope)
  224. })
  225. })
  226. it('calling .off() of a detached scope inside an active scope should not break currentScope', () => {
  227. const parentScope = effectScope()
  228. parentScope.run(() => {
  229. const childScope = effectScope(true)
  230. childScope.on()
  231. childScope.off()
  232. expect(getCurrentScope()).toBe(parentScope)
  233. })
  234. })
  235. })