import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import { apiFetch } from "@/lib/apiFetch"; import SCADADataPanel from "./SCADADataPanel"; jest.mock("@/lib/apiFetch", () => ({ apiFetch: jest.fn() })); jest.mock("@refinedev/core", () => ({ useGetIdentity: () => ({ data: { id: "test-user" } }), useNotification: () => ({ open: jest.fn() }), })); jest.mock("@mui/x-date-pickers", () => ({ DateTimePicker: () => null, LocalizationProvider: ({ children }: { children: React.ReactNode }) => children, })); jest.mock("echarts-for-react", () => () => null); const response = (payload: unknown) => ({ ok: true, json: async () => payload }) as Response; describe("SCADADataPanel requests", () => { beforeEach(() => { jest.mocked(apiFetch).mockImplementation(async (url) => response( String(url).includes("/scada-devices") ? [ { device_id: "S-1", device_type: "pressure", node_id: "J-1", link_id: null }, { device_id: "S-2", device_type: "pressure", node_id: "J-2", link_id: null }, ] : { series: [] }, ), ); }); afterEach(() => jest.clearAllMocks()); it("does not request history again when the same device IDs arrive in a new array", async () => { const { rerender } = render(); await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(2)); rerender(); await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(2)); rerender(); await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(4)); expect(jest.mocked(apiFetch).mock.calls[3][1]?.body).toContain('"device_ids":["S-2"]'); fireEvent.click(screen.getByRole("button", { name: "刷新" })); await waitFor(() => expect(apiFetch).toHaveBeenCalledTimes(6)); }); });