test: create `EffectScope` using a factory function (#8844)

This commit is contained in:
丶远方 2024-03-15 22:27:32 +08:00 committed by GitHub
parent 384591a2a1
commit cde47bfa97
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 21 deletions

View File

@ -4,6 +4,7 @@ import {
EffectScope, EffectScope,
computed, computed,
effect, effect,
effectScope,
getCurrentScope, getCurrentScope,
onScopeDispose, onScopeDispose,
reactive, reactive,
@ -13,21 +14,21 @@ import {
describe('reactivity/effect/scope', () => { describe('reactivity/effect/scope', () => {
it('should run', () => { it('should run', () => {
const fnSpy = vi.fn(() => {}) const fnSpy = vi.fn(() => {})
new EffectScope().run(fnSpy) effectScope().run(fnSpy)
expect(fnSpy).toHaveBeenCalledTimes(1) expect(fnSpy).toHaveBeenCalledTimes(1)
}) })
it('should accept zero argument', () => { it('should accept zero argument', () => {
const scope = new EffectScope() const scope = effectScope()
expect(scope.effects.length).toBe(0) expect(scope.effects.length).toBe(0)
}) })
it('should return run value', () => { it('should return run value', () => {
expect(new EffectScope().run(() => 1)).toBe(1) expect(effectScope().run(() => 1)).toBe(1)
}) })
it('should work w/ active property', () => { it('should work w/ active property', () => {
const scope = new EffectScope() const scope = effectScope()
scope.run(() => 1) scope.run(() => 1)
expect(scope.active).toBe(true) expect(scope.active).toBe(true)
scope.stop() scope.stop()
@ -35,7 +36,7 @@ describe('reactivity/effect/scope', () => {
}) })
it('should collect the effects', () => { it('should collect the effects', () => {
const scope = new EffectScope() const scope = effectScope()
scope.run(() => { scope.run(() => {
let dummy let dummy
const counter = reactive({ num: 0 }) const counter = reactive({ num: 0 })
@ -53,7 +54,7 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled let dummy, doubled
const counter = reactive({ num: 0 }) const counter = reactive({ num: 0 })
const scope = new EffectScope() const scope = effectScope()
scope.run(() => { scope.run(() => {
effect(() => (dummy = counter.num)) effect(() => (dummy = counter.num))
effect(() => (doubled = counter.num * 2)) effect(() => (doubled = counter.num * 2))
@ -77,11 +78,11 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled let dummy, doubled
const counter = reactive({ num: 0 }) const counter = reactive({ num: 0 })
const scope = new EffectScope() const scope = effectScope()
scope.run(() => { scope.run(() => {
effect(() => (dummy = counter.num)) effect(() => (dummy = counter.num))
// nested scope // nested scope
new EffectScope().run(() => { effectScope().run(() => {
effect(() => (doubled = counter.num * 2)) effect(() => (doubled = counter.num * 2))
}) })
}) })
@ -107,11 +108,11 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled let dummy, doubled
const counter = reactive({ num: 0 }) const counter = reactive({ num: 0 })
const scope = new EffectScope() const scope = effectScope()
scope.run(() => { scope.run(() => {
effect(() => (dummy = counter.num)) effect(() => (dummy = counter.num))
// nested scope // nested scope
new EffectScope(true).run(() => { effectScope(true).run(() => {
effect(() => (doubled = counter.num * 2)) effect(() => (doubled = counter.num * 2))
}) })
}) })
@ -136,7 +137,7 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled let dummy, doubled
const counter = reactive({ num: 0 }) const counter = reactive({ num: 0 })
const scope = new EffectScope() const scope = effectScope()
scope.run(() => { scope.run(() => {
effect(() => (dummy = counter.num)) effect(() => (dummy = counter.num))
}) })
@ -160,7 +161,7 @@ describe('reactivity/effect/scope', () => {
let dummy, doubled let dummy, doubled
const counter = reactive({ num: 0 }) const counter = reactive({ num: 0 })
const scope = new EffectScope() const scope = effectScope()
scope.run(() => { scope.run(() => {
effect(() => (dummy = counter.num)) effect(() => (dummy = counter.num))
}) })
@ -185,7 +186,7 @@ describe('reactivity/effect/scope', () => {
it('should fire onScopeDispose hook', () => { it('should fire onScopeDispose hook', () => {
let dummy = 0 let dummy = 0
const scope = new EffectScope() const scope = effectScope()
scope.run(() => { scope.run(() => {
onScopeDispose(() => (dummy += 1)) onScopeDispose(() => (dummy += 1))
onScopeDispose(() => (dummy += 2)) onScopeDispose(() => (dummy += 2))
@ -203,7 +204,7 @@ describe('reactivity/effect/scope', () => {
it('should warn onScopeDispose() is called when there is no active effect scope', () => { it('should warn onScopeDispose() is called when there is no active effect scope', () => {
const spy = vi.fn() const spy = vi.fn()
const scope = new EffectScope() const scope = effectScope()
scope.run(() => { scope.run(() => {
onScopeDispose(spy) onScopeDispose(spy)
}) })
@ -221,8 +222,8 @@ describe('reactivity/effect/scope', () => {
}) })
it('should dereference child scope from parent scope after stopping child scope (no memleaks)', () => { it('should dereference child scope from parent scope after stopping child scope (no memleaks)', () => {
const parent = new EffectScope() const parent = effectScope()
const child = parent.run(() => new EffectScope())! const child = parent.run(() => effectScope())!
expect(parent.scopes!.includes(child)).toBe(true) expect(parent.scopes!.includes(child)).toBe(true)
child.stop() child.stop()
expect(parent.scopes!.includes(child)).toBe(false) expect(parent.scopes!.includes(child)).toBe(false)
@ -236,7 +237,7 @@ describe('reactivity/effect/scope', () => {
const watchEffectSpy = vi.fn() const watchEffectSpy = vi.fn()
let c: ComputedRef let c: ComputedRef
const scope = new EffectScope() const scope = effectScope()
scope.run(() => { scope.run(() => {
c = computed(() => { c = computed(() => {
computedSpy() computedSpy()
@ -274,12 +275,12 @@ describe('reactivity/effect/scope', () => {
}) })
it('getCurrentScope() stays valid when running a detached nested EffectScope', () => { it('getCurrentScope() stays valid when running a detached nested EffectScope', () => {
const parentScope = new EffectScope() const parentScope = effectScope()
parentScope.run(() => { parentScope.run(() => {
const currentScope = getCurrentScope() const currentScope = getCurrentScope()
expect(currentScope).toBeDefined() expect(currentScope).toBeDefined()
const detachedScope = new EffectScope(true) const detachedScope = effectScope(true)
detachedScope.run(() => {}) detachedScope.run(() => {})
expect(getCurrentScope()).toBe(currentScope) expect(getCurrentScope()).toBe(currentScope)
@ -287,10 +288,10 @@ describe('reactivity/effect/scope', () => {
}) })
it('calling .off() of a detached scope inside an active scope should not break 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(() => { parentScope.run(() => {
const childScope = new EffectScope(true) const childScope = effectScope(true)
childScope.on() childScope.on()
childScope.off() childScope.off()
expect(getCurrentScope()).toBe(parentScope) expect(getCurrentScope()).toBe(parentScope)