Files
next-tjwater-frontend/src/shared/ui/floating-window-layer.test.ts
T
jiang 0ae0a41e61
Generic Container CI/CD / test-build-publish (push) Successful in 2m7s
Frontend CI/CD / build-test-publish-and-deploy (push) Successful in 2m8s
feat(charts): manage investigation charts in a scrolling workspace
2026-11-10 17:56:23 +08:00

45 lines
2.1 KiB
TypeScript

import { afterEach, expect, it, vi } from "vitest";
import { floatingWindowEntries, floatingWindowOrder, raiseFloatingWindow, registerFloatingWindow, removeFloatingWindow, subscribeFloatingWindows } from "./floating-window-layer";
afterEach(() => floatingWindowOrder().forEach(removeFloatingWindow));
it("keeps a stable snapshot, follows stacking order and removes window metadata", () => {
const notify = vi.fn();
const stop = subscribeFloatingWindows(notify);
const first = { id: "first", title: "压力图", focus: vi.fn(), close: vi.fn() };
registerFloatingWindow(first);
const snapshot = floatingWindowEntries();
expect(floatingWindowEntries()).toBe(snapshot);
registerFloatingWindow({ id: "second", title: "流量图", focus: vi.fn(), close: vi.fn() });
expect(floatingWindowEntries().map((entry) => entry.id)).toEqual(["first", "second"]);
raiseFloatingWindow("first");
expect(floatingWindowEntries().map((entry) => entry.id)).toEqual(["second", "first"]);
removeFloatingWindow("first");
expect(floatingWindowEntries().map((entry) => entry.id)).toEqual(["second"]);
removeFloatingWindow("second");
expect(floatingWindowEntries()).toEqual([]);
expect(notify).toHaveBeenCalledTimes(5);
stop();
});
it("updates titles without moving windows and calls the latest actions without snapshot churn", () => {
const focus = vi.fn(), close = vi.fn();
registerFloatingWindow({ id: "chart", title: "旧标题", focus: vi.fn(), close: vi.fn() });
const initial = floatingWindowEntries();
registerFloatingWindow({ id: "chart", title: "旧标题", focus, close });
expect(floatingWindowEntries()).toBe(initial);
initial[0].focus();
initial[0].close();
expect(focus).toHaveBeenCalledOnce();
expect(close).toHaveBeenCalledOnce();
registerFloatingWindow({ id: "chart", title: "新标题", focus, close });
expect(floatingWindowEntries()[0].title).toBe("新标题");
expect(initial[0].title).toBe("旧标题");
expect(floatingWindowOrder()).toEqual(["chart"]);
removeFloatingWindow("chart");
initial[0].focus();
initial[0].close();
expect(focus).toHaveBeenCalledOnce();
expect(close).toHaveBeenCalledOnce();
});