Files
community-rule/tests/components/CreateFlowGuestFinish.test.tsx
adilallo 0535f6f167 Let anyone finish a Rule without an account or email.
Continue on final review and Finish without inviting no longer open a login wall. The header stays Exit until Save your progress is done, and Send invitations is the only invite-path login.
2026-11-23 14:48:01 -07:00

245 lines
7.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import userEvent from "@testing-library/user-event";
import "@testing-library/jest-dom/vitest";
import CreateFlowLayoutClient from "../../app/(app)/create/CreateFlowLayoutClient";
import { CREATE_FLOW_ANONYMOUS_KEY } from "../../app/(app)/create/utils/anonymousDraftStorage";
import {
CREATE_FLOW_SYNC_DRAFT_QUERY,
CREATE_FLOW_SYNC_DRAFT_VALUE,
} from "../../app/(app)/create/utils/createFlowPaths";
import {
fetchAuthSession,
publishRule,
requestMagicLink,
} from "../../lib/create/api";
import { CREATE_FLOW_PENDING_KEEP_RULE_LOGIN_KEY } from "../../lib/create/pendingKeepRuleLogin";
import type { CreateFlowState } from "../../app/(app)/create/types";
import {
cleanup,
renderWithProviders as render,
screen,
waitFor,
} from "../utils/test-utils";
const nav = vi.hoisted(() => ({
pathname: "/create/final-review",
push: vi.fn(),
replace: vi.fn(),
}));
vi.mock("next/navigation", () => ({
useRouter: () => ({
push: nav.push,
replace: nav.replace,
prefetch: vi.fn(),
back: vi.fn(),
}),
usePathname: () => nav.pathname,
useSearchParams: () => new URLSearchParams(),
}));
vi.mock("../../lib/create/api", async (importOriginal) => {
const actual = await importOriginal<typeof import("../../lib/create/api")>();
return {
...actual,
fetchAuthSession: vi.fn(async () => ({ user: null })),
publishRule: vi.fn(async () => ({
ok: true as const,
id: "guest-rule-1",
title: "Oak Park Commons",
})),
requestMagicLink: vi.fn(async () => ({ ok: true as const })),
};
});
const readyToContinue: CreateFlowState = {
title: "Oak Park Commons",
selectedCommunicationMethodIds: ["signal"],
selectedMembershipMethodIds: ["open-admission"],
selectedDecisionApproachIds: ["lazy-consensus"],
selectedConflictManagementIds: ["conflict-ladder"],
communicationMethodDetailsById: {
signal: {
corePrinciple: "We talk in the open.",
logisticsAdmin: "Post in Signal.",
codeOfConduct: "No pile-ons.",
},
},
membershipMethodDetailsById: {
"open-admission": {
eligibility: "Anyone who shares the work.",
joiningProcess: "Show up.",
expectations: "Keep the keys.",
},
},
decisionApproachDetailsById: {
"lazy-consensus": {
corePrinciple: "Silence is assent.",
applicableScope: [],
selectedApplicableScope: [],
stepByStepInstructions: "Post the proposal.",
objectionsDeadlocks: "A named block stops it.",
},
},
conflictManagementDetailsById: {
"conflict-ladder": {
corePrinciple: "Start small.",
applicableScope: [],
selectedApplicableScope: [],
processProtocol: "Talk, then mediate.",
restorationFallbacks: "A panel if that fails.",
},
},
};
function seedDraft(state: CreateFlowState = readyToContinue) {
window.localStorage.setItem(CREATE_FLOW_ANONYMOUS_KEY, JSON.stringify(state));
}
function renderFlow() {
return render(
<CreateFlowLayoutClient>
<div>step</div>
</CreateFlowLayoutClient>,
);
}
describe("guest finish without an account", () => {
beforeEach(() => {
nav.pathname = "/create/final-review";
nav.push.mockReset();
nav.replace.mockReset();
vi.mocked(fetchAuthSession).mockResolvedValue({ user: null });
vi.mocked(publishRule).mockResolvedValue({
ok: true,
id: "guest-rule-1",
title: "Oak Park Commons",
});
vi.mocked(requestMagicLink).mockResolvedValue({ ok: true });
window.localStorage.clear();
sessionStorage.clear();
});
afterEach(() => {
cleanup();
window.localStorage.clear();
sessionStorage.clear();
});
it("keeps Exit on final review and continues to Invite your members without login", async () => {
const user = userEvent.setup();
seedDraft();
renderFlow();
expect(
await screen.findByRole("button", { name: "Exit" }),
).toBeInTheDocument();
expect(
screen.queryByRole("button", { name: "Save and exit" }),
).not.toBeInTheDocument();
const continueButton = await screen.findByRole("button", {
name: "Continue",
});
await waitFor(() => {
expect(continueButton).toBeEnabled();
});
await user.click(continueButton);
expect(
screen.queryByRole("heading", {
name: "Log in to invite your members",
}),
).not.toBeInTheDocument();
expect(nav.push).toHaveBeenCalledWith("/create/confirm-stakeholders");
});
it("opens the invite login from Send invitations and does not publish", async () => {
const user = userEvent.setup();
nav.pathname = "/create/confirm-stakeholders";
seedDraft();
renderFlow();
const sendInvitations = await screen.findByRole("button", {
name: "Send invitations",
});
await waitFor(() => {
expect(sendInvitations).toBeEnabled();
});
await user.click(sendInvitations);
expect(
await screen.findByRole("heading", {
name: "Log in to invite your members",
}),
).toBeInTheDocument();
expect(publishRule).not.toHaveBeenCalled();
expect(nav.push).not.toHaveBeenCalled();
});
it("finishes without inviting and does not ask for an email", async () => {
const user = userEvent.setup();
nav.pathname = "/create/confirm-stakeholders";
seedDraft();
renderFlow();
const finishWithoutInviting = await screen.findByRole("button", {
name: "Finish without inviting",
});
await waitFor(() => {
expect(finishWithoutInviting).toBeEnabled();
});
await user.click(finishWithoutInviting);
await waitFor(() => {
expect(publishRule).toHaveBeenCalled();
});
expect(
screen.queryByRole("heading", {
name: "Log in to invite your members",
}),
).not.toBeInTheDocument();
expect(
screen.queryByRole("heading", {
name: "Log in to keep this rule on your profile",
}),
).not.toBeInTheDocument();
expect(
sessionStorage.getItem(CREATE_FLOW_PENDING_KEEP_RULE_LOGIN_KEY),
).toBeNull();
expect(nav.push).toHaveBeenCalledWith(
expect.stringMatching(/^\/create\/completed/),
);
});
it("asks the invite login to return to Invite your members after the link", async () => {
const user = userEvent.setup();
nav.pathname = "/create/confirm-stakeholders";
seedDraft();
renderFlow();
const sendInvitations = await screen.findByRole("button", {
name: "Send invitations",
});
await waitFor(() => {
expect(sendInvitations).toBeEnabled();
});
await user.click(sendInvitations);
expect(
await screen.findByRole("heading", {
name: "Log in to invite your members",
}),
).toBeInTheDocument();
const email = screen.getByRole("textbox", { name: /email address/i });
await user.type(email, "maya@example.org");
await user.click(screen.getByRole("button", { name: "Send login link" }));
expect(requestMagicLink).toHaveBeenCalledWith(
"maya@example.org",
`/create/confirm-stakeholders?${CREATE_FLOW_SYNC_DRAFT_QUERY}=${CREATE_FLOW_SYNC_DRAFT_VALUE}`,
expect.anything(),
);
});
});