完善测试框架;新增部分测试文件

This commit is contained in:
JIANG
2026-01-30 18:27:09 +08:00
parent 799eab03d0
commit a3f4b477bc
4 changed files with 221 additions and 0 deletions

View File

@@ -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();
});
});
});

View File

@@ -0,0 +1,21 @@
const { parseColor } = require('./parseColor');
describe('parseColor', () => {
it('should parse hex color', () => {
expect(parseColor('#FF0000')).toEqual({ r: 255, g: 0, b: 0 });
expect(parseColor('00FF00')).toEqual({ r: 0, g: 255, b: 0 });
});
it('should parse rgb color', () => {
expect(parseColor('rgb(0, 0, 255)')).toEqual({ r: 0, g: 0, b: 255, a: 1 });
});
it('should parse rgba color', () => {
expect(parseColor('rgba(0, 0, 255, 0.5)')).toEqual({ r: 0, g: 0, b: 255, a: 0.5 });
});
it('should default alpha to 1 if not provided in rgba-like pattern', () => {
// The regex supports optional alpha
expect(parseColor('rgba(0, 0, 255)')).toEqual({ r: 0, g: 0, b: 255, a: 1 });
});
});