effectScope.spec.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import { nextTick, watch, watchEffect } from '@vue/runtime-core'
  2. import {
  3. type ComputedRef,
  4. EffectScope,
  5. computed,
  6. effect,
  7. getCurrentScope,
  8. onScopeDispose,
  9. reactive,
  10. ref,
  11. } from '../src'
  12. describe('reactivity/effect/scope', () => {
  13. it('should run', () => {
  14. const fnSpy = vi.fn(() => {})
  15. new EffectScope().run(fnSpy)
  16. expect(fnSpy).toHaveBeenCalledTimes(1)
  17. })
  18. it('should accept zero argument', () => {
  19. const scope = new EffectScope()
  20. expect(scope.effects.length).toBe(0)
  21. })
  22. it('should return run value', () => {
  23. expect(new EffectScope().run(() => 1)).toBe(1)
  24. })
  25. it('should work w/ active property', () => {
  26. const scope = new EffectScope()
  27. scope.run(() => 1)
  28. expect(scope.active).toBe(true)
  29. scope.stop()
  30. expect(scope.active).toBe(false)
  31. })
  32. it('should collect the effects', () => {
  33. const scope = new EffectScope()
  34. scope.run(() => {
  35. let dummy
  36. const counter = reactive({ num: 0 })
  37. effect(() => (dummy = counter.num))
  38. expect(dummy).toBe(0)
  39. counter.num = 7
  40. expect(dummy).toBe(7)
  41. })
  42. expect(scope.effects.length).toBe(1)
  43. })
  44. it('stop', () => {
  45. let dummy, doubled
  46. const counter = reactive({ num: 0 })
  47. const scope = new EffectScope()
  48. scope.run(() => {
  49. effect(() => (dummy = counter.num))
  50. effect(() => (doubled = counter.num * 2))
  51. })
  52. expect(scope.effects.length).toBe(2)
  53. expect(dummy).toBe(0)
  54. counter.num = 7
  55. expect(dummy).toBe(7)
  56. expect(doubled).toBe(14)
  57. scope.stop()
  58. counter.num = 6
  59. expect(dummy).toBe(7)
  60. expect(doubled).toBe(14)
  61. })
  62. it('should collect nested scope', () => {
  63. let dummy, doubled
  64. const counter = reactive({ num: 0 })
  65. const scope = new EffectScope()
  66. scope.run(() => {
  67. effect(() => (dummy = counter.num))
  68. // nested scope
  69. new EffectScope().run(() => {
  70. effect(() => (doubled = counter.num * 2))
  71. })
  72. })
  73. expect(scope.effects.length).toBe(1)
  74. expect(scope.scopes!.length).toBe(1)
  75. expect(scope.scopes![0]).toBeInstanceOf(EffectScope)
  76. expect(dummy).toBe(0)
  77. counter.num = 7
  78. expect(dummy).toBe(7)
  79. expect(doubled).toBe(14)
  80. // stop the nested scope as well
  81. scope.stop()
  82. counter.num = 6
  83. expect(dummy).toBe(7)
  84. expect(doubled).toBe(14)
  85. })
  86. it('nested scope can be escaped', () => {
  87. let dummy, doubled
  88. const counter = reactive({ num: 0 })
  89. const scope = new EffectScope()
  90. scope.run(() => {
  91. effect(() => (dummy = counter.num))
  92. // nested scope
  93. new EffectScope(true).run(() => {
  94. effect(() => (doubled = counter.num * 2))
  95. })
  96. })
  97. expect(scope.effects.length).toBe(1)
  98. expect(dummy).toBe(0)
  99. counter.num = 7
  100. expect(dummy).toBe(7)
  101. expect(doubled).toBe(14)
  102. scope.stop()
  103. counter.num = 6
  104. expect(dummy).toBe(7)
  105. // nested scope should not be stopped
  106. expect(doubled).toBe(12)
  107. })
  108. it('able to run the scope', () => {
  109. let dummy, doubled
  110. const counter = reactive({ num: 0 })
  111. const scope = new EffectScope()
  112. scope.run(() => {
  113. effect(() => (dummy = counter.num))
  114. })
  115. expect(scope.effects.length).toBe(1)
  116. scope.run(() => {
  117. effect(() => (doubled = counter.num * 2))
  118. })
  119. expect(scope.effects.length).toBe(2)
  120. counter.num = 7
  121. expect(dummy).toBe(7)
  122. expect(doubled).toBe(14)
  123. scope.stop()
  124. })
  125. it('can not run an inactive scope', () => {
  126. let dummy, doubled
  127. const counter = reactive({ num: 0 })
  128. const scope = new EffectScope()
  129. scope.run(() => {
  130. effect(() => (dummy = counter.num))
  131. })
  132. expect(scope.effects.length).toBe(1)
  133. scope.stop()
  134. scope.run(() => {
  135. effect(() => (doubled = counter.num * 2))
  136. })
  137. expect('[Vue warn] cannot run an inactive effect scope.').toHaveBeenWarned()
  138. expect(scope.effects.length).toBe(1)
  139. counter.num = 7
  140. expect(dummy).toBe(0)
  141. expect(doubled).toBe(undefined)
  142. })
  143. it('should fire onScopeDispose hook', () => {
  144. let dummy = 0
  145. const scope = new EffectScope()
  146. scope.run(() => {
  147. onScopeDispose(() => (dummy += 1))
  148. onScopeDispose(() => (dummy += 2))
  149. })
  150. scope.run(() => {
  151. onScopeDispose(() => (dummy += 4))
  152. })
  153. expect(dummy).toBe(0)
  154. scope.stop()
  155. expect(dummy).toBe(7)
  156. })
  157. it('should warn onScopeDispose() is called when there is no active effect scope', () => {
  158. const spy = vi.fn()
  159. const scope = new EffectScope()
  160. scope.run(() => {
  161. onScopeDispose(spy)
  162. })
  163. expect(spy).toHaveBeenCalledTimes(0)
  164. onScopeDispose(spy)
  165. expect(
  166. '[Vue warn] onScopeDispose() is called when there is no active effect scope to be associated with.',
  167. ).toHaveBeenWarned()
  168. scope.stop()
  169. expect(spy).toHaveBeenCalledTimes(1)
  170. })
  171. it('should dereference child scope from parent scope after stopping child scope (no memleaks)', () => {
  172. const parent = new EffectScope()
  173. const child = parent.run(() => new EffectScope())!
  174. expect(parent.scopes!.includes(child)).toBe(true)
  175. child.stop()
  176. expect(parent.scopes!.includes(child)).toBe(false)
  177. })
  178. it('test with higher level APIs', async () => {
  179. const r = ref(1)
  180. const computedSpy = vi.fn()
  181. const watchSpy = vi.fn()
  182. const watchEffectSpy = vi.fn()
  183. let c: ComputedRef
  184. const scope = new EffectScope()
  185. scope.run(() => {
  186. c = computed(() => {
  187. computedSpy()
  188. return r.value + 1
  189. })
  190. watch(r, watchSpy)
  191. watchEffect(() => {
  192. watchEffectSpy()
  193. r.value
  194. })
  195. })
  196. c!.value // computed is lazy so trigger collection
  197. expect(computedSpy).toHaveBeenCalledTimes(1)
  198. expect(watchSpy).toHaveBeenCalledTimes(0)
  199. expect(watchEffectSpy).toHaveBeenCalledTimes(1)
  200. r.value++
  201. c!.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. c!.value
  209. await nextTick()
  210. // should not trigger anymore
  211. expect(computedSpy).toHaveBeenCalledTimes(2)
  212. expect(watchSpy).toHaveBeenCalledTimes(1)
  213. expect(watchEffectSpy).toHaveBeenCalledTimes(2)
  214. })
  215. it('getCurrentScope() stays valid when running a detached nested EffectScope', () => {
  216. const parentScope = new EffectScope()
  217. parentScope.run(() => {
  218. const currentScope = getCurrentScope()
  219. expect(currentScope).toBeDefined()
  220. const detachedScope = new EffectScope(true)
  221. detachedScope.run(() => {})
  222. expect(getCurrentScope()).toBe(currentScope)
  223. })
  224. })
  225. it('calling .off() of a detached scope inside an active scope should not break currentScope', () => {
  226. const parentScope = new EffectScope()
  227. parentScope.run(() => {
  228. const childScope = new EffectScope(true)
  229. childScope.on()
  230. childScope.off()
  231. expect(getCurrentScope()).toBe(parentScope)
  232. })
  233. })
  234. })