@@ -2,13 +2,15 @@ import { nextTick, watch, watchEffect } from '@vue/runtime-core'
22import {
33 type ComputedRef ,
44 EffectScope ,
5+ ReactiveEffect ,
56 computed ,
67 effect ,
78 effectScope ,
89 getCurrentScope ,
910 onScopeDispose ,
1011 reactive ,
1112 ref ,
13+ setCurrentScope ,
1214} from '../src'
1315
1416describe ( 'reactivity/effect/scope' , ( ) => {
@@ -20,7 +22,7 @@ describe('reactivity/effect/scope', () => {
2022
2123 it ( 'should accept zero argument' , ( ) => {
2224 const scope = effectScope ( )
23- expect ( scope . effects . length ) . toBe ( 0 )
25+ expect ( getEffectsCount ( scope ) ) . toBe ( 0 )
2426 } )
2527
2628 it ( 'should return run value' , ( ) => {
@@ -29,7 +31,8 @@ describe('reactivity/effect/scope', () => {
2931
3032 it ( 'should work w/ active property' , ( ) => {
3133 const scope = effectScope ( )
32- scope . run ( ( ) => 1 )
34+ const src = computed ( ( ) => 1 )
35+ scope . run ( ( ) => src . value )
3336 expect ( scope . active ) . toBe ( true )
3437 scope . stop ( )
3538 expect ( scope . active ) . toBe ( false )
@@ -47,7 +50,7 @@ describe('reactivity/effect/scope', () => {
4750 expect ( dummy ) . toBe ( 7 )
4851 } )
4952
50- expect ( scope . effects . length ) . toBe ( 1 )
53+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
5154 } )
5255
5356 it ( 'stop' , ( ) => {
@@ -60,7 +63,7 @@ describe('reactivity/effect/scope', () => {
6063 effect ( ( ) => ( doubled = counter . num * 2 ) )
6164 } )
6265
63- expect ( scope . effects . length ) . toBe ( 2 )
66+ expect ( getEffectsCount ( scope ) ) . toBe ( 2 )
6467
6568 expect ( dummy ) . toBe ( 0 )
6669 counter . num = 7
@@ -87,9 +90,8 @@ describe('reactivity/effect/scope', () => {
8790 } )
8891 } )
8992
90- expect ( scope . effects . length ) . toBe ( 1 )
91- expect ( scope . scopes ! . length ) . toBe ( 1 )
92- expect ( scope . scopes ! [ 0 ] ) . toBeInstanceOf ( EffectScope )
93+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
94+ expect ( scope . deps ?. nextDep ?. dep ) . toBeInstanceOf ( EffectScope )
9395
9496 expect ( dummy ) . toBe ( 0 )
9597 counter . num = 7
@@ -117,7 +119,7 @@ describe('reactivity/effect/scope', () => {
117119 } )
118120 } )
119121
120- expect ( scope . effects . length ) . toBe ( 1 )
122+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
121123
122124 expect ( dummy ) . toBe ( 0 )
123125 counter . num = 7
@@ -142,13 +144,13 @@ describe('reactivity/effect/scope', () => {
142144 effect ( ( ) => ( dummy = counter . num ) )
143145 } )
144146
145- expect ( scope . effects . length ) . toBe ( 1 )
147+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
146148
147149 scope . run ( ( ) => {
148150 effect ( ( ) => ( doubled = counter . num * 2 ) )
149151 } )
150152
151- expect ( scope . effects . length ) . toBe ( 2 )
153+ expect ( getEffectsCount ( scope ) ) . toBe ( 2 )
152154
153155 counter . num = 7
154156 expect ( dummy ) . toBe ( 7 )
@@ -166,21 +168,21 @@ describe('reactivity/effect/scope', () => {
166168 effect ( ( ) => ( dummy = counter . num ) )
167169 } )
168170
169- expect ( scope . effects . length ) . toBe ( 1 )
171+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
170172
171173 scope . stop ( )
172174
175+ expect ( getEffectsCount ( scope ) ) . toBe ( 0 )
176+
173177 scope . run ( ( ) => {
174178 effect ( ( ) => ( doubled = counter . num * 2 ) )
175179 } )
176180
177- expect ( '[Vue warn] cannot run an inactive effect scope.' ) . toHaveBeenWarned ( )
178-
179- expect ( scope . effects . length ) . toBe ( 0 )
181+ expect ( getEffectsCount ( scope ) ) . toBe ( 1 )
180182
181183 counter . num = 7
182184 expect ( dummy ) . toBe ( 0 )
183- expect ( doubled ) . toBe ( undefined )
185+ expect ( doubled ) . toBe ( 14 )
184186 } )
185187
186188 it ( 'should fire onScopeDispose hook' , ( ) => {
@@ -224,9 +226,9 @@ describe('reactivity/effect/scope', () => {
224226 it ( 'should dereference child scope from parent scope after stopping child scope (no memleaks)' , ( ) => {
225227 const parent = effectScope ( )
226228 const child = parent . run ( ( ) => effectScope ( ) ) !
227- expect ( parent . scopes ! . includes ( child ) ) . toBe ( true )
229+ expect ( parent . deps ?. dep ) . toBe ( child )
228230 child . stop ( )
229- expect ( parent . scopes ! . includes ( child ) ) . toBe ( false )
231+ expect ( parent . deps ) . toBeUndefined ( )
230232 } )
231233
232234 it ( 'test with higher level APIs' , async ( ) => {
@@ -290,21 +292,7 @@ describe('reactivity/effect/scope', () => {
290292
291293 parentScope . run ( ( ) => {
292294 const childScope = effectScope ( true )
293- childScope . on ( )
294- childScope . off ( )
295- expect ( getCurrentScope ( ) ) . toBe ( parentScope )
296- } )
297- } )
298-
299- it ( 'calling on() and off() multiple times inside an active scope should not break currentScope' , ( ) => {
300- const parentScope = effectScope ( )
301- parentScope . run ( ( ) => {
302- const childScope = effectScope ( true )
303- childScope . on ( )
304- childScope . on ( )
305- childScope . off ( )
306- childScope . off ( )
307- childScope . off ( )
295+ setCurrentScope ( setCurrentScope ( childScope ) )
308296 expect ( getCurrentScope ( ) ) . toBe ( parentScope )
309297 } )
310298 } )
@@ -372,7 +360,17 @@ describe('reactivity/effect/scope', () => {
372360 expect ( watcherCalls ) . toBe ( 3 )
373361 expect ( cleanupCalls ) . toBe ( 1 )
374362
375- expect ( scope . effects . length ) . toBe ( 0 )
376- expect ( scope . cleanups . length ) . toBe ( 0 )
363+ expect ( getEffectsCount ( scope ) ) . toBe ( 0 )
364+ expect ( scope . cleanupsLength ) . toBe ( 0 )
377365 } )
378366} )
367+
368+ function getEffectsCount ( scope : EffectScope ) : number {
369+ let n = 0
370+ for ( let dep = scope . deps ; dep !== undefined ; dep = dep . nextDep ) {
371+ if ( dep . dep instanceof ReactiveEffect ) {
372+ n ++
373+ }
374+ }
375+ return n
376+ }
0 commit comments