Skip to content

Commit

Permalink
test: create EffectScope using a factory function (#8844)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue committed Mar 15, 2024
1 parent 384591a commit cde47bf
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
EffectScope,
computed,
effect,
effectScope,
getCurrentScope,
onScopeDispose,
reactive,
Expand All @@ -13,29 +14,29 @@ import {
describe('reactivity/effect/scope', () => {
it('should run', () => {
const fnSpy = vi.fn(() => {})
new EffectScope().run(fnSpy)
effectScope().run(fnSpy)
expect(fnSpy).toHaveBeenCalledTimes(1)
})

it('should accept zero argument', () => {
const scope = new EffectScope()
const scope = effectScope()
expect(scope.effects.length).toBe(0)
})

it('should return run value', () => {
expect(new EffectScope().run(() => 1)).toBe(1)
expect(effectScope().run(() => 1)).toBe(1)
})

it('should work w/ active property', () => {
const scope = new EffectScope()
const scope = effectScope()
scope.run(() => 1)
expect(scope.active).toBe(true)
scope.stop()
expect(scope.active).toBe(false)
})

it('should collect the effects', () => {
const scope = new EffectScope()
const scope = effectScope()
scope.run(() => {
let dummy
const counter = reactive({ num: 0 })
Expand All @@ -53,7 +54,7 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled
const counter = reactive({ num: 0 })

const scope = new EffectScope()
const scope = effectScope()
scope.run(() => {
effect(() => (dummy = counter.num))
effect(() => (doubled = counter.num * 2))
Expand All @@ -77,11 +78,11 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled
const counter = reactive({ num: 0 })

const scope = new EffectScope()
const scope = effectScope()
scope.run(() => {
effect(() => (dummy = counter.num))
// nested scope
new EffectScope().run(() => {
effectScope().run(() => {
effect(() => (doubled = counter.num * 2))
})
})
Expand All @@ -107,11 +108,11 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled
const counter = reactive({ num: 0 })

const scope = new EffectScope()
const scope = effectScope()
scope.run(() => {
effect(() => (dummy = counter.num))
// nested scope
new EffectScope(true).run(() => {
effectScope(true).run(() => {
effect(() => (doubled = counter.num * 2))
})
})
Expand All @@ -136,7 +137,7 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled
const counter = reactive({ num: 0 })

const scope = new EffectScope()
const scope = effectScope()
scope.run(() => {
effect(() => (dummy = counter.num))
})
Expand All @@ -160,7 +161,7 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled
const counter = reactive({ num: 0 })

const scope = new EffectScope()
const scope = effectScope()
scope.run(() => {
effect(() => (dummy = counter.num))
})
Expand All @@ -185,7 +186,7 @@ describe('reactivity/effect/scope', () => {
it('should fire onScopeDispose hook', () => {
let dummy = 0

const scope = new EffectScope()
const scope = effectScope()
scope.run(() => {
onScopeDispose(() => (dummy += 1))
onScopeDispose(() => (dummy += 2))
Expand All @@ -203,7 +204,7 @@ describe('reactivity/effect/scope', () => {

it('should warn onScopeDispose() is called when there is no active effect scope', () => {
const spy = vi.fn()
const scope = new EffectScope()
const scope = effectScope()
scope.run(() => {
onScopeDispose(spy)
})
Expand All @@ -221,8 +222,8 @@ describe('reactivity/effect/scope', () => {
})

it('should dereference child scope from parent scope after stopping child scope (no memleaks)', () => {
const parent = new EffectScope()
const child = parent.run(() => new EffectScope())!
const parent = effectScope()
const child = parent.run(() => effectScope())!
expect(parent.scopes!.includes(child)).toBe(true)
child.stop()
expect(parent.scopes!.includes(child)).toBe(false)
Expand All @@ -236,7 +237,7 @@ describe('reactivity/effect/scope', () => {
const watchEffectSpy = vi.fn()

let c: ComputedRef
const scope = new EffectScope()
const scope = effectScope()
scope.run(() => {
c = computed(() => {
computedSpy()
Expand Down Expand Up @@ -274,23 +275,23 @@ describe('reactivity/effect/scope', () => {
})

it('getCurrentScope() stays valid when running a detached nested EffectScope', () => {
const parentScope = new EffectScope()
const parentScope = effectScope()

parentScope.run(() => {
const currentScope = getCurrentScope()
expect(currentScope).toBeDefined()
const detachedScope = new EffectScope(true)
const detachedScope = effectScope(true)
detachedScope.run(() => {})

expect(getCurrentScope()).toBe(currentScope)
})
})

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

parentScope.run(() => {
const childScope = new EffectScope(true)
const childScope = effectScope(true)
childScope.on()
childScope.off()
expect(getCurrentScope()).toBe(parentScope)
Expand Down

0 comments on commit cde47bf

Please sign in to comment.