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( Conversar , ); 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); }); });