44 lines
2.4 KiB
TypeScript
44 lines
2.4 KiB
TypeScript
import { act, renderHook } from "@testing-library/react";
|
|
import { beforeEach, expect, it, vi } from "vitest";
|
|
import { useDismissedWorkbenchAlerts } from "./use-dismissed-workbench-alerts";
|
|
import type { WorkbenchAlert } from "../types";
|
|
|
|
const alert: WorkbenchAlert = { id: "alert-1", title: "压力异常", description: "等待核查", time: "08:00" };
|
|
beforeEach(() => localStorage.clear());
|
|
|
|
it("keeps removed alerts hidden after refresh while new notifications still appear", () => {
|
|
const { result, rerender, unmount } = renderHook(({ alerts }) => useDismissedWorkbenchAlerts(alerts, "project-1", "operator"),
|
|
{ initialProps: { alerts: [alert] } });
|
|
act(() => { expect(result.current.dismissAlert(alert)).toBe(true); });
|
|
expect(result.current.alerts).toEqual([]);
|
|
rerender({ alerts: [{ ...alert }, { ...alert, id: "alert-2" }] });
|
|
expect(result.current.alerts.map((item) => item.id)).toEqual(["alert-2"]);
|
|
unmount();
|
|
const restored = renderHook(() => useDismissedWorkbenchAlerts([alert], "project-1", "operator"));
|
|
expect(restored.result.current.alerts).toEqual([]);
|
|
});
|
|
|
|
it("isolates users and projects and shows a new notification version", () => {
|
|
const item = { ...alert, notificationVersion: 1 };
|
|
const { result, rerender } = renderHook(({ project, user, version }) =>
|
|
useDismissedWorkbenchAlerts([{ ...item, notificationVersion: version }], project, user),
|
|
{ initialProps: { project: "project-1", user: "operator", version: 1 } });
|
|
act(() => { result.current.dismissAlert(item); });
|
|
rerender({ project: "project-2", user: "operator", version: 1 });
|
|
expect(result.current.alerts).toHaveLength(1);
|
|
rerender({ project: "project-1", user: "viewer", version: 1 });
|
|
expect(result.current.alerts).toHaveLength(1);
|
|
rerender({ project: "project-1", user: "operator", version: 1 });
|
|
expect(result.current.alerts).toEqual([]);
|
|
rerender({ project: "project-1", user: "operator", version: 2 });
|
|
expect(result.current.alerts).toHaveLength(1);
|
|
});
|
|
|
|
it("preserves the row when local storage cannot save the removal", () => {
|
|
const { result } = renderHook(() => useDismissedWorkbenchAlerts([alert], "project-1", "operator"));
|
|
const spy = vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => { throw new Error("storage unavailable"); });
|
|
act(() => { expect(result.current.dismissAlert(alert)).toBe(false); });
|
|
expect(result.current.alerts).toEqual([alert]);
|
|
spy.mockRestore();
|
|
});
|