import { describe, it, vi, expect } from "vitest" import { Signal, effect, signal } from "#Source/reactor/index.ts" /** * @description 这个测试文件应该与 https://github.com/preactjs/signals/blob/main/packages/core/test/signal.test.tsx 保持同步。 */ describe("signal", () => { it("should return value", () => { const v = [1, 2] const s = signal(() => v) expect(s.get()).to.equal(v) }) it("should inherit from Signal", () => { expect(signal(() => 0)).to.be.instanceOf(Signal) }) it("should support .toString()", () => { // const s = signal(123); // expect(s.toString()).equal("123"); }) it("should support .toJSON()", () => { // const s = signal(123); // expect(s.toJSON()).equal(123); }) it("should support JSON.Stringify()", () => { // const s = signal(123); // expect(JSON.stringify({ s })).equal(JSON.stringify({ s: 123 })); }) it("should support .valueOf()", () => { // const s = signal(123); // expect(s).to.have.property("valueOf"); // expect(s.valueOf).to.be.a("function"); // expect(s.valueOf()).equal(123); // expect(+s).equal(123); // const a = signal(1); // const b = signal(2); // expect(a + b).to.equal(3); }) it("should notify other listeners of changes after one listener is disposed", () => { const s = signal(() => 0) const spy1 = vi.fn(() => { s.get() }) const spy2 = vi.fn(() => { s.get() }) const spy3 = vi.fn(() => { s.get() }) effect(spy1) const e2 = effect(spy2) effect(spy3) expect(spy1).toHaveBeenCalledOnce() expect(spy2).toHaveBeenCalledOnce() expect(spy3).toHaveBeenCalledOnce() e2.dispose() s.set(1) expect(spy1).toHaveBeenCalledTimes(2) expect(spy2).toHaveBeenCalledOnce() expect(spy3).toHaveBeenCalledTimes(2) }) })