Files
community-rule/tests/components/CommunitySaveEmail.test.tsx
T
adilallo 7336cba6f5 Show our own save-email error, and open review cards as the deck row view.
A bad address was stopped by the browser bubble before the designed line could appear. Final review opened every part of a method card at once instead of the three-row policy the decks use.
2026-11-11 10:47:40 -07:00

93 lines
2.9 KiB
TypeScript

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(
<CreateFlowLayoutClient>
<CreateFlowTextFieldScreen
messageNamespace="create.community.communitySave"
stateField="communitySaveEmail"
maxLength={254}
inputType="email"
showCharacterCount={false}
/>
</CreateFlowLayoutClient>,
);
}
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();
});
});