| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- /**
- * 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 (
- <div data-testid="pager-harness">
- <ListPager total={total} page={0} pageSize={20} onPageChange={() => {}} unit="条" />
- <button type="button" onClick={() => setTotal(16)}>fill</button>
- </div>
- );
- }
- describe("ListPager Hooks 回归", () => {
- afterEach(() => cleanup());
- it("total=0 仍展示「共 0 条」", () => {
- render(<ListPager total={0} page={0} pageSize={20} onPageChange={() => {}} unit="条" />);
- expect(screen.getByText("共 0 条")).toBeTruthy();
- });
- it("total 从 0 升到有数据时不抛 Hook 错误", () => {
- const spy = vi.spyOn(console, "error").mockImplementation(() => {});
- render(<Harness initialTotal={0} />);
- 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();
- });
- });
|