import { fireEvent, render, screen } from "@testing-library/react"; import HoverPrefetchLink from "./HoverPrefetchLink"; /** * Testes de caminhos-base de HoverPrefetchLink (complemento de HoverPrefetchLink.test.tsx): * o aviso do toque ao dono do link, o pré-carregamento que liga uma vez só e não desliga, * e as demais propriedades do link, que passam inalteradas. */ jest.mock("next/link", () => ({ __esModule: true, default: ({ children, prefetch, ...props }: { children?: React.ReactNode; prefetch?: boolean | null } & Record< string, unknown >) => ( {children} ), })); const link = () => screen.getByRole("link", { name: "Cases" }); describe("HoverPrefetchLink", () => { it("path: touch with the caller's handler -> the handler runs once, with the event, and prefetching starts", () => { const onTouchStart = jest.fn(); render( Cases , ); fireEvent.touchStart(link()); expect(onTouchStart).toHaveBeenCalledTimes(1); expect(onTouchStart.mock.calls[0][0]).toMatchObject({ type: "touchstart" }); expect(link()).toHaveAttribute("data-prefetch", "null"); }); it("path: hover, then focus, then touch -> prefetching stays on and each handler runs every time", () => { const onMouseEnter = jest.fn(); const onFocus = jest.fn(); const onTouchStart = jest.fn(); render( Cases , ); fireEvent.mouseEnter(link()); expect(link()).toHaveAttribute("data-prefetch", "null"); fireEvent.mouseEnter(link()); fireEvent.focus(link()); fireEvent.touchStart(link()); expect(link()).toHaveAttribute("data-prefetch", "null"); expect(onMouseEnter).toHaveBeenCalledTimes(2); expect(onFocus).toHaveBeenCalledTimes(1); expect(onTouchStart).toHaveBeenCalledTimes(1); }); it("path: leaving the link after reaching it -> prefetching is not switched off again", () => { render(Cases); fireEvent.mouseEnter(link()); fireEvent.mouseLeave(link()); fireEvent.blur(link()); expect(link()).toHaveAttribute("data-prefetch", "null"); }); it("path: events the link does not watch (click, key press) -> prefetching stays off", () => { const onClick = jest.fn(); render( Cases , ); fireEvent.click(link()); fireEvent.keyDown(link(), { key: "Enter" }); expect(onClick).toHaveBeenCalledTimes(1); expect(link()).toHaveAttribute("data-prefetch", "false"); }); it("path: other props -> reach the link as they were given", () => { render( Cases , ); const home = screen.getByRole("link", { name: "Evandro.dev.br — início" }); expect(home).toHaveAttribute("href", "/#home"); expect(home).toHaveClass("flex", "min-h-11"); }); });