4
4
EffectScope ,
5
5
computed ,
6
6
effect ,
7
+ effectScope ,
7
8
getCurrentScope ,
8
9
onScopeDispose ,
9
10
reactive ,
@@ -13,29 +14,29 @@ import {
13
14
describe ( 'reactivity/effect/scope' , ( ) => {
14
15
it ( 'should run' , ( ) => {
15
16
const fnSpy = vi . fn ( ( ) => { } )
16
- new EffectScope ( ) . run ( fnSpy )
17
+ effectScope ( ) . run ( fnSpy )
17
18
expect ( fnSpy ) . toHaveBeenCalledTimes ( 1 )
18
19
} )
19
20
20
21
it ( 'should accept zero argument' , ( ) => {
21
- const scope = new EffectScope ( )
22
+ const scope = effectScope ( )
22
23
expect ( scope . effects . length ) . toBe ( 0 )
23
24
} )
24
25
25
26
it ( 'should return run value' , ( ) => {
26
- expect ( new EffectScope ( ) . run ( ( ) => 1 ) ) . toBe ( 1 )
27
+ expect ( effectScope ( ) . run ( ( ) => 1 ) ) . toBe ( 1 )
27
28
} )
28
29
29
30
it ( 'should work w/ active property' , ( ) => {
30
- const scope = new EffectScope ( )
31
+ const scope = effectScope ( )
31
32
scope . run ( ( ) => 1 )
32
33
expect ( scope . active ) . toBe ( true )
33
34
scope . stop ( )
34
35
expect ( scope . active ) . toBe ( false )
35
36
} )
36
37
37
38
it ( 'should collect the effects' , ( ) => {
38
- const scope = new EffectScope ( )
39
+ const scope = effectScope ( )
39
40
scope . run ( ( ) => {
40
41
let dummy
41
42
const counter = reactive ( { num : 0 } )
@@ -53,7 +54,7 @@ describe('reactivity/effect/scope', () => {
53
54
let dummy , doubled
54
55
const counter = reactive ( { num : 0 } )
55
56
56
- const scope = new EffectScope ( )
57
+ const scope = effectScope ( )
57
58
scope . run ( ( ) => {
58
59
effect ( ( ) => ( dummy = counter . num ) )
59
60
effect ( ( ) => ( doubled = counter . num * 2 ) )
@@ -77,11 +78,11 @@ describe('reactivity/effect/scope', () => {
77
78
let dummy , doubled
78
79
const counter = reactive ( { num : 0 } )
79
80
80
- const scope = new EffectScope ( )
81
+ const scope = effectScope ( )
81
82
scope . run ( ( ) => {
82
83
effect ( ( ) => ( dummy = counter . num ) )
83
84
// nested scope
84
- new EffectScope ( ) . run ( ( ) => {
85
+ effectScope ( ) . run ( ( ) => {
85
86
effect ( ( ) => ( doubled = counter . num * 2 ) )
86
87
} )
87
88
} )
@@ -107,11 +108,11 @@ describe('reactivity/effect/scope', () => {
107
108
let dummy , doubled
108
109
const counter = reactive ( { num : 0 } )
109
110
110
- const scope = new EffectScope ( )
111
+ const scope = effectScope ( )
111
112
scope . run ( ( ) => {
112
113
effect ( ( ) => ( dummy = counter . num ) )
113
114
// nested scope
114
- new EffectScope ( true ) . run ( ( ) => {
115
+ effectScope ( true ) . run ( ( ) => {
115
116
effect ( ( ) => ( doubled = counter . num * 2 ) )
116
117
} )
117
118
} )
@@ -136,7 +137,7 @@ describe('reactivity/effect/scope', () => {
136
137
let dummy , doubled
137
138
const counter = reactive ( { num : 0 } )
138
139
139
- const scope = new EffectScope ( )
140
+ const scope = effectScope ( )
140
141
scope . run ( ( ) => {
141
142
effect ( ( ) => ( dummy = counter . num ) )
142
143
} )
@@ -160,7 +161,7 @@ describe('reactivity/effect/scope', () => {
160
161
let dummy , doubled
161
162
const counter = reactive ( { num : 0 } )
162
163
163
- const scope = new EffectScope ( )
164
+ const scope = effectScope ( )
164
165
scope . run ( ( ) => {
165
166
effect ( ( ) => ( dummy = counter . num ) )
166
167
} )
@@ -185,7 +186,7 @@ describe('reactivity/effect/scope', () => {
185
186
it ( 'should fire onScopeDispose hook' , ( ) => {
186
187
let dummy = 0
187
188
188
- const scope = new EffectScope ( )
189
+ const scope = effectScope ( )
189
190
scope . run ( ( ) => {
190
191
onScopeDispose ( ( ) => ( dummy += 1 ) )
191
192
onScopeDispose ( ( ) => ( dummy += 2 ) )
@@ -203,7 +204,7 @@ describe('reactivity/effect/scope', () => {
203
204
204
205
it ( 'should warn onScopeDispose() is called when there is no active effect scope' , ( ) => {
205
206
const spy = vi . fn ( )
206
- const scope = new EffectScope ( )
207
+ const scope = effectScope ( )
207
208
scope . run ( ( ) => {
208
209
onScopeDispose ( spy )
209
210
} )
@@ -221,8 +222,8 @@ describe('reactivity/effect/scope', () => {
221
222
} )
222
223
223
224
it ( 'should dereference child scope from parent scope after stopping child scope (no memleaks)' , ( ) => {
224
- const parent = new EffectScope ( )
225
- const child = parent . run ( ( ) => new EffectScope ( ) ) !
225
+ const parent = effectScope ( )
226
+ const child = parent . run ( ( ) => effectScope ( ) ) !
226
227
expect ( parent . scopes ! . includes ( child ) ) . toBe ( true )
227
228
child . stop ( )
228
229
expect ( parent . scopes ! . includes ( child ) ) . toBe ( false )
@@ -236,7 +237,7 @@ describe('reactivity/effect/scope', () => {
236
237
const watchEffectSpy = vi . fn ( )
237
238
238
239
let c : ComputedRef
239
- const scope = new EffectScope ( )
240
+ const scope = effectScope ( )
240
241
scope . run ( ( ) => {
241
242
c = computed ( ( ) => {
242
243
computedSpy ( )
@@ -274,23 +275,23 @@ describe('reactivity/effect/scope', () => {
274
275
} )
275
276
276
277
it ( 'getCurrentScope() stays valid when running a detached nested EffectScope' , ( ) => {
277
- const parentScope = new EffectScope ( )
278
+ const parentScope = effectScope ( )
278
279
279
280
parentScope . run ( ( ) => {
280
281
const currentScope = getCurrentScope ( )
281
282
expect ( currentScope ) . toBeDefined ( )
282
- const detachedScope = new EffectScope ( true )
283
+ const detachedScope = effectScope ( true )
283
284
detachedScope . run ( ( ) => { } )
284
285
285
286
expect ( getCurrentScope ( ) ) . toBe ( currentScope )
286
287
} )
287
288
} )
288
289
289
290
it ( 'calling .off() of a detached scope inside an active scope should not break currentScope' , ( ) => {
290
- const parentScope = new EffectScope ( )
291
+ const parentScope = effectScope ( )
291
292
292
293
parentScope . run ( ( ) => {
293
- const childScope = new EffectScope ( true )
294
+ const childScope = effectScope ( true )
294
295
childScope . on ( )
295
296
childScope . off ( )
296
297
expect ( getCurrentScope ( ) ) . toBe ( parentScope )
0 commit comments