Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/icon pack/preview: add some tests
Adorian Doran44a2a38847fd
diff --git a/apps/client/src/widgets/type_widgets/icon_pack/IconPack.spec.tsx b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.spec.tsxnew file mode 100644index 0000000000..b4a7ed5d16--- /dev/null+++ b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.spec.tsx@@ -0,0 +1,55 @@+import { describe, expect, it } from "vitest";++import { parseManifest, resolveGlyph } from "./IconPack";++// U+E964 is the code point used across the escape-form assertions below.+const E964 = String.fromCodePoint(0xe964);++describe("resolveGlyph", () => {+ it("converts escape-string glyphs to their code point and leaves real characters untouched", () => {+ // Manifests may store the glyph as a literal escape string (CSS-style, any case), not a real char.+ expect(resolveGlyph("\\e964")).toBe(E964);+ expect(resolveGlyph("\\ue964")).toBe(E964);+ expect(resolveGlyph("\\uE964")).toBe(E964);++ // A real glyph character (boxicons-style) is returned as-is.+ const realGlyph = String.fromCodePoint(0xea3f);+ expect(resolveGlyph(realGlyph)).toBe(realGlyph);++ // Non-escape strings must not be mistaken for escapes.+ expect(resolveGlyph("")).toBe("");+ expect(resolveGlyph("bx-sushi")).toBe("bx-sushi");+ expect(resolveGlyph("\\zzzz")).toBe("\\zzzz");+ });+});++describe("parseManifest", () => {+ it("parses icons, resolving glyphs and defaulting missing or ill-typed fields", () => {+ const result = parseManifest(JSON.stringify({+ icons: {+ mat_2k_plus: { glyph: "\\e964", terms: [ "2k", "plus" ] },+ mat_bare: { glyph: String.fromCodePoint(0xea3f) }, // no terms+ mat_bad: { terms: [ "x", 5 ] } // no glyph, non-string term dropped+ }+ }));++ expect(result.ok).toBe(true);+ if (!result.ok) return;+ expect(result.icons).toEqual([+ { id: "mat_2k_plus", glyph: E964, terms: [ "2k", "plus" ] },+ { id: "mat_bare", glyph: String.fromCodePoint(0xea3f), terms: [] },+ { id: "mat_bad", glyph: "", terms: [ "x" ] }+ ]);+ });++ it("treats blank content as an empty pack and rejects malformed input", () => {+ expect(parseManifest("")).toEqual({ ok: true, icons: [] });+ expect(parseManifest(" ")).toEqual({ ok: true, icons: [] });+ expect(parseManifest(JSON.stringify({ icons: {} }))).toEqual({ ok: true, icons: [] });++ // Invalid JSON, or valid JSON without a usable `icons` object.+ expect(parseManifest("{ not json")).toEqual({ ok: false });+ expect(parseManifest(JSON.stringify({ icons: "nope" }))).toEqual({ ok: false });+ expect(parseManifest(JSON.stringify({ foo: 1 }))).toEqual({ ok: false });+ });+});diff --git a/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsx b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsxindex 9e82e95c6f..9762c1d23f 100644--- a/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsx+++ b/apps/client/src/widgets/type_widgets/icon_pack/IconPack.tsx@@ -107,7 +107,7 @@ type ParsedManifest =| { ok: true; icons: PreviewIcon[] }| { ok: false };-function parseManifest(content: string): ParsedManifest {+export function parseManifest(content: string): ParsedManifest {if (!content.trim()) return { ok: true, icons: [] };let data: unknown;@@ -134,7 +134,7 @@ function parseManifest(content: string): ParsedManifest {* (e.g. a CSS-style escape) rather than the real character; those are converted to their code* point. A value that is already a real character is returned unchanged.*/-function resolveGlyph(raw: string): string {+export function resolveGlyph(raw: string): string {const match = raw.match(/^\\u?([0-9a-fA-F]{2,6})$/);if (match) {const codePoint = parseInt(match[1], 16);