import { render, screen } from "@testing-library/react"; import { getShowcaseAssets } from "@/lib/project-showcase"; import { CaseCover, coverSizes } from "./CaseCover"; import { getCaseMedia } from "./case-media"; /** * Testes de caminhos-base de CaseCover: sem captura (nada), com captura (moldura + pôster) * e os valores padrão de `sizes` e `priority` (prioridade = carga imediata e `fetchpriority="high"`). */ jest.mock("./case-media", () => ({ getCaseMedia: jest.fn() })); const mockedGetCaseMedia = jest.mocked(getCaseMedia); const props = { id: "sev-modaintima", title: "SEV Moda Íntima", demoUrl: "https://sevmodaintima.com.br", }; afterEach(() => { mockedGetCaseMedia.mockReset(); }); describe("CaseCover", () => { it("path: no desktop capture -> renders nothing", () => { mockedGetCaseMedia.mockReturnValue(undefined); const { container } = render(); expect(container).toBeEmptyDOMElement(); expect(mockedGetCaseMedia).toHaveBeenCalledWith(props.id, "desktop"); }); it("path: capture found -> poster inside the browser frame, with the real domain in the bar", () => { mockedGetCaseMedia.mockImplementation(getShowcaseAssets); const { container } = render(); const image = screen.getByAltText(`Captura de tela do site ${props.title}`); expect(container.querySelector(".ds-bframe")).toContainElement(image); expect(image.getAttribute("src")).toContain( encodeURIComponent(`/videos/${props.id}/desktop.webp`), ); expect(container.querySelector(".ds-bframe-url")).toHaveTextContent( "sevmodaintima.com.br", ); }); it("path: sizes and priority omitted -> default sizes and lazy loading", () => { mockedGetCaseMedia.mockImplementation(getShowcaseAssets); render(); const image = screen.getByAltText(`Captura de tela do site ${props.title}`); expect(image).toHaveAttribute("sizes", coverSizes.card); expect(image).toHaveAttribute("loading", "lazy"); expect(image).not.toHaveAttribute("fetchpriority"); }); it("path: sizes and priority given -> uses them, with eager loading and high fetch priority", () => { mockedGetCaseMedia.mockImplementation(getShowcaseAssets); render(); const image = screen.getByAltText(`Captura de tela do site ${props.title}`); expect(image).toHaveAttribute("sizes", "320px"); expect(image).toHaveAttribute("loading", "eager"); expect(image).toHaveAttribute("fetchpriority", "high"); }); it("path: no 100vw sizes -> every preset describes the width the cover really has", () => { // 100vw baixava a imagem de 750 px para uma capa de 300 px no celular for (const sizes of Object.values(coverSizes)) { expect(sizes).not.toMatch(/(^|, )100vw$/); expect(sizes).toMatch(/calc\(100vw - \d+px\)$/); } }); });