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(); const requestConfirm = vi.fn(); let applied: CreateFlowState | undefined; const replaceState = vi.fn( (updater: (_prev: CreateFlowState) => CreateFlowState) => { applied = updater({ title: "Juniper Street Tenants", communityAvatarUrl: "/old.png", setupTraits: { employer: true }, }); }, ); beforeEach(() => { vi.mocked(loadTemplateReviewBySlug).mockReset(); vi.mocked(loadTemplateReviewBySlug).mockResolvedValue(loadedTemplate); router.push.mockReset(); updateState.mockReset(); markCreateFlowInteraction.mockReset(); requestConfirm.mockReset(); requestConfirm.mockResolvedValue(true); replaceState.mockClear(); applied = undefined; window.sessionStorage.clear(); window.localStorage.clear(); }); function renderActions( state: CreateFlowState, fromCreateWizard = false, ) { return renderHook(() => useTemplateReviewActions({ pathname: "/create/review-template/mutual-aid-mondays", state, updateState, replaceState, router, fromCreateWizard, requestConfirm, 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 leftover identity, and walks the decks", async () => { const { result } = renderActions({ setupTraits: { employer: true }, communityAvatarUrl: "/old.png", }); 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(applied?.title).toBe("Neighborhood"); expect(applied?.communityContext).toBe("We meet weekly"); expect(applied?.communityLocation).toBe("Oakland"); expect(applied?.communityAvatarUrl).toBeUndefined(); expect(applied?.setupTraits).toBeUndefined(); expect(applied?.pendingTemplateAction).toEqual({ slug: "mutual-aid-mondays", mode: "customize", }); expect(applied?.templateReviewBackSlug).toBe("mutual-aid-mondays"); }); it("direct Use without changes goes to the name screen and clears leftover identity", async () => { const { result } = renderActions({ title: "Neighborhood" }); await act(async () => { await result.current.handleUseWithoutChanges(); }); expect(router.push).toHaveBeenCalledWith("/create/community-name"); expect(applied?.title).toBeUndefined(); expect(applied?.communityContext).toBeUndefined(); expect(applied?.communityAvatarUrl).toBeUndefined(); expect(applied?.setupTraits).toBeUndefined(); expect(applied?.currentStep).toBeUndefined(); expect(applied?.pendingTemplateAction).toEqual({ slug: "mutual-aid-mondays", mode: "useWithoutChanges", }); }); it("in-flow Use without changes keeps the community name, photo, and pins the template path", async () => { const { result } = renderActions( { title: "Neighborhood", communityContext: "We meet weekly", communityLocation: "Oakland", communityAvatarUrl: "/api/uploads/logo", }, true, ); await act(async () => { await result.current.handleUseWithoutChanges(); }); expect(router.push).toHaveBeenCalledWith("/create/community-name"); expect(applied?.title).toBe("Neighborhood"); expect(applied?.communityContext).toBe("We meet weekly"); expect(applied?.communityLocation).toBe("Oakland"); expect(applied?.communityAvatarUrl).toBe("/api/uploads/logo"); expect(applied?.setupTraits).toBeUndefined(); expect(applied?.pendingTemplateAction).toEqual({ slug: "mutual-aid-mondays", mode: "useWithoutChanges", }); expect(applied?.templateReviewBackSlug).toBe("mutual-aid-mondays"); }); it("a persisted in-flow flag without ?fromFlow=1 clears leftover identity", async () => { const { result } = renderActions( { title: "Neighborhood", communityAvatarUrl: "/api/uploads/logo", templateReviewEntryFromCreateFlow: true, }, false, ); await act(async () => { await result.current.handleUseWithoutChanges(); }); expect(applied?.title).toBeUndefined(); expect(applied?.communityAvatarUrl).toBeUndefined(); }); it("asks before replacing a draft in progress", async () => { requestConfirm.mockResolvedValueOnce(false); const { result } = renderActions({ title: "Juniper Street Tenants" }); await act(async () => { await result.current.handleUseWithoutChanges(); }); expect(requestConfirm).toHaveBeenCalledWith( expect.objectContaining({ title: "Replace the draft in progress?", }), ); expect(router.push).not.toHaveBeenCalled(); expect(replaceState).not.toHaveBeenCalled(); }); });