70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { revalidatePath } from "next/cache";
|
|
import { beforeEach, expect, it, vi } from "vitest";
|
|
import { requirePermission } from "@/lib/admin/guard";
|
|
import { createTeam, deleteTeam } from "./admin-teams";
|
|
|
|
const { execute } = vi.hoisted(() => ({ execute: vi.fn() }));
|
|
vi.mock("@/features/housekeeping/domains/people/services/mutations", () => ({
|
|
createPeopleMutationInvocation: vi.fn((staff, correlationId) => ({
|
|
expectedActorId: staff.id,
|
|
correlationId,
|
|
legacy: true,
|
|
})),
|
|
peopleMutationService: { execute },
|
|
}));
|
|
vi.mock("@/lib/admin/guard", () => ({ requirePermission: vi.fn() }));
|
|
vi.mock("@/lib/permissions", () => ({
|
|
PERMS: { USERS_EDIT: "admin.users.edit" },
|
|
}));
|
|
vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
|
|
|
|
const form = (data: Record<string, string>) =>
|
|
({ get: (key: string) => data[key] ?? null }) as FormData;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(requirePermission).mockResolvedValue({
|
|
id: 1,
|
|
rank: 7,
|
|
username: "admin",
|
|
});
|
|
execute.mockResolvedValue({
|
|
ok: true,
|
|
data: { before: null, after: {} },
|
|
correlationId: "team",
|
|
});
|
|
});
|
|
|
|
it("preserves create and delete team payloads plus /admin revalidation", async () => {
|
|
await createTeam(form({ rankName: "Moderator" }));
|
|
await deleteTeam(form({ id: "42" }));
|
|
expect(execute.mock.calls.map((call) => [call[1], call[2]])).toEqual([
|
|
[
|
|
"team.change",
|
|
{
|
|
action: "create",
|
|
rankName: "Moderator",
|
|
badge: "",
|
|
jobDescription: "",
|
|
staffColor: "#327fa8",
|
|
hiddenRank: false,
|
|
},
|
|
],
|
|
["team.change", { action: "delete", teamId: "42" }],
|
|
]);
|
|
expect(revalidatePath).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("preserves empty rank name as a no-op", async () => {
|
|
await createTeam(form({ rankName: "" }));
|
|
expect(execute).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("preserves a team ID above Number.MAX_SAFE_INTEGER", async () => {
|
|
await deleteTeam(form({ id: "9007199254740993" }));
|
|
expect(execute).toHaveBeenCalledWith(expect.anything(), "team.change", {
|
|
action: "delete",
|
|
teamId: "9007199254740993",
|
|
});
|
|
});
|