Files
community-rule/tests/unit/beginEditingPublishedRule.test.ts
T

79 lines
2.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
createFlowStateForEditingPublishedRule,
createFlowStateMatchesPublishedRule,
publishedRuleEditWouldReplaceDraft,
} from "../../lib/create/beginEditingPublishedRule";
import type { StoredLastPublishedRule } from "../../lib/create/lastPublishedRule";
import type { CreateFlowState } from "../../app/(app)/create/types";
const published: StoredLastPublishedRule = {
id: "rule-qa",
title: "QA Tenants Union",
summary: "How we share the building.",
document: {
coreValues: [
{
chipId: "accountability",
label: "Accountability",
meaning: "We say what we will do.",
signals: "",
},
],
methodSelections: {
communication: [
{
id: "signal",
label: "Signal",
sections: {
corePrinciple: "We write it down.",
logisticsAdmin: "A shared thread.",
codeOfConduct: "No pile-ons.",
},
},
],
},
},
};
describe("publishedRuleEditWouldReplaceDraft", () => {
it("is false when there is no draft", () => {
expect(publishedRuleEditWouldReplaceDraft({}, published)).toBe(false);
});
it("is false when the draft is already this finished Rule", () => {
const state = createFlowStateForEditingPublishedRule(published);
expect(publishedRuleEditWouldReplaceDraft(state, published)).toBe(false);
expect(createFlowStateMatchesPublishedRule(state, published)).toBe(true);
});
it("is true when the draft is a different community", () => {
const state: CreateFlowState = {
title: "The CommunityRule Project",
selectedCoreValueIds: ["solidarity"],
selectedCommunicationMethodIds: ["loomio"],
};
expect(publishedRuleEditWouldReplaceDraft(state, published)).toBe(true);
});
it("is true when the draft is already marked as an edit of another Rule", () => {
const state: CreateFlowState = {
...createFlowStateForEditingPublishedRule(published),
editingPublishedRuleId: "some-other-rule",
};
expect(publishedRuleEditWouldReplaceDraft(state, published)).toBe(true);
});
});
describe("createFlowStateForEditingPublishedRule", () => {
it("loads the finished Rule's own name and cards, not leftover draft fields", () => {
const state = createFlowStateForEditingPublishedRule(published);
expect(state.title).toBe("QA Tenants Union");
expect(state.editingPublishedRuleId).toBe("rule-qa");
expect(state.selectedCommunicationMethodIds).toEqual(["signal"]);
expect(state.selectedOrganizationTypeIds).toBeUndefined();
expect(state.communityAvatarUrl).toBeUndefined();
expect(state.sections).toEqual([]);
});
});