New edit-rule page created

This commit is contained in:
adilallo
2026-04-29 16:05:37 -06:00
parent ac1157a172
commit 3a9727bceb
25 changed files with 875 additions and 52 deletions
+64
View File
@@ -401,3 +401,67 @@ describe("FinalReviewScreen — chip edit modal save semantics", () => {
expect(latest.communicationMethodDetailsById).toBeUndefined();
});
});
function FinalReviewEditPublishedWithStateProbe({
onState,
initial,
}: {
onState: (_state: CreateFlowState) => void;
initial: CreateFlowState;
}) {
const { state, replaceState } = useCreateFlow();
useLayoutEffect(() => {
replaceState(initial);
}, [replaceState, initial]);
useEffect(() => {
onState(state);
}, [state, onState]);
return <FinalReviewScreen variant="editPublished" />;
}
describe("FinalReviewScreen — edit published description", () => {
it("does not expose click-to-edit description on default final review", () => {
render(
<FinalReviewWithFlowState
title="Oak"
communityContext="Visible body"
/>,
);
expect(screen.queryByTestId("rule-description-edit")).not.toBeInTheDocument();
});
it("opens Save modal from description click and updates communityContext + summary", async () => {
let latest: CreateFlowState = {};
render(
<FinalReviewEditPublishedWithStateProbe
onState={(s) => {
latest = s;
}}
initial={{
title: "Oak Park Commons",
communityContext: "Original",
summary: "Original",
selectedCommunicationMethodIds: ["signal"],
}}
/>,
);
void latest;
fireEvent.click(await screen.findByTestId("rule-description-edit"));
const dialog = await screen.findByRole("dialog");
expect(
within(dialog).getByText(/Community description/i),
).toBeInTheDocument();
const input = within(dialog).getByRole("textbox");
fireEvent.change(input, { target: { value: "Updated copy" } });
fireEvent.click(within(dialog).getByRole("button", { name: "Save" }));
await waitFor(() => {
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
});
await waitFor(() => {
expect(latest.communityContext).toBe("Updated copy");
expect(latest.summary).toBe("Updated copy");
});
});
});
+17
View File
@@ -106,6 +106,23 @@ describe("Rule Component", () => {
).not.toBeInTheDocument();
});
it("clicking editable description calls onDescriptionClick and does not fire card onClick", () => {
const onCard = vi.fn();
const onDesc = vi.fn();
render(
<Rule
{...defaultProps}
expanded={true}
onClick={onCard}
onDescriptionClick={onDesc}
descriptionEmptyHint="Add description"
/>,
);
fireEvent.click(screen.getByTestId("rule-description-edit"));
expect(onDesc).toHaveBeenCalledTimes(1);
expect(onCard).not.toHaveBeenCalled();
});
it("applies proper sizing for expanded states", () => {
render(<Rule {...defaultProps} expanded={true} size="L" />);
@@ -50,4 +50,8 @@ describe("getProportionBarProgressForCreateFlowStep", () => {
getProportionBarProgressForCreateFlowStep("conflict-management"),
).toBe("3-0");
});
it("uses 3-2 on edit-rule (same as final-review segment)", () => {
expect(getProportionBarProgressForCreateFlowStep("edit-rule")).toBe("3-2");
});
});
+7 -6
View File
@@ -38,12 +38,13 @@ describe("flowSteps", () => {
expect(getPreviousStep(null)).toBeNull();
});
it("isValidStep reflects FLOW_STEP_ORDER membership", () => {
expect(isValidStep("community-size")).toBe(true);
expect(isValidStep("confirm-stakeholders")).toBe(true);
expect(isValidStep("core-values")).toBe(true);
expect(isValidStep("nope")).toBe(false);
expect(isValidStep(null)).toBe(false);
it("isValidStep allows branch-only edit-rule URL segment", () => {
expect(isValidStep("edit-rule")).toBe(true);
});
it("getNextStep and getPreviousStep return null for edit-rule (not in linear order)", () => {
expect(getNextStep("edit-rule")).toBeNull();
expect(getPreviousStep("edit-rule")).toBeNull();
});
it("getStepIndex matches position in FLOW_STEP_ORDER", () => {
@@ -0,0 +1,52 @@
import { describe, expect, it } from "vitest";
import { createFlowStateFromPublishedRule } from "../../lib/create/publishedDocumentToCreateFlowState";
describe("createFlowStateFromPublishedRule", () => {
it("maps coreValues and methodSelections into draft fields", () => {
const partial = createFlowStateFromPublishedRule({
id: "rule-1",
title: "Oak",
summary: "River cleanup",
document: {
coreValues: [
{ chipId: "1", label: "Ecology", meaning: "m", signals: "s" },
],
methodSelections: {
communication: [
{
id: "slack",
label: "Slack",
sections: {
corePrinciple: "p",
logisticsAdmin: "l",
codeOfConduct: "c",
},
},
],
},
},
});
expect(partial.editingPublishedRuleId).toBe("rule-1");
expect(partial.title).toBe("Oak");
expect(partial.communityContext).toBe("River cleanup");
expect(partial.selectedCoreValueIds).toEqual(["1"]);
expect(partial.coreValuesChipsSnapshot?.[0]?.label).toBe("Ecology");
expect(partial.selectedCommunicationMethodIds).toEqual(["slack"]);
expect(partial.communicationMethodDetailsById?.slack?.corePrinciple).toBe(
"p",
);
expect(partial.sections).toEqual([]);
});
it("sets sections to [] even when methodSelections is missing (edit hydrate)", () => {
const partial = createFlowStateFromPublishedRule({
id: "rule-2",
title: "Pine",
summary: "",
document: {
coreValues: [],
},
});
expect(partial.sections).toEqual([]);
});
});
+111
View File
@@ -0,0 +1,111 @@
import { NextRequest } from "next/server";
import { beforeEach, describe, expect, it, vi } from "vitest";
const isDatabaseConfiguredMock = vi.fn();
const findUniqueMock = vi.fn();
const updateMock = vi.fn();
const getSessionUserMock = vi.fn();
vi.mock("../../lib/server/env", () => ({
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
}));
vi.mock("../../lib/server/db", () => ({
prisma: {
publishedRule: {
findUnique: (...args: unknown[]) => findUniqueMock(...args),
update: (...args: unknown[]) => updateMock(...args),
},
},
}));
vi.mock("../../lib/server/session", () => ({
getSessionUser: () => getSessionUserMock(),
}));
import { PATCH } from "../../app/api/rules/[id]/route";
function makeContext(id: string) {
return { params: Promise.resolve({ id }) };
}
beforeEach(() => {
isDatabaseConfiguredMock.mockReset();
findUniqueMock.mockReset();
updateMock.mockReset();
getSessionUserMock.mockReset();
getSessionUserMock.mockResolvedValue({ id: "user-1", email: "a@b.c" });
});
describe("PATCH /api/rules/[id]", () => {
it("returns 401 when unauthenticated", async () => {
isDatabaseConfiguredMock.mockReturnValue(true);
getSessionUserMock.mockResolvedValueOnce(null);
const res = await PATCH(
new NextRequest("https://x.test/api/rules/r1", {
method: "PATCH",
body: JSON.stringify({
title: "T",
summary: null,
document: { sections: [] },
}),
}),
makeContext("r1"),
);
expect(res.status).toBe(401);
expect(updateMock).not.toHaveBeenCalled();
});
it("returns 403 when the rule belongs to another user", async () => {
isDatabaseConfiguredMock.mockReturnValue(true);
findUniqueMock.mockResolvedValueOnce({
id: "r1",
userId: "other",
});
const res = await PATCH(
new NextRequest("https://x.test/api/rules/r1", {
method: "PATCH",
body: JSON.stringify({
title: "T",
summary: null,
document: { sections: [] },
}),
}),
makeContext("r1"),
);
expect(res.status).toBe(403);
expect(updateMock).not.toHaveBeenCalled();
});
it("updates the rule when owner matches", async () => {
isDatabaseConfiguredMock.mockReturnValue(true);
findUniqueMock.mockResolvedValueOnce({
id: "r1",
userId: "user-1",
});
updateMock.mockResolvedValueOnce({});
const res = await PATCH(
new NextRequest("https://x.test/api/rules/r1", {
method: "PATCH",
body: JSON.stringify({
title: "Updated",
summary: "Context",
document: { sections: [], coreValues: [] },
}),
}),
makeContext("r1"),
);
expect(res.status).toBe(200);
const body = (await res.json()) as { ok: boolean };
expect(body.ok).toBe(true);
expect(updateMock).toHaveBeenCalledWith(
expect.objectContaining({
where: { id: "r1" },
data: expect.objectContaining({
title: "Updated",
summary: "Context",
}),
}),
);
});
});