Skip to content

Commit cde47bf

Browse files
test: create EffectScope using a factory function (#8844)
1 parent 384591a commit cde47bf

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

packages/reactivity/__tests__/effectScope.spec.ts

+22-21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
EffectScope,
55
computed,
66
effect,
7+
effectScope,
78
getCurrentScope,
89
onScopeDispose,
910
reactive,
@@ -13,29 +14,29 @@ import {
1314
describe('reactivity/effect/scope', () => {
1415
it('should run', () => {
1516
const fnSpy = vi.fn(() => {})
16-
new EffectScope().run(fnSpy)
17+
effectScope().run(fnSpy)
1718
expect(fnSpy).toHaveBeenCalledTimes(1)
1819
})
1920

2021
it('should accept zero argument', () => {
21-
const scope = new EffectScope()
22+
const scope = effectScope()
2223
expect(scope.effects.length).toBe(0)
2324
})
2425

2526
it('should return run value', () => {
26-
expect(new EffectScope().run(() => 1)).toBe(1)
27+
expect(effectScope().run(() => 1)).toBe(1)
2728
})
2829

2930
it('should work w/ active property', () => {
30-
const scope = new EffectScope()
31+
const scope = effectScope()
3132
scope.run(() => 1)
3233
expect(scope.active).toBe(true)
3334
scope.stop()
3435
expect(scope.active).toBe(false)
3536
})
3637

3738
it('should collect the effects', () => {
38-
const scope = new EffectScope()
39+
const scope = effectScope()
3940
scope.run(() => {
4041
let dummy
4142
const counter = reactive({ num: 0 })
@@ -53,7 +54,7 @@ describe('reactivity/effect/scope', () => {
5354
let dummy, doubled
5455
const counter = reactive({ num: 0 })
5556

56-
const scope = new EffectScope()
57+
const scope = effectScope()
5758
scope.run(() => {
5859
effect(() => (dummy = counter.num))
5960
effect(() => (doubled = counter.num * 2))
@@ -77,11 +78,11 @@ describe('reactivity/effect/scope', () => {
7778
let dummy, doubled
7879
const counter = reactive({ num: 0 })
7980

80-
const scope = new EffectScope()
81+
const scope = effectScope()
8182
scope.run(() => {
8283
effect(() => (dummy = counter.num))
8384
// nested scope
84-
new EffectScope().run(() => {
85+
effectScope().run(() => {
8586
effect(() => (doubled = counter.num * 2))
8687
})
8788
})
@@ -107,11 +108,11 @@ describe('reactivity/effect/scope', () => {
107108
let dummy, doubled
108109
const counter = reactive({ num: 0 })
109110

110-
const scope = new EffectScope()
111+
const scope = effectScope()
111112
scope.run(() => {
112113
effect(() => (dummy = counter.num))
113114
// nested scope
114-
new EffectScope(true).run(() => {
115+
effectScope(true).run(() => {
115116
effect(() => (doubled = counter.num * 2))
116117
})
117118
})
@@ -136,7 +137,7 @@ describe('reactivity/effect/scope', () => {
136137
let dummy, doubled
137138
const counter = reactive({ num: 0 })
138139

139-
const scope = new EffectScope()
140+
const scope = effectScope()
140141
scope.run(() => {
141142
effect(() => (dummy = counter.num))
142143
})
@@ -160,7 +161,7 @@ describe('reactivity/effect/scope', () => {
160161
let dummy, doubled
161162
const counter = reactive({ num: 0 })
162163

163-
const scope = new EffectScope()
164+
const scope = effectScope()
164165
scope.run(() => {
165166
effect(() => (dummy = counter.num))
166167
})
@@ -185,7 +186,7 @@ describe('reactivity/effect/scope', () => {
185186
it('should fire onScopeDispose hook', () => {
186187
let dummy = 0
187188

188-
const scope = new EffectScope()
189+
const scope = effectScope()
189190
scope.run(() => {
190191
onScopeDispose(() => (dummy += 1))
191192
onScopeDispose(() => (dummy += 2))
@@ -203,7 +204,7 @@ describe('reactivity/effect/scope', () => {
203204

204205
it('should warn onScopeDispose() is called when there is no active effect scope', () => {
205206
const spy = vi.fn()
206-
const scope = new EffectScope()
207+
const scope = effectScope()
207208
scope.run(() => {
208209
onScopeDispose(spy)
209210
})
@@ -221,8 +222,8 @@ describe('reactivity/effect/scope', () => {
221222
})
222223

223224
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())!
226227
expect(parent.scopes!.includes(child)).toBe(true)
227228
child.stop()
228229
expect(parent.scopes!.includes(child)).toBe(false)
@@ -236,7 +237,7 @@ describe('reactivity/effect/scope', () => {
236237
const watchEffectSpy = vi.fn()
237238

238239
let c: ComputedRef
239-
const scope = new EffectScope()
240+
const scope = effectScope()
240241
scope.run(() => {
241242
c = computed(() => {
242243
computedSpy()
@@ -274,23 +275,23 @@ describe('reactivity/effect/scope', () => {
274275
})
275276

276277
it('getCurrentScope() stays valid when running a detached nested EffectScope', () => {
277-
const parentScope = new EffectScope()
278+
const parentScope = effectScope()
278279

279280
parentScope.run(() => {
280281
const currentScope = getCurrentScope()
281282
expect(currentScope).toBeDefined()
282-
const detachedScope = new EffectScope(true)
283+
const detachedScope = effectScope(true)
283284
detachedScope.run(() => {})
284285

285286
expect(getCurrentScope()).toBe(currentScope)
286287
})
287288
})
288289

289290
it('calling .off() of a detached scope inside an active scope should not break currentScope', () => {
290-
const parentScope = new EffectScope()
291+
const parentScope = effectScope()
291292

292293
parentScope.run(() => {
293-
const childScope = new EffectScope(true)
294+
const childScope = effectScope(true)
294295
childScope.on()
295296
childScope.off()
296297
expect(getCurrentScope()).toBe(parentScope)

0 commit comments

Comments
 (0)