effectScope.spec.ts 6.3 KB

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