Files
template-app/modules/catalog/__tests__/i18n.test.ts
T
2026-11-14 09:56:45 +00:00

95 lines
3.5 KiB
TypeScript

// Every string a page of the module shows is a message in English and in Lithuanian, and no component of
// the module carries a string of its own.
import { readdirSync, readFileSync } from "node:fs"
import { join } from "node:path"
import { buildRegistry } from "@conusai-kit/core"
import { createI18n, lintUiStrings } from "@conusai-kit/i18n"
import { tenancyModule } from "@conusai-kit/tenancy"
import { describe, expect, it } from "vitest"
import { catalogModule } from "../module.js"
import { importFormLabels, importListLabels, productListLabels } from "../ui/labels.js"
// The messages as an app that turned the module on has them: from the manifest, flattened by the kit.
const catalogues = buildRegistry([tenancyModule, catalogModule]).catalogues
const en = catalogues.en?.catalog ?? {}
const lt = catalogues.lt?.catalog ?? {}
function keysUsed(): string[] {
const asked: string[] = []
const t = (key: string) => {
asked.push(key)
return key
}
productListLabels(t, "failed", {})
importListLabels(t, 2)
importFormLabels(t, "failed", {})
return asked
}
describe("the catalog catalogues", () => {
it("hold the same keys in English and in Lithuanian, none empty", () => {
expect(Object.keys(lt).sort()).toEqual(Object.keys(en).sort())
for (const [key, text] of [...Object.entries(en), ...Object.entries(lt)]) expect(text.trim(), key).not.toBe("")
})
it("hold every message the labels ask for, and none that nothing asks for", () => {
const asked = keysUsed()
for (const key of asked) {
expect(en[key as keyof typeof en], `en is missing catalog.${key}`).toBeTruthy()
expect(lt[key as keyof typeof lt], `lt is missing catalog.${key}`).toBeTruthy()
}
// The pages and the navigation ask for these directly.
const direct = new Set([
"title",
"intro",
"nav.products",
"nav.imports",
"product.back",
"product.code",
"product.unit",
"product.status",
"product.group",
"product.description",
"product.revision",
"product.identifiers",
"product.none",
"statuses.active",
"statuses.archived",
"statuses.deleted",
"imports.title",
"imports.intro",
])
expect(Object.keys(en).filter((key) => !asked.includes(key) && !direct.has(key))).toEqual([])
})
it("answer the navigation label the manifest names", () => {
expect(catalogModule.ui?.nav?.map((item) => item.label)).toEqual(["catalog.title"])
})
it("count imports with the Lithuanian one, few and other forms", () => {
const count = (locale: "en" | "lt", n: number) =>
createI18n(locale, { en: { catalog: en }, lt: { catalog: lt } }).message("catalog.imports.count", { count: n })
expect(count("lt", 1)).toBe("1 importas")
expect(count("lt", 2)).toBe("2 importai")
expect(count("lt", 10)).toBe("10 importų")
expect(count("lt", 21)).toBe("21 importas")
expect(count("en", 2)).toBe("2 imports")
})
})
describe("the components", () => {
const dir = new URL("../ui/", import.meta.url).pathname
const sources = readdirSync(dir)
.filter((name) => name.endsWith(".tsx"))
.map((name) => ({ name, text: readFileSync(join(dir, name), "utf8") }))
it("carry no hard-coded user-facing string and no colour literal", () => {
expect(sources.length).toBeGreaterThanOrEqual(3)
for (const { name, text } of sources) {
expect(lintUiStrings(text, name), name).toEqual([])
expect(text, name).not.toMatch(/#[0-9a-fA-F]{3,8}\b|\brgb\(|\bhsl\(|\boklch\(/)
}
})
})