import { afterEach, describe, expect, it, vi } from "vitest"; import { fireEvent } from "@testing-library/react"; import "@testing-library/jest-dom/vitest"; import CreateFlowLayoutClient from "../../app/(app)/create/CreateFlowLayoutClient"; import { CreateFlowTextFieldScreen } from "../../app/(app)/create/screens/text/CreateFlowTextFieldScreen"; import { CREATE_FLOW_COMMUNITY_SAVE_FORM_ID } from "../../app/(app)/create/utils/createFlowPaths"; import { cleanup, renderWithProviders as render, screen, waitFor, } from "../utils/test-utils"; vi.mock("next/navigation", () => ({ useRouter: () => ({ push: vi.fn(), replace: vi.fn(), prefetch: vi.fn(), back: vi.fn(), }), usePathname: () => "/create/community-save", useSearchParams: () => new URLSearchParams(), })); const errorCopy = "Check your email address. We need it to send your link."; function renderSaveStep() { return render( , ); } describe("community-save email", () => { afterEach(() => { cleanup(); vi.unstubAllGlobals(); }); it("shows the designed error for an address the browser would reject", async () => { vi.stubGlobal( "fetch", vi.fn(async () => new Response(JSON.stringify({ user: null }), { status: 200, headers: { "Content-Type": "application/json" }, }), ), ); renderSaveStep(); const form = await waitFor(() => { const node = document.getElementById(CREATE_FLOW_COMMUNITY_SAVE_FORM_ID); expect(node).not.toBeNull(); return node as HTMLFormElement; }); expect(form).toHaveAttribute("novalidate"); const input = screen.getByRole("textbox", { name: "Email address" }); fireEvent.change(input, { target: { value: "not-an-email" } }); fireEvent.click(screen.getByRole("button", { name: "Submit email" })); expect(await screen.findByText(errorCopy)).toBeInTheDocument(); expect(screen.getByRole("button", { name: "Submit email" })).toHaveAttribute( "formnovalidate", ); }); it("shows the same error for an address with no dot in the domain", async () => { vi.stubGlobal( "fetch", vi.fn(async () => new Response(JSON.stringify({ user: null }), { status: 200, headers: { "Content-Type": "application/json" }, }), ), ); renderSaveStep(); const input = await screen.findByRole("textbox", { name: "Email address" }); fireEvent.change(input, { target: { value: "maya@example" } }); fireEvent.click(await screen.findByRole("button", { name: "Submit email" })); expect(await screen.findByText(errorCopy)).toBeInTheDocument(); }); });