148 lines
4.6 KiB
TypeScript
148 lines
4.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
import { screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import "@testing-library/jest-dom/vitest";
|
|
import { renderWithProviders } from "../utils/test-utils";
|
|
import AskOrganizerInquiryModal from "../../app/components/modals/AskOrganizerInquiry";
|
|
|
|
describe("AskOrganizerInquiryModal", () => {
|
|
const fetchMock = vi.fn();
|
|
|
|
beforeEach(() => {
|
|
fetchMock.mockReset();
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("opens as a phone sheet and sends the question into the sent state", async () => {
|
|
const user = userEvent.setup();
|
|
fetchMock.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ ok: true }),
|
|
});
|
|
renderWithProviders(
|
|
<AskOrganizerInquiryModal isOpen onClose={vi.fn()} />,
|
|
);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByRole("dialog")).toHaveClass("h-full", "max-h-full");
|
|
});
|
|
expect(
|
|
screen.getByRole("heading", { name: "Ask an organizer" }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("button", { name: /send question/i }),
|
|
).toBeInTheDocument();
|
|
|
|
await user.type(
|
|
screen.getByRole("textbox", { name: /email address/i }),
|
|
"rosa@example.org",
|
|
);
|
|
await user.type(
|
|
screen.getByRole("textbox", { name: /your question/i }),
|
|
"How do we decide who holds the keys to our shared accounts?",
|
|
);
|
|
await user.click(screen.getByRole("button", { name: /send question/i }));
|
|
|
|
expect(
|
|
await screen.findByRole("heading", { name: "Thanks" }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText(/one of us will write back within two working days/i),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getAllByRole("button", { name: /^close$/i }).length,
|
|
).toBeGreaterThan(1);
|
|
});
|
|
|
|
it("keeps the question and shows the danger toast when send fails", async () => {
|
|
const user = userEvent.setup();
|
|
fetchMock.mockRejectedValue(new Error("network"));
|
|
renderWithProviders(
|
|
<AskOrganizerInquiryModal isOpen onClose={vi.fn()} />,
|
|
);
|
|
|
|
await user.type(
|
|
screen.getByRole("textbox", { name: /email address/i }),
|
|
"rosa@example.org",
|
|
);
|
|
await user.type(
|
|
screen.getByRole("textbox", { name: /your question/i }),
|
|
"How do we decide who holds the keys to our shared accounts?",
|
|
);
|
|
await user.click(screen.getByRole("button", { name: /send question/i }));
|
|
|
|
await waitFor(() => {
|
|
expect(
|
|
screen.getAllByText("We could not send that.").length,
|
|
).toBeGreaterThan(0);
|
|
});
|
|
expect(
|
|
screen.getAllByText(/your question is still here/i).length,
|
|
).toBeGreaterThan(0);
|
|
expect(
|
|
screen.getByRole("textbox", { name: /email address/i }),
|
|
).toHaveValue("rosa@example.org");
|
|
expect(
|
|
screen.getByRole("textbox", { name: /your question/i }),
|
|
).toHaveValue(
|
|
"How do we decide who holds the keys to our shared accounts?",
|
|
);
|
|
expect(
|
|
screen.getByRole("button", { name: /send question/i }),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it("shows field hints for an invalid email and a short question, then clears them on edit", async () => {
|
|
const user = userEvent.setup();
|
|
renderWithProviders(
|
|
<AskOrganizerInquiryModal isOpen onClose={vi.fn()} />,
|
|
);
|
|
|
|
await user.type(
|
|
screen.getByRole("textbox", { name: /email address/i }),
|
|
"not-an-email",
|
|
);
|
|
await user.type(
|
|
screen.getByRole("textbox", { name: /your question/i }),
|
|
"Too short",
|
|
);
|
|
await user.click(screen.getByRole("button", { name: /send question/i }));
|
|
|
|
expect(
|
|
await screen.findByText("Enter an email address like name@example.org."),
|
|
).toBeInTheDocument();
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
|
|
await user.type(
|
|
screen.getByRole("textbox", { name: /email address/i }),
|
|
"@example.org",
|
|
);
|
|
expect(
|
|
screen.queryByText("Enter an email address like name@example.org."),
|
|
).not.toBeInTheDocument();
|
|
|
|
await user.clear(screen.getByRole("textbox", { name: /email address/i }));
|
|
await user.type(
|
|
screen.getByRole("textbox", { name: /email address/i }),
|
|
"rosa@example.org",
|
|
);
|
|
await user.click(screen.getByRole("button", { name: /send question/i }));
|
|
|
|
expect(
|
|
await screen.findByText("Please enter at least 10 characters."),
|
|
).toBeInTheDocument();
|
|
|
|
await user.type(
|
|
screen.getByRole("textbox", { name: /your question/i }),
|
|
" more",
|
|
);
|
|
expect(
|
|
screen.queryByText("Please enter at least 10 characters."),
|
|
).not.toBeInTheDocument();
|
|
});
|
|
});
|