Wire Publish rule from create flow
This commit is contained in:
@@ -1,7 +1,31 @@
|
||||
import { useLayoutEffect } from "react";
|
||||
import { describe, it, expect } 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 FinalReviewPage from "../../app/create/final-review/page";
|
||||
import { useCreateFlow } from "../../app/create/context/CreateFlowContext";
|
||||
|
||||
const FALLBACK_CARD_TITLE = "Your community";
|
||||
const FALLBACK_CARD_DESCRIPTION_SNIPPET =
|
||||
"Add a short description of your community";
|
||||
|
||||
function FinalReviewWithFlowState({
|
||||
title,
|
||||
summary,
|
||||
}: {
|
||||
title: string;
|
||||
summary?: string;
|
||||
}) {
|
||||
const { replaceState } = useCreateFlow();
|
||||
useLayoutEffect(() => {
|
||||
replaceState({ title, ...(summary !== undefined ? { summary } : {}) });
|
||||
}, [replaceState, title, summary]);
|
||||
return <FinalReviewPage />;
|
||||
}
|
||||
|
||||
describe("FinalReviewPage", () => {
|
||||
it("renders without crashing", () => {
|
||||
@@ -27,17 +51,27 @@ describe("FinalReviewPage", () => {
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders RuleCard with title", () => {
|
||||
it("renders RuleCard with fallback title when context has no name", () => {
|
||||
render(<FinalReviewPage />);
|
||||
expect(screen.getByText("Mutual Aid Mondays")).toBeInTheDocument();
|
||||
expect(screen.getByText(FALLBACK_CARD_TITLE)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders RuleCard with description", () => {
|
||||
it("renders RuleCard with fallback description when context has no summary", () => {
|
||||
render(<FinalReviewPage />);
|
||||
expect(
|
||||
screen.getByText(
|
||||
/Mutual Aid Monday is a grassroots community in Denver, founded in November 2020 by Kelsang Virya, dedicated to supporting neighbors experiencing homelessness./i,
|
||||
),
|
||||
screen.getByText(new RegExp(FALLBACK_CARD_DESCRIPTION_SNIPPET, "i")),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders RuleCard title from create flow state", async () => {
|
||||
render(
|
||||
<FinalReviewWithFlowState title="Oak Park Commons" summary="Local mutual aid." />,
|
||||
);
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("Oak Park Commons")).toBeInTheDocument();
|
||||
});
|
||||
expect(
|
||||
screen.getByText(/Local mutual aid\./i),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
@@ -46,7 +80,7 @@ describe("FinalReviewPage", () => {
|
||||
const buttons = screen.getAllByRole("button");
|
||||
expect(buttons.length).toBeGreaterThanOrEqual(1);
|
||||
expect(
|
||||
buttons.some((el) => el.textContent?.includes("Mutual Aid Mondays")),
|
||||
buttons.some((el) => el.textContent?.includes(FALLBACK_CARD_TITLE)),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
buildPublishPayload,
|
||||
parseDocumentSectionsForDisplay,
|
||||
parseSectionsFromCreateFlowState,
|
||||
} from "../../lib/create/buildPublishPayload";
|
||||
import type { CreateFlowState } from "../../app/create/types";
|
||||
|
||||
describe("buildPublishPayload", () => {
|
||||
it("returns error when title missing", () => {
|
||||
expect(buildPublishPayload({})).toEqual({
|
||||
ok: false,
|
||||
error: "missingCommunityName",
|
||||
});
|
||||
});
|
||||
|
||||
it("returns error when title is whitespace only", () => {
|
||||
expect(buildPublishPayload({ title: " \n\t " })).toEqual({
|
||||
ok: false,
|
||||
error: "missingCommunityName",
|
||||
});
|
||||
});
|
||||
|
||||
it("returns title and fallback Overview section when no sections", () => {
|
||||
const r = buildPublishPayload({ title: "Oak Park Commons" });
|
||||
expect(r.ok).toBe(true);
|
||||
if (!r.ok) return;
|
||||
expect(r.title).toBe("Oak Park Commons");
|
||||
expect(r.summary).toBeUndefined();
|
||||
expect(r.document).toEqual({
|
||||
sections: [
|
||||
{
|
||||
categoryName: "Overview",
|
||||
entries: [
|
||||
{
|
||||
title: "Community",
|
||||
body: "This CommunityRule was created in the create flow. Add more detail in a future edit.",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("includes trimmed summary in payload and uses it as fallback section body", () => {
|
||||
const r = buildPublishPayload({
|
||||
title: " My Group ",
|
||||
summary: " We organize locally. ",
|
||||
});
|
||||
expect(r.ok).toBe(true);
|
||||
if (!r.ok) return;
|
||||
expect(r.title).toBe("My Group");
|
||||
expect(r.summary).toBe("We organize locally.");
|
||||
expect(r.document).toEqual({
|
||||
sections: [
|
||||
{
|
||||
categoryName: "Overview",
|
||||
entries: [{ title: "Community", body: "We organize locally." }],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("uses valid state.sections when present", () => {
|
||||
const sections: CreateFlowState["sections"] = [
|
||||
{
|
||||
categoryName: "Values",
|
||||
entries: [{ title: "A", body: "B" }],
|
||||
},
|
||||
];
|
||||
const r = buildPublishPayload({ title: "T", sections });
|
||||
expect(r.ok).toBe(true);
|
||||
if (!r.ok) return;
|
||||
expect(r.document).toEqual({ sections });
|
||||
});
|
||||
|
||||
it("filters invalid section entries from state.sections", () => {
|
||||
const r = buildPublishPayload({
|
||||
title: "T",
|
||||
sections: [
|
||||
{ categoryName: "Values", entries: [{ title: "A", body: "B" }] },
|
||||
{ bad: true } as unknown as Record<string, unknown>,
|
||||
],
|
||||
});
|
||||
expect(r.ok).toBe(true);
|
||||
if (!r.ok) return;
|
||||
expect(r.document).toEqual({
|
||||
sections: [{ categoryName: "Values", entries: [{ title: "A", body: "B" }] }],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseDocumentSectionsForDisplay", () => {
|
||||
it("returns empty for non-object", () => {
|
||||
expect(parseDocumentSectionsForDisplay(null)).toEqual([]);
|
||||
});
|
||||
|
||||
it("parses valid sections array", () => {
|
||||
const doc = {
|
||||
sections: [
|
||||
{ categoryName: "X", entries: [{ title: "t", body: "b" }] },
|
||||
],
|
||||
};
|
||||
expect(parseDocumentSectionsForDisplay(doc)).toEqual(doc.sections);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseSectionsFromCreateFlowState", () => {
|
||||
it("returns empty when sections missing", () => {
|
||||
expect(parseSectionsFromCreateFlowState({})).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { publishRule } from "../../lib/create/api";
|
||||
|
||||
const input = {
|
||||
title: "T",
|
||||
document: { sections: [] as unknown[] },
|
||||
};
|
||||
|
||||
describe("publishRule", () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
|
||||
it("returns ok on 200 with rule", async () => {
|
||||
globalThis.fetch = vi.fn().mockResolvedValue(
|
||||
new Response(JSON.stringify({ rule: { id: "r1", title: "T" } }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}),
|
||||
);
|
||||
const result = await publishRule(input);
|
||||
expect(result).toEqual({ ok: true, id: "r1", title: "T" });
|
||||
});
|
||||
|
||||
it("does not throw when body is empty (e.g. connection reset)", async () => {
|
||||
globalThis.fetch = vi.fn().mockResolvedValue(
|
||||
new Response("", {
|
||||
status: 503,
|
||||
statusText: "Service Unavailable",
|
||||
}),
|
||||
);
|
||||
const result = await publishRule(input);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok === false) {
|
||||
expect(result.status).toBe(503);
|
||||
expect(result.error).toBe("Service Unavailable");
|
||||
}
|
||||
});
|
||||
|
||||
it("parses validation error when JSON present", async () => {
|
||||
globalThis.fetch = vi.fn().mockResolvedValue(
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
error: { code: "validation_error", message: "title required" },
|
||||
}),
|
||||
{ status: 400, headers: { "Content-Type": "application/json" } },
|
||||
),
|
||||
);
|
||||
const result = await publishRule(input);
|
||||
expect(result).toEqual({
|
||||
ok: false,
|
||||
error: "title required",
|
||||
status: 400,
|
||||
});
|
||||
});
|
||||
|
||||
it("returns network message when fetch rejects", async () => {
|
||||
globalThis.fetch = vi.fn().mockRejectedValue(new Error("offline"));
|
||||
const result = await publishRule(input);
|
||||
expect(result).toEqual({
|
||||
ok: false,
|
||||
error: "Something went wrong. Check your connection and try again.",
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user