Files
portifolio-evandrodevbr/app/components/WhatsAppLink.onclick.paths.test.tsx
Evandro Fonseca Junior 10f170abf4 feat(home): posicionamento com prova, menu sem recarregar e WhatsApp em todo o site
H1 'Sites e sistemas que trazem pedidos pelo WhatsApp e tiram sua operação da planilha.'; HeroWork maior com a SEV; menu Cases, Serviços, Como trabalho, Sobre e IA com next/link e pré-carregamento no hover; menu do celular em tela cheia; WhatsApp flutuante e no rodapé com a mensagem da rota; rodapé em todas as páginas; FAQ da home; /ia como oferta de revisão de app feito com IA; formulário com noscript, validação do mailto e plano B do pop-up; cartões sem foco indevido; sem a fonte Monument.
2026-10-14 14:15:25 -03:00

80 lines
2.4 KiB
TypeScript

import { fireEvent, render, screen } from "@testing-library/react";
import WhatsAppLink from "./WhatsAppLink";
/**
* Testes de caminhos-base do onClick de WhatsAppLink (complemento de WhatsAppLink.paths.test.tsx):
* o aviso ao dono do link (por exemplo, para fechar o menu do celular) corre junto com a abertura,
* qualquer que seja o botão do clique, e não corre no auxclick. Sem onClick, o clique funciona do mesmo jeito.
*/
beforeEach(() => {
jest.replaceProperty(process.env, "NODE_ENV", "production");
localStorage.setItem("evandro.analytics-consent.v1", "granted");
window.gtag = jest.fn();
});
afterEach(() => {
jest.restoreAllMocks();
localStorage.clear();
delete window.gtag;
});
function renderLink(onClick?: () => void) {
render(
<WhatsAppLink placement="header_menu" onClick={onClick}>
Conversar
</WhatsAppLink>,
);
return screen.getByRole("link", { name: "Conversar" });
}
describe("WhatsAppLink: onClick", () => {
it("path: primary click with onClick -> the callback runs once and the handoff is measured", () => {
const onClick = jest.fn();
const link = renderLink(onClick);
fireEvent.click(link, { button: 0 });
expect(onClick).toHaveBeenCalledTimes(1);
expect(window.gtag).toHaveBeenCalledWith(
"event",
"whatsapp_open",
expect.objectContaining({ placement: "header_menu" }),
);
});
it("path: click event from the middle button (some browsers send one; React drops the secondary button) and onClick -> the callback still runs, but nothing is measured", () => {
const onClick = jest.fn();
const link = renderLink(onClick);
fireEvent.click(link, { button: 1 });
expect(onClick).toHaveBeenCalledTimes(1);
expect(window.gtag).not.toHaveBeenCalled();
});
it("path: no onClick -> the primary click is measured and nothing breaks", () => {
const link = renderLink();
expect(() => fireEvent.click(link, { button: 0 })).not.toThrow();
expect(window.gtag).toHaveBeenCalledTimes(1);
});
it("path: middle-button auxclick with onClick -> measured, but the callback does not run", () => {
const onClick = jest.fn();
const link = renderLink(onClick);
link.dispatchEvent(
new MouseEvent("auxclick", {
button: 1,
bubbles: true,
cancelable: true,
}),
);
expect(onClick).not.toHaveBeenCalled();
expect(window.gtag).toHaveBeenCalledTimes(1);
});
});