Turn a saved draft into the finished Rule, and greet with congratulations.
This commit is contained in:
1 parent
a2815ae05f
commit
851133e56d
11 files changed
+150
-23
No files matched your search
@@ -224,6 +224,11 @@ describe("ProfilePageView", () => {
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(
|
||||
screen.getByRole("heading", {
|
||||
name: copy.welcomeTitle.replace("{{name}}", "dani@example.com"),
|
||||
}),
|
||||
).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByText(copy.welcomeBodyFirstRule),
|
||||
).toBeInTheDocument();
|
||||
@@ -343,5 +348,11 @@ describe("ProfilePageView", () => {
|
||||
expect(
|
||||
screen.getByRole("button", { name: copy.continueDraft }),
|
||||
).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByRole("button", { name: copy.deleteRule }),
|
||||
).toBeInTheDocument();
|
||||
expect(
|
||||
screen.queryByRole("button", { name: copy.inviteMembers }),
|
||||
).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
getNextStep,
|
||||
getPreviousStep,
|
||||
isValidStep,
|
||||
resumeCreateFlowStep,
|
||||
getStepIndex,
|
||||
parseReviewReturnSearchParam,
|
||||
parseCreateFlowScreenFromPathname,
|
||||
@@ -46,6 +47,14 @@ describe("flowSteps", () => {
|
||||
expect(getPreviousStep(null)).toBeNull();
|
||||
});
|
||||
|
||||
it("resumeCreateFlowStep opens the screen the draft was left on", () => {
|
||||
expect(resumeCreateFlowStep({ currentStep: "core-values" })).toBe(
|
||||
"core-values",
|
||||
);
|
||||
expect(resumeCreateFlowStep({})).toBe("informational");
|
||||
expect(resumeCreateFlowStep(null)).toBe("informational");
|
||||
});
|
||||
|
||||
it("isValidStep allows branch-only edit-rule URL segment", () => {
|
||||
expect(isValidStep("edit-rule")).toBe(true);
|
||||
});
|
||||
|
||||
@@ -2,7 +2,11 @@ import { renderHook, act } from "@testing-library/react";
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import type { CreateFlowState } from "../../../app/(app)/create/types";
|
||||
import { useCreateFlowFinalize } from "../../../app/(app)/create/hooks/useCreateFlowFinalize";
|
||||
import { publishRule, updatePublishedRule } from "../../../lib/create/api";
|
||||
import {
|
||||
deleteServerDraft,
|
||||
publishRule,
|
||||
updatePublishedRule,
|
||||
} from "../../../lib/create/api";
|
||||
import { buildPublishPayload } from "../../../lib/create/buildPublishPayload";
|
||||
import { stateWithPendingCommunityAvatar } from "../../../lib/create/flushPendingCommunityAvatar";
|
||||
import messages from "../../../messages/en/index";
|
||||
@@ -25,6 +29,7 @@ vi.mock("../../../lib/create/buildPublishPayload", () => ({
|
||||
vi.mock("../../../lib/create/api", () => ({
|
||||
publishRule: vi.fn(),
|
||||
updatePublishedRule: vi.fn(),
|
||||
deleteServerDraft: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../../../lib/create/flushPendingCommunityAvatar", () => ({
|
||||
@@ -50,6 +55,8 @@ describe("useCreateFlowFinalize", () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(publishRule).mockReset();
|
||||
vi.mocked(updatePublishedRule).mockReset();
|
||||
vi.mocked(deleteServerDraft).mockReset();
|
||||
vi.mocked(deleteServerDraft).mockResolvedValue(undefined);
|
||||
vi.mocked(writeLastPublishedRule).mockReset();
|
||||
vi.mocked(stateWithPendingCommunityAvatar).mockReset();
|
||||
vi.mocked(stateWithPendingCommunityAvatar).mockImplementation(
|
||||
@@ -101,6 +108,7 @@ describe("useCreateFlowFinalize", () => {
|
||||
expect(
|
||||
sessionStorage.getItem(CREATE_FLOW_PENDING_KEEP_RULE_LOGIN_KEY),
|
||||
).toBeNull();
|
||||
expect(deleteServerDraft).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not publish while the session is unresolved", async () => {
|
||||
@@ -408,6 +416,7 @@ describe("useCreateFlowFinalize", () => {
|
||||
summary: "Published summary",
|
||||
document: {},
|
||||
});
|
||||
expect(deleteServerDraft).toHaveBeenCalledTimes(1);
|
||||
expect(updateState).toHaveBeenCalledWith({
|
||||
editingPublishedRuleId: undefined,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const findUniqueMock = vi.fn();
|
||||
const deleteManyMock = vi.fn();
|
||||
const deleteUploadsForDraftMock = vi.fn();
|
||||
|
||||
vi.mock("../../lib/server/db", () => ({
|
||||
prisma: {
|
||||
ruleDraft: {
|
||||
findUnique: (...args: unknown[]) => findUniqueMock(...args),
|
||||
deleteMany: (...args: unknown[]) => deleteManyMock(...args),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/uploads/deleteUploadedFiles", () => ({
|
||||
deleteUploadsForDraft: (...args: unknown[]) =>
|
||||
deleteUploadsForDraftMock(...args),
|
||||
}));
|
||||
|
||||
import { deleteRuleDraftForUser } from "../../lib/server/ruleDrafts";
|
||||
|
||||
describe("deleteRuleDraftForUser", () => {
|
||||
beforeEach(() => {
|
||||
findUniqueMock.mockReset();
|
||||
deleteManyMock.mockReset();
|
||||
deleteUploadsForDraftMock.mockReset();
|
||||
});
|
||||
|
||||
it("removes the draft and files still tied to it", async () => {
|
||||
findUniqueMock.mockResolvedValueOnce({ id: "draft-1" });
|
||||
deleteUploadsForDraftMock.mockResolvedValueOnce(undefined);
|
||||
deleteManyMock.mockResolvedValueOnce({ count: 1 });
|
||||
|
||||
await deleteRuleDraftForUser("user-1");
|
||||
|
||||
expect(findUniqueMock).toHaveBeenCalledWith({
|
||||
where: { userId: "user-1" },
|
||||
select: { id: true },
|
||||
});
|
||||
expect(deleteUploadsForDraftMock).toHaveBeenCalledWith("draft-1");
|
||||
expect(deleteManyMock).toHaveBeenCalledWith({
|
||||
where: { userId: "user-1" },
|
||||
});
|
||||
});
|
||||
|
||||
it("still deletes when there is no draft row", async () => {
|
||||
findUniqueMock.mockResolvedValueOnce(null);
|
||||
deleteManyMock.mockResolvedValueOnce({ count: 0 });
|
||||
|
||||
await deleteRuleDraftForUser("user-1");
|
||||
|
||||
expect(deleteUploadsForDraftMock).not.toHaveBeenCalled();
|
||||
expect(deleteManyMock).toHaveBeenCalledWith({
|
||||
where: { userId: "user-1" },
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -48,6 +48,12 @@ vi.mock("../../lib/server/uploads/linkUploads", () => ({
|
||||
linkUploadsToRule: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
const deleteRuleDraftForUserMock = vi.fn();
|
||||
vi.mock("../../lib/server/ruleDrafts", () => ({
|
||||
deleteRuleDraftForUser: (...args: unknown[]) =>
|
||||
deleteRuleDraftForUserMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/db", () => ({
|
||||
prisma: {
|
||||
$transaction: (fn: (tx: unknown) => Promise<unknown>) =>
|
||||
@@ -72,6 +78,8 @@ beforeEach(() => {
|
||||
sendInviteMock.mockReset();
|
||||
rateLimitKeyMock.mockReset();
|
||||
issueGuestRuleClaimCookieMock.mockReset();
|
||||
deleteRuleDraftForUserMock.mockReset();
|
||||
deleteRuleDraftForUserMock.mockResolvedValue(undefined);
|
||||
rateLimitKeyMock.mockReturnValue({ ok: true as const });
|
||||
getSessionUserMock.mockResolvedValue({
|
||||
id: "user-1",
|
||||
@@ -112,6 +120,7 @@ describe("POST /api/rules", () => {
|
||||
).toBeUndefined();
|
||||
expect(issueGuestRuleClaimCookieMock).not.toHaveBeenCalled();
|
||||
expect(sendInviteMock).not.toHaveBeenCalled();
|
||||
expect(deleteRuleDraftForUserMock).toHaveBeenCalledWith("user-1");
|
||||
});
|
||||
|
||||
it("stores place and group type from the published document", async () => {
|
||||
@@ -196,6 +205,7 @@ describe("POST /api/rules", () => {
|
||||
}),
|
||||
);
|
||||
expect(publishedRuleCreateMock).not.toHaveBeenCalled();
|
||||
expect(deleteRuleDraftForUserMock).toHaveBeenCalledWith("user-1");
|
||||
});
|
||||
|
||||
it("rolls back publish when mail fails", async () => {
|
||||
@@ -238,6 +248,7 @@ describe("POST /api/rules", () => {
|
||||
expect(publishedRuleDeleteMock).toHaveBeenCalledWith({
|
||||
where: { id: "rule-new" },
|
||||
});
|
||||
expect(deleteRuleDraftForUserMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps the rule when a later invite send fails after one succeeded", async () => {
|
||||
@@ -329,6 +340,7 @@ describe("POST /api/rules", () => {
|
||||
expect(sendInviteMock).not.toHaveBeenCalled();
|
||||
const body = (await res.json()) as { rule: { id: string } };
|
||||
expect(body.rule.id).toBe("rule-guest");
|
||||
expect(deleteRuleDraftForUserMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rate-limits anonymous publish by IP", async () => {
|
||||
|
||||
Reference in new issue
Block a user