test: 重构测试体系 — 建立组件测试层、补充后端测试、清理低质量测试
- 新增 jsdom + @testing-library/react 组件测试环境 - 新增 12 个组件测试,覆盖所有前端组件 - 补充后端 middleware 和 helpers 单元测试 - 删除伪测试 use-target-detail-logic.test.ts - 精简过度枚举的 color-threshold.test.ts - 新增 bunfig.toml 配置测试 preload - 更新 DEVELOPMENT.md 测试章节 - 安装 @types/jsdom 修复类型声明
This commit is contained in:
127
tests/web/components/App.test.tsx
Normal file
127
tests/web/components/App.test.tsx
Normal file
@@ -0,0 +1,127 @@
|
||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
import "../../../tests/web/test-utils";
|
||||
import { render } from "@testing-library/react";
|
||||
import { describe, expect, test, vi } from "bun:test";
|
||||
|
||||
import { App } from "../../../src/web/app";
|
||||
|
||||
// Mock hooks
|
||||
void vi.mock("../../../src/web/hooks/use-queries", () => ({
|
||||
useDashboard: vi.fn(() => ({
|
||||
data: {
|
||||
summary: {
|
||||
down: 0,
|
||||
incidents: 0,
|
||||
lastCheckTime: "2025-01-15T10:00:00.000Z",
|
||||
total: 0,
|
||||
up: 0,
|
||||
window: {
|
||||
from: "2025-01-14T10:00:00.000Z",
|
||||
label: "24h",
|
||||
to: "2025-01-15T10:00:00.000Z",
|
||||
},
|
||||
},
|
||||
targets: [],
|
||||
},
|
||||
dataUpdatedAt: Date.now(),
|
||||
error: null,
|
||||
isFetching: false,
|
||||
isLoading: false,
|
||||
refetch: vi.fn(),
|
||||
})),
|
||||
useMeta: vi.fn(() => ({
|
||||
data: { checkerTypes: ["http", "cmd"] },
|
||||
})),
|
||||
}));
|
||||
|
||||
void vi.mock("../../../src/web/hooks/use-target-detail", () => ({
|
||||
useTargetDetail: vi.fn(() => ({
|
||||
activeTab: "overview",
|
||||
closeDrawer: vi.fn(),
|
||||
handlePageChange: vi.fn(),
|
||||
handleTabChange: vi.fn(),
|
||||
handleTimeChange: vi.fn(),
|
||||
historyData: {
|
||||
items: [],
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
total: 0,
|
||||
},
|
||||
historyLoading: false,
|
||||
metricsData: null,
|
||||
metricsLoading: false,
|
||||
openDrawer: vi.fn(),
|
||||
selectedTarget: null,
|
||||
timeFrom: "",
|
||||
timeTo: "",
|
||||
})),
|
||||
}));
|
||||
|
||||
describe("App", () => {
|
||||
test("渲染不崩溃", () => {
|
||||
const { container } = render(<App />);
|
||||
expect(container.firstChild).not.toBeNull();
|
||||
});
|
||||
|
||||
test("loading 状态不崩溃", () => {
|
||||
const { useDashboard } = require("../../../src/web/hooks/use-queries");
|
||||
useDashboard.mockReturnValue({
|
||||
data: null,
|
||||
dataUpdatedAt: 0,
|
||||
error: null,
|
||||
isFetching: true,
|
||||
isLoading: true,
|
||||
refetch: vi.fn(),
|
||||
});
|
||||
|
||||
const { container } = render(<App />);
|
||||
expect(container.firstChild).not.toBeNull();
|
||||
});
|
||||
|
||||
test("错误状态不崩溃", () => {
|
||||
const { useDashboard } = require("../../../src/web/hooks/use-queries");
|
||||
useDashboard.mockReturnValue({
|
||||
data: null,
|
||||
dataUpdatedAt: 0,
|
||||
error: { message: "Network error" },
|
||||
isFetching: false,
|
||||
isLoading: false,
|
||||
refetch: vi.fn(),
|
||||
});
|
||||
|
||||
const { container } = render(<App />);
|
||||
expect(container.firstChild).not.toBeNull();
|
||||
});
|
||||
|
||||
test("有数据状态不崩溃", () => {
|
||||
const { useDashboard } = require("../../../src/web/hooks/use-queries");
|
||||
useDashboard.mockReturnValue({
|
||||
data: {
|
||||
summary: {
|
||||
down: 1,
|
||||
incidents: 0,
|
||||
lastCheckTime: "2025-01-15T10:00:00.000Z",
|
||||
total: 2,
|
||||
up: 1,
|
||||
window: {
|
||||
from: "2025-01-14T10:00:00.000Z",
|
||||
label: "24h",
|
||||
to: "2025-01-15T10:00:00.000Z",
|
||||
},
|
||||
},
|
||||
targets: [],
|
||||
},
|
||||
dataUpdatedAt: Date.now(),
|
||||
error: null,
|
||||
isFetching: false,
|
||||
isLoading: false,
|
||||
refetch: vi.fn(),
|
||||
});
|
||||
|
||||
const { container } = render(<App />);
|
||||
expect(container.firstChild).not.toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user