139 lines
4.0 KiB
TypeScript
139 lines
4.0 KiB
TypeScript
import type { ActionFunctionArgs } from "react-router";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { action } from "./$id";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { db } from "~/utils/db.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
|
|
const dbMocks = vi.hoisted(() => ({
|
|
sequenceFindFirst: vi.fn(),
|
|
sequenceUpdate: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/utils/db.server", () => ({
|
|
db: {
|
|
emailSequence: {
|
|
findFirst: dbMocks.sequenceFindFirst,
|
|
update: dbMocks.sequenceUpdate,
|
|
},
|
|
},
|
|
dbRead: {
|
|
emailSequence: {
|
|
findFirst: vi.fn(),
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({
|
|
verifyUserHasPermission: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/locale/i18next.server", () => ({
|
|
getTranslations: vi.fn(async () => ({ t: (key: string) => `translated:${key}` })),
|
|
}));
|
|
|
|
vi.mock("i18next", () => ({
|
|
t: (key: string) => key,
|
|
}));
|
|
|
|
async function callActionResponse(values: Record<string, string>) {
|
|
const body = new URLSearchParams(values);
|
|
return (await action({
|
|
request: new Request("https://admin.example.test/email-sequences/seq-1", {
|
|
method: "POST",
|
|
body,
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
}),
|
|
params: { id: "seq-1" },
|
|
context: {},
|
|
unstable_pattern: "admin/email-sequences/:id",
|
|
} as ActionFunctionArgs)) as Response;
|
|
}
|
|
|
|
async function callAction(values: Record<string, string>) {
|
|
const response = await callActionResponse(values);
|
|
return response.json();
|
|
}
|
|
|
|
describe("admin email sequence detail action", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(verifyUserHasPermission).mockResolvedValue(undefined as never);
|
|
vi.mocked(db.emailSequence.findFirst).mockResolvedValue({
|
|
id: "seq-1",
|
|
isEnabled: false,
|
|
} as never);
|
|
vi.mocked(db.emailSequence.update).mockResolvedValue({ id: "seq-1" } as never);
|
|
});
|
|
|
|
it("localizes validation errors for visible edit-form feedback", async () => {
|
|
const result = await callAction({
|
|
name: "",
|
|
triggerEvent: "lead.captured",
|
|
stepsJson: "[]",
|
|
});
|
|
|
|
expect(result).toEqual({
|
|
ok: false,
|
|
error: "translated:admin.emailSequences.errors.nameRequired",
|
|
field: "name",
|
|
});
|
|
expect(getTranslations).toHaveBeenCalledWith(expect.any(Request));
|
|
expect(db.emailSequence.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("uses the shared conditions JSON translation key on parse failure", async () => {
|
|
const result = await callAction({
|
|
name: "Welcome",
|
|
triggerEvent: "lead.captured",
|
|
stepsJson: "[]",
|
|
conditionsJson: "{",
|
|
});
|
|
|
|
expect(result).toEqual({
|
|
ok: false,
|
|
error: "translated:admin.emailSequences.errors.conditionsJsonInvalid",
|
|
field: "conditionsJson",
|
|
});
|
|
expect(db.emailSequence.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not expose the email-sequence engine error in validation feedback", async () => {
|
|
const result = await callAction({
|
|
name: "Welcome",
|
|
triggerEvent: "lead.captured",
|
|
stepsJson: "{}",
|
|
});
|
|
|
|
expect(result).toEqual({
|
|
ok: false,
|
|
error: "translated:admin.emailSequences.errors.stepsJsonValidationFailed",
|
|
field: "stepsJson",
|
|
});
|
|
expect(db.emailSequence.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("redirects with an action-specific toast after enabling a sequence", async () => {
|
|
const response = await callActionResponse({ intent: "toggle" });
|
|
|
|
expect(response.status).toBe(302);
|
|
expect(response.headers.get("Location")).toContain(
|
|
`toast=${encodeURIComponent("admin.emailSequences.toast.enabled")}`
|
|
);
|
|
});
|
|
|
|
it("redirects with the saved toast after updating a sequence", async () => {
|
|
const response = await callActionResponse({
|
|
name: "Welcome",
|
|
triggerEvent: "lead.captured",
|
|
stepsJson: "[]",
|
|
});
|
|
|
|
expect(response.status).toBe(302);
|
|
expect(response.headers.get("Location")).toContain(
|
|
`toast=${encodeURIComponent("admin.emailSequences.toast.saved")}`
|
|
);
|
|
});
|
|
});
|