muddle/tests/templates.test.ts
2026-10-23 08:53:07 -04:00

65 lines
2.4 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { join, resolve } from "node:path";
import { runTemplateTests } from "../views/testing.ts";
import { createViews } from "../views/api.ts";
import { parse } from "../views/engine/parser.ts";
import { lex } from "../views/engine/lexer.ts";
import { compileTemplate } from "../views/engine/compiler.ts";
import { registerAmezing } from "../modules/amezing/index.ts";
const root = resolve(import.meta.dir, "..");
const sample = join(root, "samples/hello-world");
describe("template tests", () => {
test("SPUG anchors emit generated relation identities", () => {
const nodes = parse(lex("nav\n ul\n li@hello1() First\n li@row(item.id) Second"));
const firstAnchor = nodes.nodes[0]?.kind === "element"
? nodes.nodes[0].children[0]?.kind === "element"
? nodes.nodes[0].children[0].children[0]
: undefined
: undefined;
expect(firstAnchor).toMatchObject({ kind: "element", anchor: { name: "hello1" } });
const compiled = compileTemplate(nodes.nodes, { path: "/tmp/relations.spug", dir: "/tmp" });
expect(compiled.source).toContain('data-muddle-anchor');
expect(compiled.source).toContain('"hello1"');
expect(compiled.source).toContain('"row" + String(item.id)');
expect(compiled.source).not.toContain("__muddleRelations");
});
test("SPUG derives a dotted anchor leaf from composition scope", () => {
const nodes = parse(lex("a@docs.{section.id}(href='#' + section.id) Link"));
const compiled = compileTemplate(nodes.nodes, { path: "/tmp/reflective.spug", dir: "/tmp" });
expect(compiled.source).toContain('"docs." + String(section.id)');
});
test(
"the assertions of the templates pass",
async () => {
const views = createViews({ viewsDir: join(sample, "modules/app/views") });
registerAmezing(views);
const run = await runTemplateTests({
root: sample,
viewRoots: [join(sample, "modules/app/views")],
components: views.components,
locals: {
title: "muddle",
nav: [{ label: "Home", href: "/" }],
posts: [{ id: "test1", title: "Test1", excerpt: "Test1" }],
post: { id: "test1", title: "Test1", excerpt: "Test1" },
},
});
if (run.skipped) {
console.log(`muddle: template tests skipped, ${run.reason}`);
return;
}
expect(run.tested.length).toBeGreaterThan(0);
for (const failure of run.failures) {
console.error(`muddle: ${failure.key}\n${failure.error.message}`);
}
expect(run.failures).toEqual([]);
},
30_000,
);
});