import { cleanup, render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, describe, expect, it, vi } from "vitest"; import { FeatureLocationPanel } from "./feature-location-panel"; afterEach(cleanup); const state = { pending: false, targets: [], targetResult: null, errorCode: null }; const createCommands = () => ({ highlight: vi.fn(), zoomToFeature: vi.fn(), locateAndHighlight: vi.fn(), clearHighlight: vi.fn() }); describe("FeatureLocationPanel", () => { it("uses the selected feature's source and ID together", async () => { const user = userEvent.setup(); const commands = createCommands(); const onUseSelectedFeature = vi.fn(); render(); await user.click(screen.getByRole("button", { name: "使用当前地图选中" })); expect(screen.getByLabelText("要素 ID(可多个)")).toHaveValue("PRESSURE-17"); await user.click(screen.getByRole("button", { name: "定位并高亮" })); expect(commands.locateAndHighlight).toHaveBeenCalledWith({ sourceId: "scada", featureIds: ["PRESSURE-17"] }); expect(onUseSelectedFeature).toHaveBeenCalledOnce(); }); it("keeps map actions disabled until the map is ready and while a request is pending", async () => { const user = userEvent.setup(); const commands = createCommands(); const props = { commands, controllerState: state, detailFeature: null }; const { rerender } = render(); await user.type(screen.getByLabelText("要素 ID(可多个)"), "P-17"); const action = () => screen.getByRole("button", { name: "定位并高亮" }); expect(action()).toBeDisabled(); rerender(); expect(action()).toBeEnabled(); rerender(); expect(screen.getByRole("button", { name: "正在定位要素" })).toBeDisabled(); expect(screen.getByRole("button", { name: "仅高亮" })).toBeDisabled(); expect(screen.getByRole("button", { name: "缩放到要素" })).toBeDisabled(); expect(commands.locateAndHighlight).not.toHaveBeenCalled(); }); });