test.rules/rules/eslint/markup-through-builder.eslint.rule.test.ts

test.rules/rules/eslint/markup-through-builder.eslint.rule.test.ts is a file in Codebase Testing. 45 lines of code and 0 definitions.

import { describe, expect, it } from "vitest";
import { Linter } from "eslint";
import { MARKUP_BUILDERS } from "@ssot/govlab/shared/manifests/invariant.manifest.ts";
import { absolutePath } from "@ssot/paths";
import rule from "@ssot/govlab/rules/eslint/markup-through-builder.eslint.rule.ts";
import { sep } from "node:path";

const linter = new Linter();
const config = [
    {
        files: ["**/*.ts"],
        languageOptions: {
            ecmaVersion: 2025 as const,
            parser: await import("@typescript-eslint/parser"),
            sourceType: "module" as const,
        },
        plugins: { t: { rules: { r: rule } } },
        rules: { "t/r": "error" as const },
    },
];

const ROOT = absolutePath("govlab.patterns").split(sep).join("/");
const CODE_FILE = `${ROOT}/core/converters/probe.converter.ts`;
const COPY_FILE = `${ROOT}/configuration/strings/probe.strings.ts`;

const idsFor = function idsFor(code: string, file = CODE_FILE): string[] {
    return linter
        .verify(code, config, file)
        .map((message) => message.messageId)
        .filter((id): id is string => typeof id === "string");
};

const [entry] = [...MARKUP_BUILDERS];
const [opening, builder] = entry ?? ["", ""];
const BUILT = `export const link = (href: string, label: string): string => \`${opening}"\${href}">\${label}</a>\`;`;

describe("markup-through-builder", () => {
    it("reports a template that interpolates values into a registered element outside its builder", () => {
        expect(idsFor(BUILT)).toStrictEqual(["inlineMarkup"]);
    });

    it("accepts the template inside the file that declares the builder", () => {
        const code = `export const ${builder} = function ${builder}(href: string, label: string): string {\n    return \`${opening}"\${href}">\${label}</a>\`;\n};`;
        expect(idsFor(code)).toStrictEqual([]);
    });

    it("accepts static markup, a call to the builder and copy in a strings module", () => {
        expect(idsFor(`export const plain = \`${opening}"/a">A</a>\`;`)).toStrictEqual([]);
        expect(idsFor(`import { ${builder} } from "./link.formatter.ts";\nexport const a = ${builder}("/a", "A");`)).toStrictEqual([]);
        expect(idsFor(BUILT, COPY_FILE)).toStrictEqual([]);
    });
});