完善测试框架;新增部分测试文件
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import React from "react";
|
||||
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
||||
import ValveIsolation from "./ValveIsolation";
|
||||
import axios from "axios";
|
||||
import "@testing-library/jest-dom";
|
||||
|
||||
// Mock dependencies
|
||||
jest.mock("axios");
|
||||
const mockedAxios = axios as jest.Mocked<typeof axios>;
|
||||
|
||||
jest.mock("@refinedev/core", () => ({
|
||||
useNotification: () => ({
|
||||
open: jest.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock config
|
||||
jest.mock("@config/config", () => ({
|
||||
config: {
|
||||
BACKEND_URL: "http://test-api.com",
|
||||
},
|
||||
NETWORK_NAME: "test-network",
|
||||
// If config is a default export or named export, adjust accordingly.
|
||||
// Based on usage: import { config, NETWORK_NAME } from '@config/config';
|
||||
// The mock above covers named exports.
|
||||
}));
|
||||
|
||||
describe("ValveIsolation Component", () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test("renders input and analyze button", () => {
|
||||
render(<ValveIsolation />);
|
||||
expect(screen.getByLabelText(/爆管管段ID/i)).toBeInTheDocument();
|
||||
expect(screen.getByText(/开始分析/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test("calls API with correct parameters when analyze is clicked", async () => {
|
||||
mockedAxios.get.mockResolvedValueOnce({
|
||||
data: {
|
||||
accident_element: "pipe1",
|
||||
accident_type: "Burst",
|
||||
affected_nodes: ["node1", "node2"],
|
||||
must_close_valves: ["valve1"],
|
||||
optional_valves: [],
|
||||
isolatable: true,
|
||||
},
|
||||
});
|
||||
|
||||
render(<ValveIsolation />);
|
||||
|
||||
const input = screen.getByLabelText(/爆管管段ID/i);
|
||||
fireEvent.change(input, { target: { value: "pipe1" } });
|
||||
|
||||
const button = screen.getByText(/开始分析/i);
|
||||
fireEvent.click(button);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockedAxios.get).toHaveBeenCalledWith(
|
||||
"http://test-api.com/api/v1/valve_isolation_analysis",
|
||||
{
|
||||
params: {
|
||||
network: "test-network",
|
||||
accident_element: "pipe1",
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test("displays results after successful analysis", async () => {
|
||||
mockedAxios.get.mockResolvedValueOnce({
|
||||
data: {
|
||||
accident_element: "pipe1",
|
||||
accident_type: "Burst",
|
||||
affected_nodes: ["nodeA"],
|
||||
must_close_valves: ["valveA", "valveB"],
|
||||
optional_valves: [],
|
||||
isolatable: true,
|
||||
},
|
||||
});
|
||||
|
||||
render(<ValveIsolation />);
|
||||
|
||||
fireEvent.change(screen.getByLabelText(/爆管管段ID/i), {
|
||||
target: { value: "pipe1" },
|
||||
});
|
||||
fireEvent.click(screen.getByText(/开始分析/i));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("可隔离")).toBeInTheDocument();
|
||||
expect(screen.getByText("valveA")).toBeInTheDocument();
|
||||
expect(screen.getByText("valveB")).toBeInTheDocument();
|
||||
expect(screen.getByText("nodeA")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user