UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

100 lines 4.89 kB
import shorten, { shortForLength, shortenToWidth } from "."; describe("shorten", function () { it("returns string as-is for non-string inputs", function () { expect(shorten(null)).toBeNull(); expect(shorten(undefined)).toBeUndefined(); expect(shorten(123)).toBe(123); expect(shorten({})).toEqual({}); }); it("trims whitespace at round 0", function () { expect(shorten(" test ", 0)).toBe("test"); expect(shorten(" hello world ", 0)).toBe("hello world"); }); it("removes duplicate letters at round 1", function () { // Functions only work on compound words with separators, not single words expect(shorten("hello", 1)).toBe("hello"); expect(shorten("testing", 1)).toBe("testing"); expect(shorten("bookkeeper", 1)).toBe("bookkeeper"); }); it("removes middle vowels at round 2", function () { // Functions only work on compound words with separators, not single words expect(shorten("hello", 2)).toBe("hello"); expect(shorten("testing", 2)).toBe("testing"); expect(shorten("a", 2)).toBe("a"); // too short expect(shorten("ab", 2)).toBe("ab"); // too short expect(shorten("abc", 2)).toBe("abc"); // too short }); it("keeps numbers intact when removing vowels", function () { expect(shorten("test123", 2)).toBe("test123"); // contains digit expect(shorten("hello", 2)).toBe("hello"); // no separators, no transformation }); it("creates ellipsis in middle for round > 2", function () { expect(shorten("very long string", 10)).toBe("ver...ing"); expect(shorten("short", 10)).toBe("..."); // short strings get truncated to just ellipsis }); it("handles compound words with separators for round 1", function () { expect(shorten("hello-world", 1)).toBe("helo-world"); // removes duplicate 'l' expect(shorten("bookkeeper-test", 1)).toBe("bokeper-test"); // removes duplicates }); it("handles compound words with separators for round 2", function () { expect(shorten("hello-world", 2)).toBe("hllo-world"); // only first part gets transformed expect(shorten("testing_case", 2)).toBe("tstng_case"); // only first part gets transformed }); }); describe("shortForLength", function () { it("returns string as-is for non-string inputs", function () { expect(shortForLength(null)).toBeNull(); expect(shortForLength(undefined)).toBeUndefined(); expect(shortForLength(123)).toBe(123); }); it("returns string as-is when under max length", function () { expect(shortForLength("short", 30)).toBe("short"); expect(shortForLength("medium length", 20)).toBe("medium length"); }); it("progressively shortens long strings", function () { var longString = "this is a very long string that needs shortening"; var result = shortForLength(longString, 20); expect(result.length).toBeLessThanOrEqual(20); expect(result).toBeTruthy(); }); it("uses default max length of 30", function () { var longString = "this is a very long string that definitely exceeds thirty characters"; var result = shortForLength(longString); expect(result.length).toBeLessThanOrEqual(30); }); it("handles extreme shortening with ellipsis", function () { var veryLongString = "extremely long string with many repeated letters and vowels"; var result = shortForLength(veryLongString, 10); expect(result.length).toBeLessThanOrEqual(10); expect(result).toContain("..."); }); it("terminates instead of hanging for maxLength below the ellipsis floor", function () { expect(shortForLength("systemd-journald-eu-west-1b", 0)).toBe("..."); expect(shortForLength("systemd-journald-eu-west-1b", 1)).toBe("..."); expect(shortForLength("systemd-journald-eu-west-1b", 2)).toBe("..."); }); }); describe("shortenToWidth", function () { it("returns text as-is for non-string inputs", function () { expect(shortenToWidth(null, 100, 50)).toBeNull(); expect(shortenToWidth(undefined, 100, 50)).toBeUndefined(); expect(shortenToWidth("", 100, 50)).toBe(""); }); it("returns text unchanged when it already fits", function () { expect(shortenToWidth("nginx", 40, 100)).toBe("nginx"); expect(shortenToWidth("nginx", 100, 100)).toBe("nginx"); }); it("returns text unchanged when content has no measurable width", function () { expect(shortenToWidth("systemd-journald-eu-west-1b", 0, 0)).toBe("systemd-journald-eu-west-1b"); }); it("shortens text that overflows its container", function () { var text = "systemd-journald-audit.service@cgroup_pod_1234567890-eu-west-1b"; var result = shortenToWidth(text, 600, 150); expect(result.length).toBeLessThan(text.length); expect(result).toContain("..."); }); it("never produces output shorter than the ellipsis floor", function () { var result = shortenToWidth("systemd-journald-eu-west-1b", 600, 1); expect(result).toBe("..."); }); });