effectScope.spec.ts 7.2 KB

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