/**
* D-7/O-3:ListPager 不得在早退后条件性调用 Hook;
* total 从 0 → >0 重渲染必须不抛(硬加载/筛选空态回归)。
*/
import { describe, expect, it, vi, afterEach } from "vitest";
import { cleanup, render, screen, act, within } from "@testing-library/react";
import { useState } from "react";
import { ListPager } from "../ListPager";
function Harness({ initialTotal }: { initialTotal: number }) {
const [total, setTotal] = useState(initialTotal);
return (
{}} unit="条" />
);
}
describe("ListPager Hooks 回归", () => {
afterEach(() => cleanup());
it("total=0 仍展示「共 0 条」", () => {
render( {}} unit="条" />);
expect(screen.getByText("共 0 条")).toBeTruthy();
});
it("total 从 0 升到有数据时不抛 Hook 错误", () => {
const spy = vi.spyOn(console, "error").mockImplementation(() => {});
render();
const root = screen.getByTestId("pager-harness");
expect(within(root).getByText("共 0 条")).toBeTruthy();
act(() => {
within(root).getByRole("button", { name: "fill" }).click();
});
expect(within(root).getByText(/共 16 条/)).toBeTruthy();
const hookNoise = spy.mock.calls.some((args) =>
String(args[0] ?? "").includes("Invalid hook call")
|| String(args[0] ?? "").includes("Rules of Hooks")
|| String(args[0] ?? "").includes("Rendered more hooks"),
);
expect(hookNoise).toBe(false);
spy.mockRestore();
});
});