effectScope.spec.ts 6.7 KB

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