185 lines
5.0 KiB
TypeScript
185 lines
5.0 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { publishedRuleAnalyticsFromDocument } from "../../lib/create/publishedRuleAnalytics";
|
|
import {
|
|
TEAM_RULE_DRAFT_TITLE,
|
|
TEAM_RULE_TITLE,
|
|
buildAdoptedTeamRule,
|
|
} from "../../lib/create/teamRule";
|
|
import { RULE_VISIBILITY } from "../../lib/ruleVisibility";
|
|
|
|
const findManyMock = vi.fn();
|
|
const updateMock = vi.fn();
|
|
const updateManyMock = vi.fn();
|
|
const createMock = vi.fn();
|
|
|
|
import { seedTeamRule } from "../../prisma/seed/teamRule";
|
|
|
|
function prismaStub() {
|
|
return {
|
|
publishedRule: {
|
|
findMany: findManyMock,
|
|
update: updateMock,
|
|
updateMany: updateManyMock,
|
|
create: createMock,
|
|
},
|
|
};
|
|
}
|
|
|
|
function staleRow(
|
|
id: string,
|
|
title: string,
|
|
visibility: string = RULE_VISIBILITY.private,
|
|
) {
|
|
return {
|
|
id,
|
|
title,
|
|
summary: "September draft",
|
|
document: { stale: true },
|
|
visibility,
|
|
communityLocation: null,
|
|
orgType: null,
|
|
};
|
|
}
|
|
|
|
beforeEach(() => {
|
|
findManyMock.mockReset();
|
|
updateMock.mockReset();
|
|
updateManyMock.mockReset();
|
|
createMock.mockReset();
|
|
updateMock.mockResolvedValue({});
|
|
updateManyMock.mockResolvedValue({ count: 1 });
|
|
});
|
|
|
|
describe("seedTeamRule", () => {
|
|
it("replaces the old draft row in place, including the document", async () => {
|
|
const adopted = buildAdoptedTeamRule();
|
|
findManyMock.mockResolvedValueOnce([
|
|
staleRow("draft-1", TEAM_RULE_DRAFT_TITLE),
|
|
]);
|
|
|
|
const result = await seedTeamRule(prismaStub() as never);
|
|
|
|
expect(result).toEqual({
|
|
action: "updated",
|
|
id: "draft-1",
|
|
title: TEAM_RULE_TITLE,
|
|
});
|
|
expect(findManyMock).toHaveBeenCalledWith({
|
|
where: {
|
|
title: { in: [TEAM_RULE_DRAFT_TITLE, TEAM_RULE_TITLE] },
|
|
},
|
|
orderBy: { createdAt: "asc" },
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
summary: true,
|
|
document: true,
|
|
visibility: true,
|
|
communityLocation: true,
|
|
orgType: true,
|
|
},
|
|
});
|
|
expect(updateMock).toHaveBeenCalledWith({
|
|
where: { id: "draft-1" },
|
|
data: {
|
|
title: adopted.title,
|
|
summary: adopted.summary,
|
|
document: adopted.document,
|
|
visibility: RULE_VISIBILITY.public_and_featured,
|
|
...publishedRuleAnalyticsFromDocument(adopted.document),
|
|
},
|
|
});
|
|
expect(updateManyMock).not.toHaveBeenCalled();
|
|
expect(createMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("updates an already-renamed adopted row", async () => {
|
|
findManyMock.mockResolvedValueOnce([
|
|
staleRow("adopted-1", TEAM_RULE_TITLE),
|
|
]);
|
|
|
|
const result = await seedTeamRule(prismaStub() as never);
|
|
|
|
expect(result.action).toBe("updated");
|
|
expect(updateMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ where: { id: "adopted-1" } }),
|
|
);
|
|
});
|
|
|
|
it("creates the adopted Rule when neither row exists", async () => {
|
|
const adopted = buildAdoptedTeamRule();
|
|
findManyMock.mockResolvedValueOnce([]);
|
|
createMock.mockResolvedValueOnce({
|
|
id: "new-1",
|
|
title: TEAM_RULE_TITLE,
|
|
});
|
|
|
|
const result = await seedTeamRule(prismaStub() as never);
|
|
|
|
expect(result).toEqual({
|
|
action: "created",
|
|
id: "new-1",
|
|
title: TEAM_RULE_TITLE,
|
|
});
|
|
expect(createMock).toHaveBeenCalledWith({
|
|
data: {
|
|
userId: null,
|
|
title: adopted.title,
|
|
summary: adopted.summary,
|
|
document: adopted.document,
|
|
visibility: RULE_VISIBILITY.public_and_featured,
|
|
...publishedRuleAnalyticsFromDocument(adopted.document),
|
|
},
|
|
select: { id: true, title: true },
|
|
});
|
|
});
|
|
|
|
it("unpublishes a second featured row instead of leaving its document public", async () => {
|
|
findManyMock.mockResolvedValueOnce([
|
|
staleRow(
|
|
"adopted-1",
|
|
TEAM_RULE_TITLE,
|
|
RULE_VISIBILITY.public_and_featured,
|
|
),
|
|
staleRow("draft-1", TEAM_RULE_DRAFT_TITLE),
|
|
]);
|
|
|
|
const result = await seedTeamRule(prismaStub() as never);
|
|
|
|
expect(result).toEqual({
|
|
action: "updated",
|
|
id: "draft-1",
|
|
title: TEAM_RULE_TITLE,
|
|
});
|
|
expect(updateMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ where: { id: "draft-1" } }),
|
|
);
|
|
expect(updateManyMock).toHaveBeenCalledWith({
|
|
where: { id: { in: ["adopted-1"] } },
|
|
data: { visibility: RULE_VISIBILITY.private },
|
|
});
|
|
});
|
|
|
|
it("does not rewrite a row that already matches the adopted Rule", async () => {
|
|
const adopted = buildAdoptedTeamRule();
|
|
const analytics = publishedRuleAnalyticsFromDocument(adopted.document);
|
|
findManyMock.mockResolvedValueOnce([
|
|
{
|
|
id: "adopted-1",
|
|
title: adopted.title,
|
|
summary: adopted.summary,
|
|
document: adopted.document,
|
|
visibility: RULE_VISIBILITY.public_and_featured,
|
|
communityLocation: analytics.communityLocation,
|
|
orgType: analytics.orgType,
|
|
},
|
|
]);
|
|
|
|
const result = await seedTeamRule(prismaStub() as never);
|
|
|
|
expect(result.action).toBe("unchanged");
|
|
expect(updateMock).not.toHaveBeenCalled();
|
|
expect(updateManyMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|