feat(inbox): 素材持久化 CRUD — 数据库表 + API + 前端接入
- 新增 materials 表(id/projectId/description/associatedDate/status/createdAt/updatedAt) - 新增 4 个后端 API 路由(list/create/get/delete)+ 13 个测试 - 新增 use-materials hooks(TanStack Query) - 收集箱页面重构为三层架构(InboxPage + MaterialSidebar + MaterialDetailPanel) - MaterialCard: Popconfirm 删除确认 + 粗粒度时间格式 - MaterialContent: 展示状态标签 + createdAt - 更新开发文档 backend.md / frontend.md
This commit is contained in:
@@ -2,11 +2,21 @@ import { fireEvent, screen, waitFor } from "@testing-library/react";
|
||||
import { describe, expect, test, vi } from "bun:test";
|
||||
import { createElement } from "react";
|
||||
|
||||
import type { Material } from "../../../../src/web/features/inbox/types";
|
||||
import type { CreateMaterialRequest, Material } from "../../../../src/shared/api";
|
||||
|
||||
import { AddMaterialModal } from "../../../../src/web/features/inbox/components/AddMaterialModal";
|
||||
import { renderWithProviders } from "../../test-utils";
|
||||
|
||||
const MOCK_CREATED: Material = {
|
||||
associatedDate: "2026-06-03",
|
||||
createdAt: "2026-06-03T00:00:00.000Z",
|
||||
description: "测试描述",
|
||||
id: "new-id",
|
||||
projectId: "project-1",
|
||||
status: "pending",
|
||||
updatedAt: "2026-06-03T00:00:00.000Z",
|
||||
};
|
||||
|
||||
describe("AddMaterialModal", () => {
|
||||
test("打开时渲染表单字段", () => {
|
||||
renderWithProviders(
|
||||
@@ -49,7 +59,8 @@ describe("AddMaterialModal", () => {
|
||||
});
|
||||
|
||||
test("点击确定触发表单提交", async () => {
|
||||
const onAdd = vi.fn<(material: Material) => void>();
|
||||
const onAdd = vi.fn<(body: CreateMaterialRequest) => Promise<Material>>();
|
||||
onAdd.mockResolvedValue(MOCK_CREATED);
|
||||
renderWithProviders(
|
||||
createElement(AddMaterialModal, {
|
||||
onAdd,
|
||||
@@ -69,9 +80,29 @@ describe("AddMaterialModal", () => {
|
||||
|
||||
const callArgs = onAdd.mock.calls[0];
|
||||
expect(callArgs).toBeDefined();
|
||||
const calledMaterial = callArgs![0];
|
||||
expect(calledMaterial.description).toBe("测试描述");
|
||||
expect(calledMaterial.associatedDate).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||
expect(calledMaterial.id).toBeTruthy();
|
||||
const calledBody = callArgs![0];
|
||||
expect(calledBody.description).toBe("测试描述");
|
||||
expect(calledBody.associatedDate).toMatch(/^\d{4}-\d{2}-\d{2}$/);
|
||||
});
|
||||
|
||||
test("提交失败显示错误提示", async () => {
|
||||
const onAdd = vi.fn<(body: CreateMaterialRequest) => Promise<Material>>();
|
||||
onAdd.mockRejectedValue(new Error("网络错误"));
|
||||
renderWithProviders(
|
||||
createElement(AddMaterialModal, {
|
||||
onAdd,
|
||||
onOpenChange: vi.fn(),
|
||||
open: true,
|
||||
}),
|
||||
);
|
||||
|
||||
const textarea = screen.getByPlaceholderText("请输入素材描述");
|
||||
fireEvent.change(textarea, { target: { value: "测试描述" } });
|
||||
|
||||
fireEvent.click(screen.getByText("确 定"));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onAdd).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user