Open a template from its cards even when a saved draft exists.

This commit is contained in:
adilallo committed 2026-11-22 18:08:34 -07:00
1 parent 851133e56d
commit 55b071aa83
11 files changed
+326 -140

No files matched your search

+1 -1
View File
@@ -121,7 +121,7 @@ describe("ProfilePageView", () => {
expect(screen.getByText(copy.emptyCardBody)).toBeInTheDocument();
expect(
screen.getByRole("link", { name: copy.emptyCardBrowse }),
).toHaveAttribute("href", "/templates?fromFlow=1");
).toHaveAttribute("href", "/templates");
await user.click(
screen.getByRole("button", { name: copy.emptyCardCreate }),
+26
View File
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import {
buildAppliedTemplateCreateFlowState,
buildCoreValuesPrefillFromTemplateBody,
buildTemplateCustomizePrefill,
templatePresetDetailsFromPrefill,
@@ -224,6 +225,31 @@ describe("buildTemplateCustomizePrefill from templateChips.json", () => {
});
});
describe("buildAppliedTemplateCreateFlowState", () => {
it("starts from the template and drops leftover draft identity", () => {
const seeded = buildAppliedTemplateCreateFlowState({
slug: "mutual-aid-mondays",
body: {
sections: [
{
categoryName: "Communication",
entries: [{ title: "Signal" }],
},
],
},
mode: "useWithoutChanges",
});
expect(seeded.title).toBeUndefined();
expect(seeded.communityAvatarUrl).toBeUndefined();
expect(seeded.setupTraits).toBeUndefined();
expect(seeded.selectedCommunicationMethodIds).toEqual(["signal"]);
expect(seeded.pendingTemplateAction).toEqual({
slug: "mutual-aid-mondays",
mode: "useWithoutChanges",
});
});
});
describe("templatePresetDetailsFromPrefill", () => {
it("fills catalog copy for each prefilled chip", () => {
const details = templatePresetDetailsFromPrefill({
@@ -35,10 +35,15 @@ 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({});
applied = updater({
title: "Juniper Street Tenants",
communityAvatarUrl: "/old.png",
setupTraits: { employer: true },
});
},
);
@@ -48,8 +53,12 @@ describe("useTemplateReviewActions", () => {
router.push.mockReset();
updateState.mockReset();
markCreateFlowInteraction.mockReset();
requestConfirm.mockReset();
requestConfirm.mockResolvedValue(true);
replaceState.mockClear();
applied = undefined;
window.sessionStorage.clear();
window.localStorage.clear();
});
function renderActions(
@@ -64,6 +73,7 @@ describe("useTemplateReviewActions", () => {
replaceState,
router,
fromCreateWizard,
requestConfirm,
markCreateFlowInteraction,
}),
);
@@ -86,9 +96,10 @@ describe("useTemplateReviewActions", () => {
);
});
it("Customize Continue prefills cards, drops setup answers, and walks the decks", async () => {
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 () => {
@@ -103,32 +114,19 @@ describe("useTemplateReviewActions", () => {
});
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",
}),
);
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 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 () => {
@@ -138,8 +136,9 @@ describe("useTemplateReviewActions", () => {
expect(router.push).toHaveBeenCalledWith("/create/final-review");
expect(applied?.title).toBeUndefined();
expect(applied?.communityContext).toBeUndefined();
expect(applied?.currentStep).toBeUndefined();
expect(applied?.communityAvatarUrl).toBeUndefined();
expect(applied?.setupTraits).toBeUndefined();
expect(applied?.currentStep).toBeUndefined();
expect(applied?.pendingTemplateAction).toEqual({
slug: "mutual-aid-mondays",
mode: "useWithoutChanges",
@@ -147,18 +146,12 @@ describe("useTemplateReviewActions", () => {
});
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 },
{
title: "Neighborhood",
communityContext: "We meet weekly",
templateReviewEntryFromCreateFlow: true,
},
true,
);
@@ -176,4 +169,21 @@ describe("useTemplateReviewActions", () => {
});
expect(applied?.templateReviewBackSlug).toBe("mutual-aid-mondays");
});
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();
});
});
@@ -1,7 +1,11 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
FRESH_ENTRY_PENDING_KEY,
TEMPLATE_APPLY_LATCH_KEY,
clearTemplateApplyLatch,
hasFreshEntryPending,
hasTemplateApplyLatch,
latchTemplateApply,
prepareFreshCreateFlowEntry,
prepareFreshCreateFlowEntrySync,
} from "../../app/(app)/create/utils/prepareFreshCreateFlowEntry";
@@ -66,6 +70,25 @@ describe("prepareFreshCreateFlowEntrySync", () => {
});
});
describe("template apply latch", () => {
beforeEach(() => {
window.sessionStorage.clear();
});
afterEach(() => {
window.sessionStorage.clear();
});
it("sets, reads, and clears the session flag", () => {
expect(hasTemplateApplyLatch()).toBe(false);
latchTemplateApply();
expect(window.sessionStorage.getItem(TEMPLATE_APPLY_LATCH_KEY)).toBe("1");
expect(hasTemplateApplyLatch()).toBe(true);
clearTemplateApplyLatch();
expect(hasTemplateApplyLatch()).toBe(false);
});
});
describe("prepareFreshCreateFlowEntry", () => {
beforeEach(() => {
deleteServerDraft.mockReset();