test: 接入 jsdom 测试栈, 把 15 个 React hooks 纳入 80% 覆盖率门禁
新增 jsdom + @testing-library/react, 为 lib/** 全部 use*.ts hooks 写 renderHook 单测: 生成/CRUD(world/character/generator/outline/accept/foreshadow/rules)、 SSE流(draftStream/reviewStream)、定时轮询(autosave/jobPoll/kimiOauth)、 文风与注入(refine/styleLearn/injection)。覆盖率 mock api 客户端+Toast, 不打真实 LLM。 vitest.config.ts 去掉 use*.ts 排除; 前端覆盖率 63%→95%。CLAUDE.md 同步说明。
This commit is contained in:
94
apps/web/lib/rules/useRules.test.ts
Normal file
94
apps/web/lib/rules/useRules.test.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
// @vitest-environment jsdom
|
||||
import { act, renderHook } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { RuleView } from "@/lib/api/types";
|
||||
import { useRules } from "./useRules";
|
||||
|
||||
const post = vi.fn();
|
||||
const del = vi.fn();
|
||||
const toast = vi.fn();
|
||||
vi.mock("@/lib/api/client", () => ({
|
||||
api: { POST: (...a: unknown[]) => post(...a), DELETE: (...a: unknown[]) => del(...a) },
|
||||
}));
|
||||
vi.mock("@/components/Toast", () => ({ useToast: () => toast }));
|
||||
|
||||
const seed: RuleView[] = [{ id: "r0", level: "global", content: "已有规则" }];
|
||||
|
||||
describe("useRules", () => {
|
||||
beforeEach(() => {
|
||||
post.mockReset();
|
||||
del.mockReset();
|
||||
toast.mockReset();
|
||||
});
|
||||
afterEach(() => vi.clearAllMocks());
|
||||
|
||||
it("初始 items = 传入值,busy=false", () => {
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
expect(result.current.items).toEqual(seed);
|
||||
expect(result.current.busy).toBe(false);
|
||||
});
|
||||
|
||||
it("add 空白正文:弹错、不请求、返回 false", async () => {
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
let ok = true;
|
||||
await act(async () => {
|
||||
ok = await result.current.add("p1", "global", " ");
|
||||
});
|
||||
expect(ok).toBe(false);
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
expect(toast).toHaveBeenCalledWith("请填写规则正文。", "error");
|
||||
});
|
||||
|
||||
it("add 成功:用服务端权威行替换乐观行、弹成功、返回 true", async () => {
|
||||
const authoritative: RuleView = { id: "r1", level: "global", content: "新规则" };
|
||||
post.mockResolvedValue({ data: authoritative, error: null });
|
||||
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
let ok = false;
|
||||
await act(async () => {
|
||||
ok = await result.current.add("p1", "global", "新规则");
|
||||
});
|
||||
|
||||
expect(ok).toBe(true);
|
||||
expect(result.current.items).toEqual([...seed, authoritative]);
|
||||
expect(result.current.busy).toBe(false);
|
||||
expect(toast).toHaveBeenCalledWith("已新增规则", "success");
|
||||
});
|
||||
|
||||
it("add 后端 error:回滚到快照、弹错、返回 false", async () => {
|
||||
post.mockResolvedValue({ data: null, error: { detail: "boom" } });
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
let ok = true;
|
||||
await act(async () => {
|
||||
ok = await result.current.add("p1", "global", "新规则");
|
||||
});
|
||||
expect(ok).toBe(false);
|
||||
expect(result.current.items).toEqual(seed);
|
||||
expect(toast).toHaveBeenCalledWith("新增规则失败,请稍后重试。", "error");
|
||||
});
|
||||
|
||||
it("remove 成功:乐观移除、弹成功、返回 true", async () => {
|
||||
del.mockResolvedValue({ error: null });
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
let ok = false;
|
||||
await act(async () => {
|
||||
ok = await result.current.remove("p1", "r0");
|
||||
});
|
||||
expect(ok).toBe(true);
|
||||
expect(result.current.items).toEqual([]);
|
||||
expect(toast).toHaveBeenCalledWith("已删除规则", "success");
|
||||
});
|
||||
|
||||
it("remove 后端 error:回滚、弹错、返回 false", async () => {
|
||||
del.mockResolvedValue({ error: { detail: "boom" } });
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
let ok = true;
|
||||
await act(async () => {
|
||||
ok = await result.current.remove("p1", "r0");
|
||||
});
|
||||
expect(ok).toBe(false);
|
||||
expect(result.current.items).toEqual(seed);
|
||||
expect(toast).toHaveBeenCalledWith("删除规则失败,请稍后重试。", "error");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user