Files
community-rule/tests/unit/hooks/useTemplateReviewActions.test.tsx
T
adilallo 6498d52541 Skip setup questions on the template path and collect name, photo, and description without recommendations.
Customize opens a dialog then walks the template decks; Use without changes lands on final review with Has Name Field. Template cards stay first, the engine is not called, and the catalog no longer shows a recommended tag.
2026-11-09 17:34:33 -07:00

180 lines
5.3 KiB
TypeScript

import { renderHook, act } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import type { CreateFlowState } from "../../../app/(app)/create/types";
import { useTemplateReviewActions } from "../../../app/(app)/create/hooks/useTemplateReviewActions";
import { loadTemplateReviewBySlug } from "../../../lib/create/loadTemplateReviewBySlug";
vi.mock("../../../lib/create/loadTemplateReviewBySlug", () => ({
loadTemplateReviewBySlug: vi.fn(),
}));
const templateBody = {
sections: [
{
categoryName: "Communication",
entries: [{ title: "Signal" }],
},
],
};
const loadedTemplate = {
ok: true as const,
template: {
id: "t1",
slug: "mutual-aid-mondays",
title: "Mutual Aid Mondays",
category: null,
description: null,
body: templateBody,
sortOrder: 0,
featured: false,
},
};
describe("useTemplateReviewActions", () => {
const router = { push: vi.fn() };
const updateState = vi.fn();
const markCreateFlowInteraction = vi.fn();
let applied: CreateFlowState | undefined;
const replaceState = vi.fn(
(updater: (_prev: CreateFlowState) => CreateFlowState) => {
applied = updater({});
},
);
beforeEach(() => {
vi.mocked(loadTemplateReviewBySlug).mockReset();
vi.mocked(loadTemplateReviewBySlug).mockResolvedValue(loadedTemplate);
router.push.mockReset();
updateState.mockReset();
markCreateFlowInteraction.mockReset();
replaceState.mockClear();
applied = undefined;
});
function renderActions(
state: CreateFlowState,
fromCreateWizard = false,
) {
return renderHook(() =>
useTemplateReviewActions({
pathname: "/create/review-template/mutual-aid-mondays",
state,
updateState,
replaceState,
router,
fromCreateWizard,
markCreateFlowInteraction,
}),
);
}
it("Customize opens the dialog and does not route until Continue", async () => {
const { result } = renderActions({});
await act(async () => {
await result.current.handleCustomize();
});
expect(markCreateFlowInteraction).toHaveBeenCalled();
expect(router.push).not.toHaveBeenCalled();
expect(result.current.customizeDialog).toEqual(
expect.objectContaining({
slug: "mutual-aid-mondays",
templateTitle: "Mutual Aid Mondays",
}),
);
});
it("Customize Continue prefills cards, drops setup answers, and walks the decks", async () => {
const { result } = renderActions({
setupTraits: { employer: true },
});
await act(async () => {
await result.current.handleCustomize();
});
act(() => {
result.current.confirmCustomizeDialog({
title: "Neighborhood",
communityContext: "We meet weekly",
communityLocation: "Oakland",
});
});
expect(router.push).toHaveBeenCalledWith("/create/core-values");
expect(updateState).toHaveBeenCalledWith(
expect.objectContaining({
title: "Neighborhood",
communityContext: "We meet weekly",
communityLocation: "Oakland",
setupTraits: undefined,
pendingTemplateAction: {
slug: "mutual-aid-mondays",
mode: "customize",
},
templateReviewBackSlug: "mutual-aid-mondays",
}),
);
});
it("direct Use without changes goes to final review and clears leftover identity", async () => {
replaceState.mockImplementation(
(updater: (_prev: CreateFlowState) => CreateFlowState) => {
applied = updater({
title: "Neighborhood",
communityContext: "Stale description",
currentStep: "confirm-stakeholders",
setupTraits: { employer: true, network: false },
});
},
);
const { result } = renderActions({ title: "Neighborhood" });
await act(async () => {
await result.current.handleUseWithoutChanges();
});
expect(router.push).toHaveBeenCalledWith("/create/final-review");
expect(applied?.title).toBeUndefined();
expect(applied?.communityContext).toBeUndefined();
expect(applied?.currentStep).toBeUndefined();
expect(applied?.setupTraits).toBeUndefined();
expect(applied?.pendingTemplateAction).toEqual({
slug: "mutual-aid-mondays",
mode: "useWithoutChanges",
});
});
it("in-flow Use without changes keeps the community name and pins the template path", async () => {
replaceState.mockImplementation(
(updater: (_prev: CreateFlowState) => CreateFlowState) => {
applied = updater({
title: "Neighborhood",
communityContext: "We meet weekly",
templateReviewEntryFromCreateFlow: true,
setupTraits: { employer: true },
});
},
);
const { result } = renderActions(
{ title: "Neighborhood", templateReviewEntryFromCreateFlow: true },
true,
);
await act(async () => {
await result.current.handleUseWithoutChanges();
});
expect(router.push).toHaveBeenCalledWith("/create/final-review");
expect(applied?.title).toBe("Neighborhood");
expect(applied?.communityContext).toBe("We meet weekly");
expect(applied?.setupTraits).toBeUndefined();
expect(applied?.pendingTemplateAction).toEqual({
slug: "mutual-aid-mondays",
mode: "useWithoutChanges",
});
expect(applied?.templateReviewBackSlug).toBe("mutual-aid-mondays");
});
});