318 lines
9.6 KiB
TypeScript
318 lines
9.6 KiB
TypeScript
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { within } from "@testing-library/react";
|
|
import {
|
|
cleanup,
|
|
renderWithProviders as render,
|
|
screen,
|
|
} from "../utils/test-utils";
|
|
import {
|
|
ProfilePageView,
|
|
type ProfilePageViewProps,
|
|
} from "../../app/(app)/profile/_components/ProfilePage.view";
|
|
import messages from "../../messages/en/index";
|
|
|
|
const copy = messages.pages.profile;
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
});
|
|
|
|
function viewProps(
|
|
overrides: Partial<ProfilePageViewProps> = {},
|
|
): ProfilePageViewProps {
|
|
return {
|
|
userEmail: "dani@example.com",
|
|
ruleCardSize: "M",
|
|
profileLgUp: true,
|
|
profileListSize: "l",
|
|
rules: [],
|
|
rulesError: false,
|
|
draft: null,
|
|
showDraftCard: false,
|
|
ruleDeleteOpen: false,
|
|
ruleDeleteBusy: false,
|
|
draftDeleteOpen: false,
|
|
draftDeleteBusy: false,
|
|
accountDeleteOpen: false,
|
|
accountDeleteBusy: false,
|
|
actionError: null,
|
|
profileSuccessMessage: null,
|
|
emailChangeOpen: false,
|
|
emailChangeValue: "",
|
|
onEmailChangeValueChange: vi.fn(),
|
|
emailChangeBusy: false,
|
|
emailChangeRequestSent: false,
|
|
emailChangeModalError: null,
|
|
onDismissEmailChangeModalError: vi.fn(),
|
|
onOpenEmailChange: vi.fn(),
|
|
onCloseEmailChange: vi.fn(),
|
|
onSubmitEmailChange: vi.fn(),
|
|
onSignOut: vi.fn(),
|
|
onDeleteRule: vi.fn(),
|
|
onCloseDeleteRule: vi.fn(),
|
|
onConfirmDeleteRule: vi.fn(),
|
|
onDuplicateRule: vi.fn(),
|
|
onInviteMembers: vi.fn(),
|
|
onContinueDraft: vi.fn(),
|
|
onDeleteDraft: vi.fn(),
|
|
onCloseDeleteDraft: vi.fn(),
|
|
onConfirmDeleteDraft: vi.fn(),
|
|
onOpenDeleteAccount: vi.fn(),
|
|
onCloseDeleteAccount: vi.fn(),
|
|
onConfirmDeleteAccount: vi.fn(),
|
|
onExportRulePdf: vi.fn(),
|
|
onExportRuleMarkdown: vi.fn(),
|
|
onDismissProfileSuccess: vi.fn(),
|
|
onDismissActionError: vi.fn(),
|
|
onDismissRulesError: vi.fn(),
|
|
onStartNewCustomRule: vi.fn(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("ProfilePageView", () => {
|
|
test("puts delete account in the options list as a danger row", () => {
|
|
render(<ProfilePageView {...viewProps()} />);
|
|
|
|
const options = screen.getByRole("navigation", {
|
|
name: copy.yourOptionsHeading,
|
|
});
|
|
expect(
|
|
within(options).getByRole("button", { name: copy.optionCreateCustom }),
|
|
).toBeInTheDocument();
|
|
expect(screen.queryByText(copy.deleteAccountIntro)).not.toBeInTheDocument();
|
|
expect(
|
|
screen.queryByRole("region", { name: copy.deleteAccount }),
|
|
).not.toBeInTheDocument();
|
|
|
|
const deleteAccount = within(options).getByRole("button", {
|
|
name: copy.deleteAccount,
|
|
});
|
|
const deleteTitle = within(deleteAccount).getByText(copy.deleteAccount);
|
|
expect(deleteTitle.className).toContain(
|
|
"!text-[var(--color-content-default-negative-primary)]",
|
|
);
|
|
expect(deleteAccount.firstElementChild?.className).not.toContain(
|
|
"negative-primary",
|
|
);
|
|
expect(deleteAccount.lastElementChild?.className).not.toContain(
|
|
"negative-primary",
|
|
);
|
|
});
|
|
|
|
test("shows the No Rules card when the person has no owned Rule", async () => {
|
|
const user = userEvent.setup();
|
|
const onStartNewCustomRule = vi.fn();
|
|
render(
|
|
<ProfilePageView {...viewProps({ onStartNewCustomRule })} />,
|
|
);
|
|
|
|
expect(
|
|
screen.getByRole("heading", {
|
|
name: copy.welcomeTitleReturn.replace("{{name}}", "dani@example.com"),
|
|
}),
|
|
).toBeInTheDocument();
|
|
expect(screen.queryByText(copy.welcomeBodyFirstRule)).not.toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("heading", { name: copy.emptyCardTitle }),
|
|
).toBeInTheDocument();
|
|
expect(screen.getByText(copy.emptyCardBody)).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("link", { name: copy.emptyCardBrowse }),
|
|
).toHaveAttribute("href", "/templates?fromFlow=1");
|
|
|
|
await user.click(
|
|
screen.getByRole("button", { name: copy.emptyCardCreate }),
|
|
);
|
|
expect(onStartNewCustomRule).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("asks for confirmation before deleting the account", async () => {
|
|
const user = userEvent.setup();
|
|
const onOpenDeleteAccount = vi.fn();
|
|
const onConfirmDeleteAccount = vi.fn();
|
|
const { rerender } = render(
|
|
<ProfilePageView {...viewProps({ onOpenDeleteAccount })} />,
|
|
);
|
|
|
|
await user.click(
|
|
screen.getByRole("button", { name: copy.deleteAccount }),
|
|
);
|
|
expect(onOpenDeleteAccount).toHaveBeenCalledTimes(1);
|
|
|
|
rerender(
|
|
<ProfilePageView
|
|
{...viewProps({
|
|
onOpenDeleteAccount,
|
|
onConfirmDeleteAccount,
|
|
accountDeleteOpen: true,
|
|
})}
|
|
/>,
|
|
);
|
|
|
|
const dialog = screen.getByRole("dialog", {
|
|
name: messages.modals.deleteAccount.title,
|
|
});
|
|
expect(dialog).toHaveTextContent(messages.modals.deleteAccount.description);
|
|
const confirm = within(dialog).getByRole("button", {
|
|
name: messages.modals.deleteAccount.confirm,
|
|
});
|
|
expect(confirm).toBeDisabled();
|
|
expect(confirm.className).toContain(
|
|
"border-[var(--color-border-default-negative-primary)]",
|
|
);
|
|
expect(confirm.className).not.toContain(
|
|
"bg-[var(--color-surface-invert-primary)]",
|
|
);
|
|
|
|
await user.click(
|
|
within(dialog).getByRole("checkbox", {
|
|
name: messages.modals.deleteAccount.confirmLabel,
|
|
}),
|
|
);
|
|
expect(confirm).toBeEnabled();
|
|
await user.click(confirm);
|
|
expect(onConfirmDeleteAccount).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("finished owner cards offer Invite your members beside Duplicate and Delete", async () => {
|
|
const user = userEvent.setup();
|
|
const onInviteMembers = vi.fn();
|
|
render(
|
|
<ProfilePageView
|
|
{...viewProps({
|
|
onInviteMembers,
|
|
rules: [
|
|
{
|
|
id: "rule-1",
|
|
title: "Garden coop",
|
|
summary: "How we share the plots.",
|
|
createdAt: "2026-11-01T00:00:00.000Z",
|
|
updatedAt: "2026-11-01T00:00:00.000Z",
|
|
role: "owner",
|
|
},
|
|
],
|
|
})}
|
|
/>,
|
|
);
|
|
|
|
expect(
|
|
screen.getByText(copy.welcomeBodyFirstRule),
|
|
).toBeInTheDocument();
|
|
expect(screen.getByText(copy.finishedBadge)).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("heading", { name: copy.yourRulesHeading }),
|
|
).toBeInTheDocument();
|
|
|
|
await user.click(
|
|
screen.getByRole("button", { name: copy.inviteMembers }),
|
|
);
|
|
expect(onInviteMembers).toHaveBeenCalledWith("rule-1");
|
|
expect(
|
|
screen.getByRole("button", { name: copy.duplicate }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("button", { name: copy.deleteRule }),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
test("lists invited Rules under Rules you belong to, not Your Rules", () => {
|
|
render(
|
|
<ProfilePageView
|
|
{...viewProps({
|
|
rules: [
|
|
{
|
|
id: "rule-owned",
|
|
title: "Garden coop",
|
|
summary: "How we share the plots.",
|
|
createdAt: "2026-11-01T00:00:00.000Z",
|
|
updatedAt: "2026-11-01T00:00:00.000Z",
|
|
role: "owner",
|
|
},
|
|
{
|
|
id: "rule-invited",
|
|
title: "Hawthorn Row Seed Library",
|
|
summary: "Neighbors save seeds each fall.",
|
|
createdAt: "2026-10-01T00:00:00.000Z",
|
|
updatedAt: "2026-10-01T00:00:00.000Z",
|
|
role: "stakeholder",
|
|
},
|
|
],
|
|
})}
|
|
/>,
|
|
);
|
|
|
|
expect(
|
|
screen.getByRole("heading", { name: copy.rulesYouBelongToHeading }),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("link", { name: /Hawthorn Row Seed Library/ }),
|
|
).toHaveAttribute("href", "/invite/rule-invited");
|
|
expect(
|
|
screen.queryByRole("button", { name: copy.viewPublic }),
|
|
).not.toBeInTheDocument();
|
|
|
|
const yourRules = screen.getByRole("region", {
|
|
name: copy.yourRulesHeading,
|
|
});
|
|
expect(within(yourRules).getByText("Garden coop")).toBeInTheDocument();
|
|
expect(
|
|
within(yourRules).queryByText("Hawthorn Row Seed Library"),
|
|
).not.toBeInTheDocument();
|
|
expect(
|
|
within(yourRules).getByRole("button", { name: copy.inviteMembers }),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
test("hides Rules you belong to when nothing was shared with this address", () => {
|
|
render(
|
|
<ProfilePageView
|
|
{...viewProps({
|
|
rules: [
|
|
{
|
|
id: "rule-1",
|
|
title: "Garden coop",
|
|
summary: "How we share the plots.",
|
|
createdAt: "2026-11-01T00:00:00.000Z",
|
|
updatedAt: "2026-11-01T00:00:00.000Z",
|
|
role: "owner",
|
|
},
|
|
],
|
|
})}
|
|
/>,
|
|
);
|
|
|
|
expect(
|
|
screen.queryByRole("heading", { name: copy.rulesYouBelongToHeading }),
|
|
).not.toBeInTheDocument();
|
|
});
|
|
|
|
test("uses the return-visit greeting when a draft is in progress", () => {
|
|
render(
|
|
<ProfilePageView
|
|
{...viewProps({
|
|
showDraftCard: true,
|
|
draft: {
|
|
hasDraft: true,
|
|
updatedAt: "2026-11-01T00:00:00.000Z",
|
|
state: { title: "Alder Court Tenants Union" },
|
|
} as ProfilePageViewProps["draft"],
|
|
})}
|
|
/>,
|
|
);
|
|
|
|
expect(
|
|
screen.getByRole("heading", {
|
|
name: copy.welcomeTitleReturn.replace("{{name}}", "dani@example.com"),
|
|
}),
|
|
).toBeInTheDocument();
|
|
expect(screen.getByText(copy.welcomeBodyReturn)).toBeInTheDocument();
|
|
expect(screen.getByText("Alder Court Tenants Union")).toBeInTheDocument();
|
|
expect(screen.getByText(copy.draftInProgressBadge)).toBeInTheDocument();
|
|
expect(
|
|
screen.getByRole("button", { name: copy.continueDraft }),
|
|
).toBeInTheDocument();
|
|
});
|
|
});
|