import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import Contact from "./Contact"; beforeEach(() => { jest.replaceProperty(process.env, "NODE_ENV", "production"); localStorage.setItem("evandro.analytics-consent.v1", "granted"); delete window["ga-disable-G-QTT9QT0T3S"]; window.gtag = jest.fn(); jest.spyOn(window, "open").mockImplementation(() => null); }); afterEach(() => { jest.restoreAllMocks(); localStorage.clear(); delete window.gtag; }); it("prepares an isolated WhatsApp draft with the entered project details only after submission", async () => { const user = userEvent.setup(); render(); const name = "Ana & João"; const email = "ana+projeto@example.com"; const context = "Integrar pedidos & estoque? Objetivo: evitar retrabalho.\nPrazo: outubro #2026"; await user.type(screen.getByRole("textbox", { name: /nome/i }), name); await user.type(screen.getByRole("textbox", { name: /e-mail/i }), email); const subject = screen.getByRole("combobox", { name: /tipo de projeto/i }); await user.selectOptions(subject, "Integrações e automações"); await user.type(screen.getByRole("textbox", { name: /contexto/i }), context); expect(window.open).not.toHaveBeenCalled(); expect(window.gtag).not.toHaveBeenCalled(); await user.click(screen.getByRole("button", { name: /whatsapp/i })); expect(window.open).toHaveBeenCalledTimes(1); const [destination, target, features] = jest.mocked(window.open).mock .calls[0]; const url = new URL(String(destination)); expect(url.origin).toBe("https://wa.me"); expect(url.pathname).toMatch(/^\/\d+$/); expect([...url.searchParams.keys()]).toEqual(["text"]); const message = url.searchParams.get("text"); expect(message).toContain(name); expect(message).toContain(email); expect(message).toContain("Integrações e automações"); expect(message).toContain(context); expect(target).toBe("_blank"); expect(features).toBe("noopener,noreferrer"); expect(window.gtag).toHaveBeenCalledTimes(1); expect(window.gtag).toHaveBeenCalledWith("event", "whatsapp_open", { send_to: "G-QTT9QT0T3S", service: "Contato", placement: "contact_form", page_path: "/", page_location: "https://evandro.dev.br/", page_referrer: "", }); }); it("does not open WhatsApp while required fields are empty or the email is invalid", async () => { const user = userEvent.setup(); render(); const submit = screen.getByRole("button", { name: /whatsapp/i }); await user.click(submit); expect(window.open).not.toHaveBeenCalled(); await user.type(screen.getByRole("textbox", { name: /nome/i }), "Ana"); await user.type( screen.getByRole("textbox", { name: /e-mail/i }), "email-invalido", ); await user.type( screen.getByRole("textbox", { name: /contexto/i }), "Organizar pedidos", ); await user.click(submit); expect(window.open).not.toHaveBeenCalled(); expect(window.gtag).not.toHaveBeenCalled(); }); it("opens the project draft with analytics unavailable and consent denied", async () => { localStorage.setItem("evandro.analytics-consent.v1", "denied"); delete window.gtag; const user = userEvent.setup(); render(); await user.type(screen.getByRole("textbox", { name: /nome/i }), "Ana"); await user.type( screen.getByRole("textbox", { name: /e-mail/i }), "ana@example.com", ); await user.type( screen.getByRole("textbox", { name: /contexto/i }), "Organizar pedidos", ); await user.click(screen.getByRole("button", { name: /whatsapp/i })); expect(window.open).toHaveBeenCalledTimes(1); const destination = new URL( String(jest.mocked(window.open).mock.calls[0][0]), ); expect(destination.origin).toBe("https://wa.me"); expect(destination.searchParams.get("text")).toContain("Organizar pedidos"); });