import { describe, expect, it } from "vitest"; import { deckData, templateDeck } from "../../lib/create/deckRecommend"; import { isTemplateCreatePath, orderIdsFirst, templateCardIdsForDeck, templateChipCounts, templatePathFromState, templateValueIdsForSlug, } from "../../lib/create/templatePath"; describe("templatePathFromState", () => { it("reads a customize or use-without-changes pin", () => { expect(templatePathFromState({})).toBeNull(); expect( templatePathFromState({ pendingTemplateAction: { slug: "consensus", mode: "customize" }, }), ).toEqual({ slug: "consensus", mode: "customize" }); expect( isTemplateCreatePath({ pendingTemplateAction: { slug: "consensus", mode: "useWithoutChanges", }, }), ).toBe(true); }); it("ignores a pin with no slug or an unknown mode", () => { expect( templatePathFromState({ pendingTemplateAction: { slug: " ", mode: "customize" }, }), ).toBeNull(); expect( templatePathFromState({ pendingTemplateAction: { slug: "consensus", mode: "other" as "customize", }, }), ).toBeNull(); }); }); describe("template chips", () => { it("returns the consensus template's values and communication cards", () => { expect(templateValueIdsForSlug("consensus")).toEqual([ "consensus", "community-care", "horizontalism", ]); expect(templateCardIdsForDeck("consensus", "communication")).toEqual([ "how-we-run-meetings", "in-person-meetings", "loomio", ]); expect(templateChipCounts("consensus")).toEqual({ values: 3, cards: 10, }); }); it("returns null counts for an unknown slug", () => { expect(templateChipCounts("not-a-template")).toBeNull(); }); }); describe("orderIdsFirst", () => { it("puts named ids first and keeps the rest in their original order", () => { const items = [{ id: "a" }, { id: "b" }, { id: "c" }, { id: "d" }]; expect(orderIdsFirst(items, ["c", "a", "missing"])).toEqual([ { id: "c" }, { id: "a" }, { id: "b" }, { id: "d" }, ]); }); }); describe("templateDeck", () => { it("puts the template's cards first, then the rest of the deck, with no cut", () => { const data = deckData("communication"); const firstIds = templateCardIdsForDeck("consensus", "communication"); const ordered = templateDeck(data, firstIds); expect(ordered.first).toEqual(firstIds); expect(ordered.rest.some((id) => firstIds.includes(id))).toBe(false); const all = [...ordered.first, ...ordered.rest]; expect(new Set(all).size).toBe(all.length); expect(all.length).toBe(Object.keys(data.engine.cards).length); }); });