41 lines
2.3 KiB
TypeScript
41 lines
2.3 KiB
TypeScript
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
import { afterEach, expect, it, vi } from "vitest";
|
|
import { ScadaAlertAtmosphere } from "./scada-alert-atmosphere";
|
|
import type { ScadaAlertAtmosphereState } from "../hooks/use-scada-alert-notices";
|
|
|
|
const preview: ScadaAlertAtmosphereState = { id: "preview-1", projectId: "project-1", preview: true, alert: null };
|
|
afterEach(() => { cleanup(); vi.useRealTimers(); });
|
|
|
|
it("shows a local preview with a dismiss button and no investigation action", () => {
|
|
const onDismiss = vi.fn(), onOpen = vi.fn();
|
|
render(<ScadaAlertAtmosphere atmosphere={preview} onDismiss={onDismiss} onOpen={onOpen} />);
|
|
expect(screen.getByRole("status")).toHaveTextContent("爆管告警效果预览");
|
|
expect(screen.queryByRole("button", { name: "查看调查" })).not.toBeInTheDocument();
|
|
const close = screen.getByRole("button", { name: "关闭告警灯效" });
|
|
expect(close).toHaveTextContent("关闭灯效");
|
|
fireEvent.click(close);
|
|
expect(onDismiss).toHaveBeenCalledWith("preview-1");
|
|
expect(onOpen).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("opens the actual linked investigation while retaining the visual warning", () => {
|
|
const onDismiss = vi.fn(), onOpen = vi.fn();
|
|
const alert = { id: "incident-1", conditionId: "operational-task:task-1", title: "SCADA 指标异常", description: "已启动调查", time: "08:00" };
|
|
render(<ScadaAlertAtmosphere atmosphere={{ ...preview, id: alert.id, preview: false, alert }} onDismiss={onDismiss} onOpen={onOpen} />);
|
|
expect(screen.getByRole("status")).toHaveTextContent("爆管风险调查中");
|
|
fireEvent.click(screen.getByRole("button", { name: "查看调查" }));
|
|
expect(onDismiss).not.toHaveBeenCalled();
|
|
expect(onOpen).toHaveBeenCalledWith(alert);
|
|
});
|
|
|
|
it("keeps the warning for a long-running event until its state is removed", () => {
|
|
vi.useFakeTimers();
|
|
const onDismiss = vi.fn(), onOpen = vi.fn();
|
|
const { rerender } = render(<ScadaAlertAtmosphere atmosphere={preview} onDismiss={onDismiss} onOpen={onOpen} />);
|
|
act(() => vi.advanceTimersByTime(24 * 60 * 60_000));
|
|
expect(onDismiss).not.toHaveBeenCalled();
|
|
expect(screen.getByRole("status")).toBeVisible();
|
|
rerender(<ScadaAlertAtmosphere atmosphere={null} onDismiss={onDismiss} onOpen={onOpen} />);
|
|
expect(screen.queryByRole("status")).not.toBeInTheDocument();
|
|
});
|