@netdata/charts
Version:
Netdata frontend SDK and chart utilities
144 lines (143 loc) • 5.77 kB
JavaScript
;
var _heatmap = require("./heatmap");
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); }
describe("heatmap utilities", function () {
describe("heatmapTypes", function () {
it("exports correct heatmap types", function () {
expect(_heatmap.heatmapTypes["default"]).toBe("default");
expect(_heatmap.heatmapTypes.disabled).toBe("disabled");
expect(_heatmap.heatmapTypes.incremental).toBe("incremental");
});
});
describe("isHeatmap", function () {
it("returns true for heatmap string", function () {
expect((0, _heatmap.isHeatmap)("heatmap")).toBe(true);
});
it("returns false for non-heatmap string", function () {
expect((0, _heatmap.isHeatmap)("line")).toBe(false);
});
it("returns true for chart object with heatmap type", function () {
var chart = {
getAttribute: jest.fn(function () {
return "heatmap";
})
};
expect((0, _heatmap.isHeatmap)(chart)).toBe(true);
});
it("returns false for chart object with non-heatmap type", function () {
var chart = {
getAttribute: jest.fn(function () {
return "line";
})
};
expect((0, _heatmap.isHeatmap)(chart)).toBe(false);
});
it("handles null/undefined input", function () {
expect((0, _heatmap.isHeatmap)(null)).toBe(false);
expect((0, _heatmap.isHeatmap)(undefined)).toBe(false);
});
});
describe("isIncremental", function () {
it("returns true for incremental heatmap", function () {
var chart = {
getAttribute: jest.fn(function (attr) {
if (attr === "chartType") return "heatmap";
if (attr === "heatmapType") return "incremental";
})
};
expect((0, _heatmap.isIncremental)(chart)).toBe(true);
});
it("returns false for non-incremental heatmap", function () {
var chart = {
getAttribute: jest.fn(function (attr) {
if (attr === "chartType") return "heatmap";
if (attr === "heatmapType") return "default";
})
};
expect((0, _heatmap.isIncremental)(chart)).toBe(false);
});
it("returns false for non-heatmap", function () {
var chart = {
getAttribute: jest.fn(function () {
return "line";
})
};
expect((0, _heatmap.isIncremental)(chart)).toBe(false);
});
});
describe("heatmapOrChartType", function () {
it("returns heatmap for matching id patterns", function () {
var ids = ["temp_10", "temp_20.5", "temp_+Inf"];
var result = (0, _heatmap.heatmapOrChartType)(ids, "line");
expect(result).toBe("heatmap");
});
it("returns original chartType for non-matching patterns", function () {
var ids = ["temp", "value", "count"];
var result = (0, _heatmap.heatmapOrChartType)(ids, "line");
expect(result).toBe("line");
});
it("returns original chartType for empty array", function () {
var result = (0, _heatmap.heatmapOrChartType)([], "line");
expect(result).toBe("line");
});
it("returns original chartType for non-array input", function () {
var result = (0, _heatmap.heatmapOrChartType)("not-array", "line");
expect(result).toBe("line");
});
it("handles mixed matching and non-matching ids", function () {
var ids = ["temp_10", "invalid"];
var result = (0, _heatmap.heatmapOrChartType)(ids, "line");
expect(result).toBe("line");
});
});
describe("makeGetColor", function () {
var mockChart;
beforeEach(function () {
mockChart = {
getAttribute: jest.fn(function () {
return 800;
})
};
});
it("returns color function", function () {
var getColor = (0, _heatmap.makeGetColor)(mockChart);
expect(_typeof(getColor)).toBe("function");
});
it("returns transparent for zero/falsy values", function () {
var getColor = (0, _heatmap.makeGetColor)(mockChart);
expect(getColor(0)).toBe("transparent");
expect(getColor(null)).toBe("transparent");
expect(getColor(undefined)).toBe("transparent");
expect(getColor(false)).toBe("transparent");
});
it("returns color for valid values", function () {
var getColor = (0, _heatmap.makeGetColor)(mockChart);
var color = getColor(100);
expect(color).toMatch(/rgba?\(\d+,\s*\d+,\s*\d+/);
});
it("uses custom opacity", function () {
var getColor = (0, _heatmap.makeGetColor)(mockChart, 0.5);
var color = getColor(100);
expect(color).toContain("0.5");
});
});
describe("withoutPrefix", function () {
it("removes prefix from heatmap labels", function () {
expect((0, _heatmap.withoutPrefix)("temperature_10")).toBe("10");
expect((0, _heatmap.withoutPrefix)("value_20.5")).toBe("20.5");
expect((0, _heatmap.withoutPrefix)("data_+Inf")).toBe("+Inf");
expect((0, _heatmap.withoutPrefix)("metric_+inf")).toBe("+inf");
});
it("returns unchanged for non-matching patterns", function () {
expect((0, _heatmap.withoutPrefix)("simple")).toBe("simple");
expect((0, _heatmap.withoutPrefix)("no_number")).toBe("no_number");
});
it("handles null/undefined input", function () {
expect((0, _heatmap.withoutPrefix)(null)).toBe(null);
expect((0, _heatmap.withoutPrefix)(undefined)).toBe(undefined);
});
it("handles empty string", function () {
expect((0, _heatmap.withoutPrefix)("")).toBe("");
});
});
});