/** * Unit tests for buildContactLinksHtml (contact links HTML builder). */ import { describe, it, expect } from "vitest"; import { buildContactLinksHtml } from "./contactHtml.js"; describe("buildContactLinksHtml", () => { const baseOptions = { classPrefix: "test-contact", showLabels: true, separator: " " }; it("returns empty string when phone and username are missing", () => { expect(buildContactLinksHtml("en", null, null, baseOptions)).toBe(""); expect(buildContactLinksHtml("en", undefined, undefined, baseOptions)).toBe(""); expect(buildContactLinksHtml("en", "", "", baseOptions)).toBe(""); expect(buildContactLinksHtml("en", " ", " ", baseOptions)).toBe(""); }); it("renders phone only with label and tel: link", () => { const html = buildContactLinksHtml("en", "+79991234567", null, baseOptions); expect(html).toContain("test-contact-row"); expect(html).toContain('href="tel:'); expect(html).toContain("+79991234567"); expect(html).toContain("Phone"); expect(html).not.toContain("t.me"); }); it("renders username only with label and t.me link", () => { const html = buildContactLinksHtml("en", null, "alice_dev", baseOptions); expect(html).toContain("test-contact-row"); expect(html).toContain("https://t.me/"); expect(html).toContain("alice_dev"); expect(html).toContain("@alice_dev"); expect(html).toContain("Telegram"); expect(html).not.toContain("tel:"); }); it("renders both phone and username with labels", () => { const html = buildContactLinksHtml("en", "+79001112233", "bob", baseOptions); expect(html).toContain("test-contact-row"); expect(html).toContain("tel:"); expect(html).toContain("+79001112233"); expect(html).toContain("t.me"); expect(html).toContain("@bob"); expect(html).toContain("Phone"); expect(html).toContain("Telegram"); }); it("strips leading @ from username and displays with @", () => { const html = buildContactLinksHtml("en", null, "@alice", baseOptions); expect(html).toContain("https://t.me/alice"); expect(html).toContain("@alice"); expect(html).not.toContain("@@"); }); it("handles multiple leading @ in username", () => { const html = buildContactLinksHtml("en", null, "@@@user", baseOptions); expect(html).toContain("https://t.me/user"); expect(html).toContain("@user"); }); it("escapes special characters in phone in href and text", () => { const html = buildContactLinksHtml("en", '+7 999 "1" <2>', null, baseOptions); expect(html).toContain("""); expect(html).toContain("<"); expect(html).toContain(">"); expect(html).toContain("tel:"); expect(html).not.toContain("<2>"); expect(html).not.toContain('"1"'); }); it("uses custom separator when showLabels is false", () => { const html = buildContactLinksHtml("en", "+7999", "u1", { classPrefix: "duty-contact", showLabels: false, separator: " · " }); expect(html).toContain(" · "); expect(html).not.toContain("Phone"); expect(html).not.toContain("Telegram"); expect(html).toContain("duty-contact-row"); expect(html).toContain("duty-contact-link"); }); it("uses Russian labels when lang is ru", () => { const html = buildContactLinksHtml("ru", "+7999", null, baseOptions); expect(html).toContain("Телефон"); const htmlTg = buildContactLinksHtml("ru", null, "u", baseOptions); expect(htmlTg).toContain("Telegram"); }); it("uses default showLabels true and separator space when options omit them", () => { const html = buildContactLinksHtml("en", "+7999", "u", { classPrefix: "minimal", }); expect(html).toContain("Phone"); expect(html).toContain("Telegram"); expect(html).toContain("minimal-row"); expect(html).not.toContain(" · "); }); });