import { expect, test } from "vitest" import { effect, effectScope, signal } from "#Source/reactor/index.ts" /** * @description 这个测试文件应该与 https://github.com/stackblitz/alien-signals/blob/master/tests/effectScope.spec.ts 保持同步。 */ test("should not trigger after dispose", () => { /** * @description Count * | * e(inner) * | * e(scope) */ const count = signal(() => 1) let innerEffectRunTimes = 0 const scope = effectScope(() => { effect(() => { innerEffectRunTimes = innerEffectRunTimes + 1 count.get() }) expect(innerEffectRunTimes).toBe(1) count.set(2) expect(innerEffectRunTimes).toBe(2) }) count.set(3) expect(innerEffectRunTimes).toBe(3) scope.dispose() count.set(4) expect(innerEffectRunTimes).toBe(3) }) test("should dispose inner effects if created in an effect", () => { /** * @description Source * | * e(inner) * | * e(scope) * | * e(outer) */ const source = signal(() => 1) let innerEffectRunTimes = 0 effect(() => { const scope = effectScope(() => { effect(() => { source.get() innerEffectRunTimes = innerEffectRunTimes + 1 }) }) expect(innerEffectRunTimes).toBe(1) source.set(2) expect(innerEffectRunTimes).toBe(2) scope.dispose() source.set(3) expect(innerEffectRunTimes).toBe(2) }) }) test("should track signal updates in an inner scope when accessed by an outer effect", () => { /** * @description Source * | * e(scope) * | * e(outer) */ const source = signal(() => 1) let outerEffectRunTimes = 0 effect(() => { effectScope(() => { source.get() }) outerEffectRunTimes = outerEffectRunTimes + 1 }) expect(outerEffectRunTimes).toBe(1) source.set(2) expect(outerEffectRunTimes).toBe(2) })