fix(frontend): RefineView 取消在途 refine——AbortController 防段切换竞态(CR-H12)
This commit is contained in:
@@ -122,4 +122,40 @@ describe("useRefine", () => {
|
||||
expect(result.current.status).toBe("idle");
|
||||
expect(result.current.result).toBeNull();
|
||||
});
|
||||
|
||||
it("中止后迟到的响应不覆盖状态(段切换竞态)", async () => {
|
||||
// Arrange:post 挂起,手动兑现以模拟在途请求。
|
||||
let resolvePost!: (v: unknown) => void;
|
||||
const pending = new Promise((r) => {
|
||||
resolvePost = r;
|
||||
});
|
||||
post.mockReturnValue(pending);
|
||||
|
||||
const { result } = renderHook(() => useRefine());
|
||||
|
||||
// Act:发起回炉(进入 refining),随后 abort(模拟切段取消在途)。
|
||||
let refinePromise!: Promise<unknown>;
|
||||
act(() => {
|
||||
refinePromise = result.current.refine("p1", 1, "新段");
|
||||
});
|
||||
expect(result.current.status).toBe("refining");
|
||||
|
||||
act(() => {
|
||||
result.current.abort();
|
||||
});
|
||||
|
||||
// 迟到的旧响应兑现——应被丢弃:不写状态、不弹 toast。
|
||||
await act(async () => {
|
||||
resolvePost({
|
||||
data: { original: "旧段", refined: "旧改写" },
|
||||
error: null,
|
||||
});
|
||||
await refinePromise;
|
||||
});
|
||||
|
||||
// Assert:结果仍为空、未落到 done、未误报 toast。
|
||||
expect(result.current.result).toBeNull();
|
||||
expect(result.current.status).not.toBe("done");
|
||||
expect(toast).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user