test.build/core/formatters/icons.formatter.test.ts

test.build/core/formatters/icons.formatter.test.ts is a file in Codebase Testing. 39 lines of code and 0 definitions.

import { FONT_ROUTE, SUBSET_FILE } from "@banes-lab/build-scripts/configuration/constants/icons.constants.ts";
import { describe, expect, it } from "vitest";
import {
    glyphTextOf,
    renderIconPaths,
    renderStylesheet,
} from "@banes-lab/build-scripts/core/formatters/icons.formatter.ts";

const ICON_PREFIX = "bi-";
const SWAP = "font-display: swap;";
const UNKNOWN = "no-such-glyph-anywhere";

describe("renderStylesheet", () => {
    it("declares the swapped face once and one glyph rule per name, nothing else", () => {
        const css = renderStylesheet(["x", "check2"]);
        expect(css.split(SWAP)).toHaveLength(2);
        expect(css).toContain(`${FONT_ROUTE}${SUBSET_FILE}`);
        expect(css).toContain(`.${ICON_PREFIX}x::before`);
        expect(css).toContain(`.${ICON_PREFIX}check2::before`);
        expect(css.split("::before {")).toHaveLength(4);
    });

    it("refuses a name the icon font has no glyph for", () => {
        expect(() => renderStylesheet([UNKNOWN])).toThrow(UNKNOWN);
    });
});

describe("glyphTextOf", () => {
    it("spells one character per declared icon and refuses an unknown one", () => {
        const glyphs = new Intl.Segmenter().segment(glyphTextOf(["x", "check2"]));
        expect([...glyphs]).toHaveLength(2);
        expect(() => glyphTextOf([UNKNOWN])).toThrow(UNKNOWN);
    });
});

describe("renderIconPaths", () => {
    it("maps each prefixed icon name to its path data and fill rule, typed by the web member", () => {
        const paths = [{ d: "M0 0h16v16H0z", evenOdd: true }];
        const module = renderIconPaths([["x", paths]]);
        expect(module).toContain(`["${ICON_PREFIX}x", ${JSON.stringify(paths)}]`);
        expect(module).toContain("export const ICON_PATHS: ReadonlyMap<string, readonly IconPath[]>");
        expect(module.startsWith("import type { IconPath }")).toBe(true);
    });
});