From 13d12070113929bbf8a19a94e71e39c2936fa4c4 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Fri, 24 Jul 2026 22:30:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20lint=20=E8=AD=A6?= =?UTF-8?q?=E5=91=8A=20+=20=E6=B7=BB=E5=8A=A0=20format=20=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E5=87=BD=E6=95=B0=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - constants.tsx: 添加 react-refresh/only-export-components 禁用注释 - 新增 format.test.ts: 为 format 工具函数添加单元测试(8 个用例) --- .../src/pages/voice-materials/constants.tsx | 1 + .../test/pages/voice-materials/format.test.ts | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 apps/web/src/test/pages/voice-materials/format.test.ts diff --git a/apps/web/src/pages/voice-materials/constants.tsx b/apps/web/src/pages/voice-materials/constants.tsx index 62f3043f8..5babc19dd 100644 --- a/apps/web/src/pages/voice-materials/constants.tsx +++ b/apps/web/src/pages/voice-materials/constants.tsx @@ -1,6 +1,7 @@ /** * 配音素材库常量 */ +/* eslint-disable react-refresh/only-export-components */ import React from "react" import { ManOutlined, WomanOutlined, UserOutlined, SoundOutlined } from "@ant-design/icons" import type { VoiceGender } from "./types" diff --git a/apps/web/src/test/pages/voice-materials/format.test.ts b/apps/web/src/test/pages/voice-materials/format.test.ts new file mode 100644 index 000000000..4fcd5a5d8 --- /dev/null +++ b/apps/web/src/test/pages/voice-materials/format.test.ts @@ -0,0 +1,75 @@ +import { describe, expect, it } from "vitest" +import { + genderLabel, + genderIcon, + genderClass, + formatDuration, + formatFileSize, + formatDate, +} from "@/pages/voice-materials/utils/format" + +describe("voice-materials format utils", () => { + describe("genderLabel", () => { + it("应返回正确的性别标签", () => { + expect(genderLabel("male")).toBe("男声") + expect(genderLabel("female")).toBe("女声") + expect(genderLabel("child")).toBe("童声") + expect(genderLabel("neutral")).toBe("中性") + }) + + it("未知性别返回原值", () => { + expect(genderLabel("unknown" as any)).toBe("unknown") + }) + }) + + describe("genderIcon", () => { + it("应返回图标组件", () => { + expect(genderIcon("male")).toBeDefined() + expect(genderIcon("female")).toBeDefined() + expect(genderIcon("child")).toBeDefined() + expect(genderIcon("neutral")).toBeDefined() + }) + + it("未知性别返回 null", () => { + expect(genderIcon("unknown" as any)).toBeNull() + }) + }) + + describe("genderClass", () => { + it("应返回正确的 CSS 类名", () => { + expect(genderClass("male")).toBe("vmat-gender--male") + expect(genderClass("female")).toBe("vmat-gender--female") + }) + }) + + describe("formatDuration", () => { + it("应正确格式化秒数", () => { + expect(formatDuration(0)).toBe("0:00") + expect(formatDuration(5)).toBe("0:05") + expect(formatDuration(59)).toBe("0:59") + expect(formatDuration(60)).toBe("1:00") + expect(formatDuration(65)).toBe("1:05") + expect(formatDuration(125)).toBe("2:05") + expect(formatDuration(3600)).toBe("60:00") + }) + }) + + describe("formatFileSize", () => { + it("应正确格式化文件大小", () => { + expect(formatFileSize(0)).toBe("0 B") + expect(formatFileSize(512)).toBe("512 B") + expect(formatFileSize(1024)).toBe("1.0 KB") + expect(formatFileSize(1536)).toBe("1.5 KB") + expect(formatFileSize(1024 * 1024)).toBe("1.0 MB") + expect(formatFileSize(1024 * 1024 * 2.5)).toBe("2.5 MB") + }) + }) + + describe("formatDate", () => { + it("应格式化 ISO 日期字符串", () => { + const result = formatDate("2026-07-24T10:30:00.000Z") + // 格式应该是 YYYY/MM/DD 格式 + expect(result).toMatch(/^\d{4}\/\d{2}\/\d{2}$/) + }) + }) +})