import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { PROBLEMAS, problemaPorId } from "@/lib/jogo/problemas";
import type { Resultado } from "./contrato";
import Relatorio from "./Relatorio";
const [a, b, c] = PROBLEMAS;
const base: Resultado = {
vitoria: true,
problemaId: c.id,
dificuldade: "facil",
resolvidos: [a.id, b.id, c.id],
placar: { jogador: 9, drones: 5 },
drones: [],
segundos: 300,
};
describe("Relatorio", () => {
it("na vitória mostra o título, o placar e foca o título", () => {
render();
const titulo = screen.getByRole("heading", {
level: 1,
name: "Problemas resolvidos",
});
expect(titulo).toHaveFocus();
expect(screen.getByText("Você 9 · Drones 5")).toBeInTheDocument();
});
it("na derrota mostra o outro título", () => {
render(
,
);
expect(
screen.getByRole("heading", {
level: 1,
name: "Os problemas venceram desta vez",
}),
).toBeInTheDocument();
});
it("cada cartão aponta para o destino do problema", () => {
render();
const links = screen.getAllByRole("link", {
name: /Ver como eu resolvo isso/,
});
expect(links).toHaveLength(3);
for (const [i, id] of base.resolvidos.entries()) {
expect(links[i]).toHaveAttribute("href", problemaPorId(id)?.destino);
}
});
it("o WhatsApp cita o problema escolhido, mesmo sem ter sido resolvido", () => {
render(
,
);
const link = screen.getByRole("link", { name: "Chamar no WhatsApp" });
const href = link.getAttribute("href") ?? "";
expect(href).toMatch(/^https:\/\/wa\.me\/\d+\?text=/);
expect(href).toContain(encodeURIComponent(c.nome));
expect(
screen.getAllByRole("link", { name: /Ver como eu resolvo isso/ }),
).toHaveLength(1);
});
it("Jogar de novo chama a ação", async () => {
const aoJogarDeNovo = jest.fn();
render();
await userEvent.click(
screen.getByRole("button", { name: "Jogar de novo" }),
);
expect(aoJogarDeNovo).toHaveBeenCalledTimes(1);
});
});