fix(web): 规则删除按操作隔离 busy(deletingId/adding) + 全空规则页折叠为单一引导
This commit is contained in:
@@ -23,10 +23,11 @@ describe("useRules", () => {
|
||||
});
|
||||
afterEach(() => vi.clearAllMocks());
|
||||
|
||||
it("初始 items = 传入值,busy=false", () => {
|
||||
it("初始 items = 传入值,adding=false、deletingId=null", () => {
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
expect(result.current.items).toEqual(seed);
|
||||
expect(result.current.busy).toBe(false);
|
||||
expect(result.current.adding).toBe(false);
|
||||
expect(result.current.deletingId).toBeNull();
|
||||
});
|
||||
|
||||
it("add 空白正文:弹错、不请求、返回 false", async () => {
|
||||
@@ -52,7 +53,7 @@ describe("useRules", () => {
|
||||
|
||||
expect(ok).toBe(true);
|
||||
expect(result.current.items).toEqual([...seed, authoritative]);
|
||||
expect(result.current.busy).toBe(false);
|
||||
expect(result.current.adding).toBe(false);
|
||||
expect(toast).toHaveBeenCalledWith("已新增规则", "success");
|
||||
});
|
||||
|
||||
@@ -80,6 +81,48 @@ describe("useRules", () => {
|
||||
expect(toast).toHaveBeenCalledWith("已删除规则", "success");
|
||||
});
|
||||
|
||||
it("add 进行中:adding=true 而 deletingId 仍为 null(互不冻结)", async () => {
|
||||
let resolvePost: (v: { data: RuleView; error: null }) => void = () => {};
|
||||
post.mockReturnValue(
|
||||
new Promise((res) => {
|
||||
resolvePost = res;
|
||||
}),
|
||||
);
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
let done: Promise<boolean> = Promise.resolve(false);
|
||||
act(() => {
|
||||
done = result.current.add("p1", "global", "新规则");
|
||||
});
|
||||
expect(result.current.adding).toBe(true);
|
||||
expect(result.current.deletingId).toBeNull();
|
||||
await act(async () => {
|
||||
resolvePost({ data: { id: "r1", level: "global", content: "新规则" }, error: null });
|
||||
await done;
|
||||
});
|
||||
expect(result.current.adding).toBe(false);
|
||||
});
|
||||
|
||||
it("remove 进行中:deletingId=目标 id 而 adding 仍为 false(只冻结该行)", async () => {
|
||||
let resolveDel: (v: { error: null }) => void = () => {};
|
||||
del.mockReturnValue(
|
||||
new Promise((res) => {
|
||||
resolveDel = res;
|
||||
}),
|
||||
);
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
let done: Promise<boolean> = Promise.resolve(false);
|
||||
act(() => {
|
||||
done = result.current.remove("p1", "r0");
|
||||
});
|
||||
expect(result.current.deletingId).toBe("r0");
|
||||
expect(result.current.adding).toBe(false);
|
||||
await act(async () => {
|
||||
resolveDel({ error: null });
|
||||
await done;
|
||||
});
|
||||
expect(result.current.deletingId).toBeNull();
|
||||
});
|
||||
|
||||
it("remove 后端 error:回滚、弹错、返回 false", async () => {
|
||||
del.mockResolvedValue({ error: { detail: "boom" } });
|
||||
const { result } = renderHook(() => useRules(seed));
|
||||
|
||||
Reference in New Issue
Block a user