import { render, screen } from "@testing-library/react"; import { compactHero, heroWithMediaGrid, } from "@/app/cases/_components/hero-styles"; import { caseStudies, getServicePage, type ServicePageContent, } from "@/lib/site-content"; import { ServiceHero } from "./ServiceHero"; import type { ServiceHeroData } from "./service-page"; /** * Testes de caminhos-base de ServiceHero (CC 6, contado à mão: as cinco escolhas do JSX): * hasHeroSide (classe do topo: compacto ou não), hasHeroSide (grade de duas colunas), hasCaseCards (botão "Ver projetos * parecidos"), diagram (esquema ou capa) e heroCase (a frase da capa). * Os dois filhos que desenham a coluna ao lado (esquema e capa) são trocados por dublês que mostram o que receberam. */ jest.mock("./ServiceDiagram", () => ({ ServiceDiagram: ({ kind }: { kind: string }) => (
{kind}
), })); jest.mock("./ServiceHeroCover", () => ({ ServiceHeroCover: (props: { caseStudy?: { id: string }; device?: string; relevance?: string; }) => (
), })); function service(slug = "criacao-de-sites"): ServicePageContent { const found = getServicePage(slug); if (!found) throw new Error(`Missing service ${slug}`); return found; } function caseStudy(id: string) { const found = caseStudies.find((item) => item.id === id); if (!found) throw new Error(`Missing case ${id}`); return found; } const noSide: ServiceHeroData = { heroCase: undefined, heroDevice: "desktop", diagram: undefined, hasHeroSide: false, hasCaseCards: false, }; const shell = (container: HTMLElement) => container.querySelector(".ds-section-shell") as HTMLElement; const section = (container: HTMLElement) => container.querySelector("section") as HTMLElement; const projectsLink = () => screen.queryByRole("link", { name: "Ver projetos parecidos" }); describe("ServiceHero", () => { it("path: no cover, no diagram and no card -> one low column, no 'Ver projetos parecidos', and an empty slot where the cover would be", () => { const { container } = render( , ); for (const token of compactHero.split(/\s+/).filter(Boolean)) expect(section(container)).toHaveClass(token); for (const token of heroWithMediaGrid.split(/\s+/).filter(Boolean)) expect(shell(container)).not.toHaveClass(token); expect(projectsLink()).toBeNull(); expect(screen.queryByTestId("diagram")).toBeNull(); // sem esquema, quem ocupa a coluna é a capa, que por conta própria não desenha nada sem case expect(screen.getByTestId("cover")).toHaveAttribute("data-case", ""); expect(screen.getByTestId("cover")).toHaveAttribute( "data-has-relevance", "false", ); }); it("path: a side column -> the compact class goes away and the two-column grid comes in", () => { const { container } = render( , ); for (const token of compactHero.split(/\s+/).filter(Boolean)) expect(section(container)).not.toHaveClass(token); for (const token of heroWithMediaGrid.split(/\s+/).filter(Boolean)) expect(shell(container)).toHaveClass(token); }); it("path: cards below -> 'Ver projetos parecidos' points at the related cases", () => { render( , ); expect(projectsLink()).toHaveAttribute("href", "#casos-relacionados"); }); it("path: a diagram -> the diagram fills the side column and the cover is not mounted", () => { render( , ); expect(screen.getByTestId("diagram")).toHaveTextContent("iot"); expect(screen.queryByTestId("cover")).toBeNull(); }); it("path: a cover case -> the cover gets the case, the device and the service's sentence about that case", () => { const lavacao = service(); render( , ); const cover = screen.getByTestId("cover"); expect(cover).toHaveAttribute("data-case", "lavacao-047"); expect(cover).toHaveAttribute("data-device", "desktop"); expect(cover).toHaveAttribute( "data-relevance", lavacao.caseRelevance["lavacao-047"], ); expect(screen.queryByTestId("diagram")).toBeNull(); }); it("path: a cover case the service has no sentence for -> the cover gets no sentence (it falls back on its own)", () => { // o serviço de esquema não tem frase para a SEV const withoutSentence = service("tauri-rust"); expect(withoutSentence.caseRelevance["sev-modaintima"]).toBeUndefined(); render( , ); const cover = screen.getByTestId("cover"); expect(cover).toHaveAttribute("data-case", "sev-modaintima"); expect(cover).toHaveAttribute("data-device", "mobile"); expect(cover).toHaveAttribute("data-has-relevance", "false"); }); it("path: the contact button speaks of the service by its whatsappSubject, in lower case, with the hero placement", () => { const sistemas = service("engenharia-de-software"); render(); const button = screen.getByRole("link", { name: /falar sobre meu projeto/i, }); const message = new URL(button.getAttribute("href") ?? "").searchParams.get( "text", ); expect(message).toContain( `Quero conversar sobre ${sistemas.whatsappSubject}.`, ); expect(sistemas.whatsappSubject).toMatch(/^[a-zà-ú]/); }); });