import { describe, expect, it } from "vitest"; import { fragmentDefinition, inBundle, messageBasenames, propertyDefinition, templatesRoot, thymeleafReferenceAt, thymeleafReferences, unresolvedThymeleafReferences, } from "./references"; const at = (text: string, word: string, n = 0) => { let i = -1; for (let k = 0; k <= n; k++) i = text.indexOf(word, i + 1); return thymeleafReferenceAt(text, i + 1); }; describe("thymeleafReferences", () => { it("finds fragment expressions with and without ~{}", () => { const text = `
`; expect(at(text, "fragments/nav")).toMatchObject({ kind: "template", template: "fragments/nav" }); expect(at(text, "menu")).toMatchObject({ kind: "fragment", template: "fragments/nav", fragment: "menu" }); expect(at(text, "footer", 1)).toMatchObject({ kind: "fragment", template: "fragments/footer", fragment: "footer" }); expect(at(text, "layout")).toMatchObject({ kind: "template", template: "layout" }); expect(at(text, "local")).toMatchObject({ kind: "fragment", template: "", fragment: "local" }); expect(at(text, "other")).toMatchObject({ kind: "fragment", template: "", fragment: "other" }); const menu = thymeleafReferences(text).find((r) => r.kind === "fragment")!; expect(text.slice(menu.from, menu.to)).toBe("menu"); }); it("finds message keys in attributes and inline expressions", () => { const text = `

x

[[#{home.intro}]] [(#{raw-html})]

`; expect(at(text, "home.title")).toMatchObject({ kind: "message", key: "home.title" }); expect(at(text, "home.intro")).toMatchObject({ kind: "message", key: "home.intro" }); expect(at(text, "raw-html")).toMatchObject({ kind: "message", key: "raw-html" }); }); it("ignores expressions outside Thymeleaf attributes and dynamic template names", () => { expect(thymeleafReferences(`

~{b :: c}

`)).toEqual([]); expect(thymeleafReferences(`
`)).toEqual([]); expect(thymeleafReferences(`
`)).toEqual([]); }); }); describe("definitions", () => { it("finds a fragment by th:fragment, then by id", () => { const text = `