reconciliationRuleTabIndex.spec.js 1.57 KB
const {
	getReconciliationRuleIndexForTab,
	hasPhantomFullCategoryRuleSlot,
} = require("./reconciliationRuleTabIndex");

describe("对账规则 Tab 下标映射", () => {
	const phantomRule = {
		groupType: 1,
		expenseCategoryRules: [ { expenseCategoryId: "flow" } ],
	};

	it("全部品类为占位草稿时,特殊品类从 rules[0] 开始映射", () => {
		const rules = [
			{ groupType: 2, groupDivisionRuleId: "special-1" },
			{ groupType: 2, groupDivisionRuleId: "special-2" },
		];

		expect(hasPhantomFullCategoryRuleSlot(rules, phantomRule)).toBe(true);
		expect(getReconciliationRuleIndexForTab(rules, 0, phantomRule)).toBeNull();
		expect(getReconciliationRuleIndexForTab(rules, 1, phantomRule)).toBe(0);
		expect(getReconciliationRuleIndexForTab(rules, 2, phantomRule)).toBe(1);
	});

	it("删除最后一个特殊品类后 rules 为空,仍保留全部品类占位草稿", () => {
		const rules = [];

		expect(hasPhantomFullCategoryRuleSlot(rules, phantomRule)).toBe(true);
		expect(getReconciliationRuleIndexForTab(rules, 0, phantomRule)).toBeNull();
	});

	it("已有正式全部品类规则时,优先使用 rules[0]", () => {
		const rules = [ { groupType: 1, groupDivisionRuleId: "full-1" } ];

		expect(hasPhantomFullCategoryRuleSlot(rules, phantomRule)).toBe(false);
		expect(getReconciliationRuleIndexForTab(rules, 0, phantomRule)).toBe(0);
	});

	it("规则和草稿都为空时,全部品类使用可写的 rules[0] 槽位", () => {
		expect(hasPhantomFullCategoryRuleSlot([], null)).toBe(false);
		expect(getReconciliationRuleIndexForTab([], 0, null)).toBe(0);
	});
});