UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

236 lines 8.24 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } import { makeTestChart } from "@jest/testUtilities"; describe("makeChart", function () { var chart; var sdk; beforeEach(function () { var testChart = makeTestChart(); chart = testChart.chart; sdk = testChart.sdk; }); it("creates chart with required methods", function () { expect(chart).toHaveProperty("getUpdateEvery"); expect(chart).toHaveProperty("getDateWindow"); expect(chart).toHaveProperty("startAutofetch"); expect(chart).toHaveProperty("getUI"); expect(chart).toHaveProperty("setUI"); expect(chart).toHaveProperty("focus"); expect(chart).toHaveProperty("blur"); expect(chart).toHaveProperty("activate"); expect(chart).toHaveProperty("deactivate"); expect(chart).toHaveProperty("getUnits"); expect(chart).toHaveProperty("getThemeIndex"); expect(chart).toHaveProperty("intl"); }); it("setUI and getUI work together", function () { var mockUI = { test: true }; chart.setUI(mockUI); expect(chart.getUI()).toBe(mockUI); }); it("makeSubChart creates new chart with UI", function () { var subChart = chart.makeSubChart({ test: true }); expect(subChart).toBeDefined(); expect(_typeof(subChart.getUI)).toBe("function"); expect(_typeof(subChart.setUI)).toBe("function"); expect(subChart.getUI()).toBeDefined(); }); it("getUpdateEvery returns correct value when loaded", function () { chart.updateAttributes({ loaded: true, updateEvery: 5, viewUpdateEvery: null }); var result = chart.getUpdateEvery(); expect(result).toBe(5000); }); it("getUpdateEvery returns viewUpdateEvery when available", function () { chart.updateAttributes({ loaded: true, updateEvery: 5, viewUpdateEvery: 10 }); var result = chart.getUpdateEvery(); expect(result).toBe(10000); }); it("getUpdateEvery returns 0 when not loaded", function () { chart.updateAttributes({ loaded: false, updateEvery: 5 }); var result = chart.getUpdateEvery(); expect(result).toBe(0); }); it("keeps live relative date window anchored between payload updates", function () { chart.updateAttributes({ after: -300, before: 0, renderedAt: null, liveAnchor: 1000000 }); sdk.getRoot().setAttribute("fetchAt", 1000500); expect(chart.getDateWindow()).toEqual([700000, 1000000]); sdk.getRoot().setAttribute("fetchAt", 1001500); expect(chart.getDateWindow()).toEqual([700000, 1000000]); }); it("getThemeIndex returns correct index for theme", function () { chart.updateAttribute("theme", "dark"); var result = chart.getThemeIndex(); expect(result).toBe(1); }); it("getThemeIndex returns default for unknown theme", function () { chart.updateAttribute("theme", "unknown"); var result = chart.getThemeIndex(); expect(result).toBe(0); }); it("focus does not update when already focused and hovering", function () { chart.updateAttributes({ focused: true, hovering: true }); var spy = jest.spyOn(chart, "updateAttributes"); chart.focus({ type: "focus" }); expect(spy).not.toHaveBeenCalled(); }); it("focus updates attributes", function () { chart.updateAttribute("focused", false); chart.updateAttribute("hovering", false); chart.focus({ type: "focus" }); expect(chart.getAttribute("focused")).toBe(true); expect(chart.getAttribute("hovering")).toBe(true); }); it("blur does not update when not focused or hovering", function () { chart.updateAttributes({ focused: false, hovering: false }); var spy = jest.spyOn(chart, "updateAttributes"); chart.blur({ type: "blur" }); expect(spy).not.toHaveBeenCalled(); }); it("blur updates attributes", function () { chart.updateAttribute("focused", true); chart.updateAttribute("hovering", true); chart.blur({ type: "blur" }); expect(chart.getAttribute("focused")).toBe(false); expect(chart.getAttribute("hovering")).toBe(false); }); it("activate sets active to true", function () { chart.updateAttribute("active", false); chart.activate(); expect(chart.getAttribute("active")).toBe(true); }); it("deactivate sets active to false", function () { chart.updateAttribute("active", true); chart.deactivate(); expect(chart.getAttribute("active")).toBe(false); }); it("getUnits returns units from attributes", function () { chart.updateAttribute("units", "GB"); var units = chart.getUnits(); expect(units).toBe("GB"); }); it("keeps grouped notation while raw values remain readable", function () { chart.updateAttribute("units", ["raw-events"]); expect(chart.getConvertedValue(9999999999999)).toBe("9,999,999,999,999"); }); it("uses exponential notation for unreadably large raw values", function () { chart.updateAttribute("units", ["raw-events"]); expect(chart.getConvertedValue(6.999e99)).toBe("6.999e+99"); expect(chart.getConvertedValueWithUnit(6.999e99)).toBe("6.999e+99"); }); it("formats seconds below one microsecond as nanoseconds", function () { chart.updateAttributes({ units: ["s"], desiredUnits: ["auto"], secondsAsTime: true }); var unitAttributes = chart.getUnitAttributesForValue(25e-9); expect(unitAttributes.method).toBe("s-ns"); expect(chart.getConvertedValueWithUnit(25e-9, { unitAttributes: unitAttributes })).toBe("25 ns"); }); it("formats seconds below one millisecond with the micro sign", function () { chart.updateAttributes({ units: ["s"], desiredUnits: ["auto"], secondsAsTime: true }); var unitAttributes = chart.getUnitAttributesForValue(5e-6); expect(unitAttributes.method).toBe("s-us"); expect(chart.getUnitSign({ unitAttributes: unitAttributes })).toBe("\xB5s"); expect(chart.getConvertedValueWithUnit(5e-6, { unitAttributes: unitAttributes })).toBe("5 \xB5s"); }); it("formats hour-range seconds as compact duration without a unit suffix", function () { chart.updateAttributes({ units: ["s"], desiredUnits: ["auto"], secondsAsTime: true }); var unitAttributes = chart.getUnitAttributesForValue(33820.22); expect(unitAttributes.method).toBe("s-h:mm:ss"); expect(chart.getUnitSign({ unitAttributes: unitAttributes })).toBe(""); expect(chart.getConvertedValueWithUnit(33820.22, { unitAttributes: unitAttributes })).toBe("9h23m40s.22"); }); it("keeps high-precision latency values distinguishable by using a smaller scale", function () { chart.updateAttributes({ units: ["s"], desiredUnits: ["auto"], secondsAsTime: true }); var min = 0.00512000001; var max = 0.00512000009; var unitAttributes = chart.getUnitAttributesForValue(min, { min: min, max: max }); expect(unitAttributes.method).toBe("s-us"); expect(unitAttributes.fractionDigits).toBe(5); expect(chart.getConvertedValueWithUnit(min, { unitAttributes: unitAttributes })).toBe("5,120.00001 \xB5s"); expect(chart.getConvertedValueWithUnit(max, { unitAttributes: unitAttributes })).toBe("5,120.00009 \xB5s"); }); it("intl returns fallback when no translation", function () { var result = chart.intl("test.key", { fallback: "Default Text" }); expect(result).toBe("Default Text"); }); it("intl returns pluralized key when count > 1", function () { var result = chart.intl("item", { count: 2, pluralize: true }); expect(result).toBe("items"); }); it("intl uses translation when available", function () { chart.updateAttribute("en", { "test.key": "Translated Text" }); var result = chart.intl("test.key"); expect(result).toBe("Translated Text"); }); });