Open a finished Rule from its Profile card, and drop it from the browser on Log out.

This commit is contained in:
adilallo committed 2026-11-22 17:47:46 -07:00
1 parent db1599123b
commit 942c7cc9de
17 files changed
+280 -14

No files matched your search

+63 -1
View File
@@ -1,6 +1,10 @@
import { useSearchParams } from "next/navigation";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { renderWithProviders as render, screen } from "../utils/test-utils";
import {
renderWithProviders as render,
screen,
waitFor,
} from "../utils/test-utils";
import "@testing-library/jest-dom/vitest";
import { CompletedScreen } from "../../app/(app)/create/screens/completed/CompletedScreen";
import { CREATE_FLOW_LAST_PUBLISHED_KEY } from "../../lib/create/lastPublishedRule";
@@ -8,6 +12,14 @@ import {
CREATE_FLOW_COMPLETED_CELEBRATE_QUERY,
CREATE_FLOW_COMPLETED_CELEBRATE_VALUE,
} from "../../app/(app)/create/utils/flowSteps";
import { testRouter } from "../mocks/navigation";
const fetchPublishedRuleDetail = vi.hoisted(() => vi.fn());
vi.mock("../../lib/create/api", () => ({
fetchPublishedRuleDetail: (...args: unknown[]) =>
fetchPublishedRuleDetail(...args),
}));
const storedRuleFixture = {
id: "rule-fixture-1",
@@ -48,6 +60,8 @@ function mockSearchParams(record?: Record<string, string>) {
describe("CompletedScreen", () => {
beforeEach(() => {
sessionStorage.removeItem(CREATE_FLOW_LAST_PUBLISHED_KEY);
fetchPublishedRuleDetail.mockReset();
testRouter.replace.mockReset();
mockSearchParams();
});
@@ -165,4 +179,52 @@ describe("CompletedScreen", () => {
),
).toBe(true);
});
it("loads an owned Rule from ?ruleId= and stores it", async () => {
fetchPublishedRuleDetail.mockResolvedValue({
viewerIsOwner: true,
rule: {
id: "rule-owned",
title: "QA Tenants Union",
summary: "How we share the building.",
document: storedRuleFixture.document,
createdAt: "2026-11-01T00:00:00.000Z",
},
});
mockSearchParams({ ruleId: "rule-owned" });
render(<CompletedScreen />);
expect(
await screen.findByRole("heading", { name: "QA Tenants Union" }),
).toBeInTheDocument();
await waitFor(() => {
expect(testRouter.replace).toHaveBeenCalledWith("/create/completed");
});
const stored = JSON.parse(
sessionStorage.getItem(CREATE_FLOW_LAST_PUBLISHED_KEY) ?? "null",
);
expect(stored).toMatchObject({
id: "rule-owned",
title: "QA Tenants Union",
});
});
it("sends a non-owner ?ruleId= load to the reader page", async () => {
fetchPublishedRuleDetail.mockResolvedValue({
viewerIsOwner: false,
rule: {
id: "rule-other",
title: "Someone else's Rule",
summary: null,
document: storedRuleFixture.document,
},
});
mockSearchParams({ ruleId: "rule-other" });
render(<CompletedScreen />);
await waitFor(() => {
expect(testRouter.replace).toHaveBeenCalledWith("/rules/rule-other");
});
expect(sessionStorage.getItem(CREATE_FLOW_LAST_PUBLISHED_KEY)).toBeNull();
});
});
+5 -2
View File
@@ -242,6 +242,9 @@ describe("ProfilePageView", () => {
expect(
screen.getByRole("button", { name: copy.deleteRule }),
).toBeInTheDocument();
expect(
screen.getByRole("link", { name: "Garden coop" }),
).toHaveAttribute("href", "/create/completed?ruleId=rule-1");
});
test("lists invited Rules under Rules you belong to, not Your Rules", () => {
@@ -274,8 +277,8 @@ describe("ProfilePageView", () => {
screen.getByRole("heading", { name: copy.rulesYouBelongToHeading }),
).toBeInTheDocument();
expect(
screen.getByRole("link", { name: /Hawthorn Row Seed Library/ }),
).toHaveAttribute("href", "/invite/rule-invited");
screen.getByRole("link", { name: "Hawthorn Row Seed Library" }),
).toHaveAttribute("href", "/rules/rule-invited");
expect(
screen.queryByRole("button", { name: copy.viewPublic }),
).not.toBeInTheDocument();
+19
View File
@@ -94,6 +94,25 @@ describe("Rule Component", () => {
expect(handleClick).toHaveBeenCalledTimes(1);
});
it("keeps bottom links and names the overlay link with ariaLabel", () => {
const onInvite = vi.fn();
render(
<Rule
{...defaultProps}
href="/create/completed?ruleId=rule-1"
ariaLabel="Garden coop"
hasBottomLinks
bottomLinks={[{ id: "invite", label: "Invite your members", onClick: onInvite }]}
/>,
);
expect(
screen.getByRole("link", { name: "Garden coop" }),
).toHaveAttribute("href", "/create/completed?ruleId=rule-1");
fireEvent.click(screen.getByRole("button", { name: "Invite your members" }));
expect(onInvite).toHaveBeenCalledTimes(1);
});
it("applies hover effects correctly", () => {
render(<Rule {...defaultProps} />);
+55
View File
@@ -0,0 +1,55 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { CREATE_FLOW_ANONYMOUS_KEY } from "../../app/(app)/create/utils/anonymousDraftStorage";
import { clearBrowserRuleData } from "../../app/(app)/create/utils/clearBrowserRuleData";
import { CORE_VALUE_DETAILS_STORAGE_KEY } from "../../app/(app)/create/utils/coreValueDetailsLocalStorage";
import { CREATE_FLOW_LAST_PUBLISHED_KEY } from "../../lib/create/lastPublishedRule";
import { CREATE_FLOW_PENDING_KEEP_RULE_LOGIN_KEY } from "../../lib/create/pendingKeepRuleLogin";
const deleteServerDraft = vi.fn();
vi.mock("../../lib/create/api", () => ({
deleteServerDraft: (...args: unknown[]) => deleteServerDraft(...args),
}));
describe("clearBrowserRuleData", () => {
beforeEach(() => {
deleteServerDraft.mockReset();
window.localStorage.clear();
window.sessionStorage.clear();
});
afterEach(() => {
window.localStorage.clear();
window.sessionStorage.clear();
});
it("clears local draft keys and lastPublished without deleting the server draft", () => {
window.localStorage.setItem(
CREATE_FLOW_ANONYMOUS_KEY,
JSON.stringify({ title: "Stale" }),
);
window.localStorage.setItem(
CORE_VALUE_DETAILS_STORAGE_KEY,
JSON.stringify({ v1: { meaning: "x" } }),
);
window.localStorage.setItem("create-flow-state", "{}");
window.localStorage.setItem("create-flow-draft", "{}");
window.sessionStorage.setItem(
CREATE_FLOW_LAST_PUBLISHED_KEY,
JSON.stringify({ id: "rule-1", title: "Garden", document: {} }),
);
window.sessionStorage.setItem(CREATE_FLOW_PENDING_KEEP_RULE_LOGIN_KEY, "1");
clearBrowserRuleData();
expect(window.localStorage.getItem(CREATE_FLOW_ANONYMOUS_KEY)).toBeNull();
expect(window.localStorage.getItem(CORE_VALUE_DETAILS_STORAGE_KEY)).toBeNull();
expect(window.localStorage.getItem("create-flow-state")).toBeNull();
expect(window.localStorage.getItem("create-flow-draft")).toBeNull();
expect(window.sessionStorage.getItem(CREATE_FLOW_LAST_PUBLISHED_KEY)).toBeNull();
expect(
window.sessionStorage.getItem(CREATE_FLOW_PENDING_KEEP_RULE_LOGIN_KEY),
).toBeNull();
expect(deleteServerDraft).not.toHaveBeenCalled();
});
});
+16
View File
@@ -35,6 +35,7 @@ vi.mock("../../lib/logger", () => ({
import {
claimGuestRulesForUser,
clearGuestRuleClaimCookie,
issueGuestRuleClaimCookie,
} from "../../lib/server/guestRuleClaim";
@@ -76,6 +77,21 @@ describe("issueGuestRuleClaimCookie", () => {
});
});
describe("clearGuestRuleClaimCookie", () => {
it("expires the claim cookie", async () => {
await clearGuestRuleClaimCookie();
expect(cookieSetMock).toHaveBeenCalledWith(
"cr_rule_claim",
"",
expect.objectContaining({
httpOnly: true,
path: "/",
maxAge: 0,
}),
);
});
});
describe("claimGuestRulesForUser", () => {
it("returns 0 when the cookie is empty", async () => {
cookieGetMock.mockReturnValue(undefined);
+29 -2
View File
@@ -1,5 +1,10 @@
import { describe, expect, it } from "vitest";
import { finishedAtIso } from "../../lib/create/lastPublishedRule";
import { afterEach, describe, expect, it } from "vitest";
import {
CREATE_FLOW_LAST_PUBLISHED_KEY,
clearLastPublishedRule,
finishedAtIso,
writeLastPublishedRule,
} from "../../lib/create/lastPublishedRule";
describe("finishedAtIso", () => {
it("normalizes strings and dates", () => {
@@ -17,3 +22,25 @@ describe("finishedAtIso", () => {
expect(finishedAtIso(null)).toBeUndefined();
});
});
describe("clearLastPublishedRule", () => {
afterEach(() => {
sessionStorage.removeItem(CREATE_FLOW_LAST_PUBLISHED_KEY);
});
it("removes the stored finished Rule", () => {
writeLastPublishedRule({
id: "rule-1",
title: "Garden coop",
document: { sections: [] },
});
expect(sessionStorage.getItem(CREATE_FLOW_LAST_PUBLISHED_KEY)).not.toBeNull();
clearLastPublishedRule();
expect(sessionStorage.getItem(CREATE_FLOW_LAST_PUBLISHED_KEY)).toBeNull();
});
it("does nothing when the key is already missing", () => {
expect(() => clearLastPublishedRule()).not.toThrow();
expect(sessionStorage.getItem(CREATE_FLOW_LAST_PUBLISHED_KEY)).toBeNull();
});
});
+4
View File
@@ -26,6 +26,10 @@ vi.mock("../../lib/server/uploads/deleteUploadedFiles", () => ({
deleteUploadsForUser: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("../../lib/server/guestRuleClaim", () => ({
clearGuestRuleClaimCookie: vi.fn().mockResolvedValue(undefined),
}));
import { DELETE } from "../../app/api/user/me/route";
beforeEach(() => {