Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: create EffectScope using a factory function #8844

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { nextTick, watch, watchEffect } from '@vue/runtime-core'
import {
reactive,
effect,
effectScope,
EffectScope,
onScopeDispose,
computed,
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