105 lines
3.7 KiB
TypeScript
105 lines
3.7 KiB
TypeScript
import { render, screen } from "@testing-library/react";
|
|
import { notFound } from "next/navigation";
|
|
import { setNavigation } from "@/__mocks__/next/navigation";
|
|
import { getServicePage } from "@/lib/site-content";
|
|
import ServicePage, { generateMetadata } from "./page";
|
|
|
|
/**
|
|
* Testes de caminhos-base de ServicePage e generateMetadata (complemento de page.test.tsx).
|
|
* Cobrem só o que o arquivo principal não exercita: slug desconhecido e perguntas frequentes ausentes ou vazias.
|
|
*/
|
|
|
|
beforeEach(() => {
|
|
setNavigation({ pathname: "/servicos" });
|
|
});
|
|
|
|
beforeEach(() => {
|
|
jest.mocked(notFound).mockClear();
|
|
});
|
|
|
|
async function renderService(slug: string) {
|
|
const ui = await ServicePage({ params: Promise.resolve({ slug }) });
|
|
return render(ui);
|
|
}
|
|
|
|
const FAQ_HEADING = "Perguntas frequentes";
|
|
|
|
describe("ServicePage: caminhos que o teste principal não cobre", () => {
|
|
it("path: unknown slug -> calls notFound() and renders nothing", async () => {
|
|
await expect(
|
|
ServicePage({ params: Promise.resolve({ slug: "nao-existe" }) }),
|
|
).rejects.toThrow("NEXT_NOT_FOUND");
|
|
expect(notFound).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("path: service with a FAQ (main path) -> shows every question as a closed accordion item, the same pattern as the home", async () => {
|
|
const service = getServicePage("criacao-de-sites");
|
|
if (!service?.faq?.length) throw new Error("Missing service FAQ");
|
|
|
|
const { container } = await renderService(service.slug);
|
|
|
|
expect(
|
|
screen.getByRole("heading", { level: 2, name: FAQ_HEADING }),
|
|
).toBeVisible();
|
|
const summaries = Array.from(
|
|
container.querySelectorAll("#faq summary"),
|
|
).map((summary) => summary.textContent?.replace("+", "").trim());
|
|
expect(summaries).toEqual(service.faq.map(({ question }) => question));
|
|
for (const item of service.faq) {
|
|
const details = screen.getByText(item.question).closest("details");
|
|
expect(details).not.toBeNull();
|
|
expect(details?.open).toBe(false);
|
|
expect(details).toHaveTextContent(item.answer);
|
|
}
|
|
// um formato só: nenhum cartão de pergunta e resposta
|
|
expect(container.querySelector("#faq article")).toBeNull();
|
|
});
|
|
|
|
it("path: service without the faq field -> no FAQ section, the rest of the page stays", async () => {
|
|
const service = getServicePage("criacao-de-sites");
|
|
if (!service?.faq) throw new Error("Missing service FAQ");
|
|
const originalFaq = service.faq;
|
|
delete service.faq;
|
|
try {
|
|
await renderService(service.slug);
|
|
expect(screen.queryByRole("heading", { name: FAQ_HEADING })).toBeNull();
|
|
expect(
|
|
screen.getByRole("heading", {
|
|
level: 2,
|
|
name: "Conte o que você precisa resolver.",
|
|
}),
|
|
).toBeVisible();
|
|
} finally {
|
|
service.faq = originalFaq;
|
|
}
|
|
});
|
|
|
|
it("path: service with an empty FAQ list -> no FAQ section", async () => {
|
|
const service = getServicePage("criacao-de-sites");
|
|
if (!service?.faq) throw new Error("Missing service FAQ");
|
|
const originalFaq = service.faq;
|
|
service.faq = [];
|
|
try {
|
|
await renderService(service.slug);
|
|
expect(screen.queryByRole("heading", { name: FAQ_HEADING })).toBeNull();
|
|
expect(
|
|
screen.getByRole("heading", {
|
|
level: 2,
|
|
name: "Conte o que você precisa resolver.",
|
|
}),
|
|
).toBeVisible();
|
|
} finally {
|
|
service.faq = originalFaq;
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("generateMetadata: caminho do slug desconhecido", () => {
|
|
it("path: unknown slug -> only a 'not found' title, no canonical or social tags", async () => {
|
|
const metadata = await generateMetadata({
|
|
params: Promise.resolve({ slug: "nao-existe" }),
|
|
});
|
|
expect(metadata).toEqual({ title: "Serviço não encontrado" });
|
|
});
|
|
});
|