import { expect, test } from "vitest" import { computed, signal } from "#Source/reactor/index.ts" /** * @description 这个测试文件应该与 https://github.com/stackblitz/alien-signals/blob/master/tests/computed.spec.ts 保持同步。 */ test("should correctly propagate changes through computed signals", () => { /** * @description Src * | * c1 * | * c2 * | * c3 */ const src = signal(() => 0) const c1 = computed(() => src.get() % 2) const c2 = computed(() => c1.get()) const c3 = computed(() => c2.get()) expect(c3.get()).toBe(0) src.set(1) expect(c2.get()).toBe(1) src.set(3) expect(c3.get()).toBe(1) }) test("should propagate updated source value through chained computations", () => { /** * @description Src / \ a c * * | | * * B | \ / d */ const src = signal(() => 0) const a = computed(() => src.get()) const b = computed(() => a.get() % 2) const c = computed(() => src.get()) const d = computed(() => b.get() + c.get()) expect(d.get()).toBe(0) src.set(2) expect(d.get()).toBe(2) }) test("should handle flags are indirectly updated during resolve pending", () => { /** * @description A * | * b * /\ * c | * \ / * d */ const a = signal(() => false) const b = computed(() => a.get()) const c = computed(() => { b.get() return 0 }) const d = computed(() => { c.get() return b.get() }) expect(d.get()).toBe(false) a.set(true) expect(d.get()).toBe(true) }) test("should not update if the signal value is reverted", () => { /** * @description Src * | * c1 */ const src = signal(() => 0) let c1RunTimes = 0 const c1 = computed(() => { c1RunTimes = c1RunTimes + 1 return src.get() }) expect(c1.get()).toBe(0) expect(c1RunTimes).toBe(1) src.set(1) src.set(0) expect(c1.get()).toBe(0) expect(c1RunTimes).toBe(1) })