Files
Alfred/tests/web/features/settings/SettingsPage.test.tsx

118 lines
3.9 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { screen, waitFor } from "@testing-library/react";
import { createElement } from "react";
import { SettingsPage } from "../../../../src/web/features/settings/index";
import { installFetchMock, jsonResponse, renderWithProviders } from "../../test-utils";
function mockSettingsResponse(theme = "system", compact = false): Response {
return jsonResponse({ compact, theme });
}
function mockEmptyModelsResponse(): Response {
return jsonResponse({ items: [], page: 1, pageSize: 200, total: 0 });
}
describe("SettingsPage", () => {
test("渲染主题卡片", () => {
installFetchMock((call) => {
if (call.url.includes("/api/settings")) return mockSettingsResponse();
if (call.url.includes("/api/models")) return mockEmptyModelsResponse();
return jsonResponse({});
});
renderWithProviders(createElement(SettingsPage));
expect(screen.getByText("主题")).not.toBeNull();
});
test("渲染主题模式 Radio.Group 选项", () => {
installFetchMock((call) => {
if (call.url.includes("/api/settings")) return mockSettingsResponse();
if (call.url.includes("/api/models")) return mockEmptyModelsResponse();
return jsonResponse({});
});
renderWithProviders(createElement(SettingsPage));
expect(screen.getByText("系统")).not.toBeNull();
expect(screen.getByText("明亮")).not.toBeNull();
expect(screen.getByText("黑暗")).not.toBeNull();
});
test("渲染紧凑模式标签和开关", () => {
installFetchMock((call) => {
if (call.url.includes("/api/settings")) return mockSettingsResponse();
if (call.url.includes("/api/models")) return mockEmptyModelsResponse();
return jsonResponse({});
});
renderWithProviders(createElement(SettingsPage));
expect(screen.getByText("紧凑模式")).not.toBeNull();
});
test("渲染水平表单结构", () => {
installFetchMock((call) => {
if (call.url.includes("/api/settings")) return mockSettingsResponse();
if (call.url.includes("/api/models")) return mockEmptyModelsResponse();
return jsonResponse({});
});
renderWithProviders(createElement(SettingsPage));
const form = document.querySelector(".ant-form");
expect(form).not.toBeNull();
});
test("不再使用 Segmented", () => {
installFetchMock((call) => {
if (call.url.includes("/api/settings")) return mockSettingsResponse();
if (call.url.includes("/api/models")) return mockEmptyModelsResponse();
return jsonResponse({});
});
renderWithProviders(createElement(SettingsPage));
expect(document.querySelector(".ant-segmented")).toBeNull();
});
test("不显示保存状态文本(已迁移到 toast)", () => {
installFetchMock((call) => {
if (call.url.includes("/api/settings")) return mockSettingsResponse();
if (call.url.includes("/api/models")) return mockEmptyModelsResponse();
return jsonResponse({});
});
renderWithProviders(createElement(SettingsPage));
expect(screen.queryByText("保存中...")).toBeNull();
});
test("GET /api/settings 获取已保存主题和紧凑设置", async () => {
installFetchMock((call) => {
if (call.url.includes("/api/settings")) return mockSettingsResponse("dark", true);
return jsonResponse({});
});
renderWithProviders(createElement(SettingsPage));
await waitFor(() => {
const radioGroup = document.querySelector(".ant-radio-group");
expect(radioGroup).not.toBeNull();
});
});
test("渲染模型卡片标题", () => {
installFetchMock((call) => {
if (call.url.includes("/api/settings")) return mockSettingsResponse();
if (call.url.includes("/api/models")) return mockEmptyModelsResponse();
return jsonResponse({});
});
renderWithProviders(createElement(SettingsPage));
expect(screen.getByText("模型")).not.toBeNull();
});
});