@netdata/charts
Version:
Netdata frontend SDK and chart utilities
191 lines (190 loc) • 8.24 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 null/undefined values", function () {
var getColor = (0, _heatmap.makeGetColor)(mockChart);
expect(getColor(null)).toBe("transparent");
expect(getColor(undefined)).toBe("transparent");
});
it("returns transparent for zero value", function () {
var getColor = (0, _heatmap.makeGetColor)(mockChart);
expect(getColor(0)).toBe("transparent");
});
it("uses the low-end color when the heatmap max is zero", function () {
mockChart.getAttribute = jest.fn(function () {
return 0;
});
var getColor = (0, _heatmap.makeGetColor)(mockChart);
expect(getColor(0)).toBe("transparent");
expect(getColor(null)).toBe("transparent");
expect(getColor(undefined)).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("");
});
});
describe("cropHeatmapZeroEdges", function () {
var makeIsZeroOnly = function makeIsZeroOnly(zeroIds) {
return function (id) {
return zeroIds.includes(id);
};
};
it("crops only zero buckets from the bottom and top edges", function () {
var ids = ["0", "1", "2", "3", "4", "5", "6"];
expect((0, _heatmap.cropHeatmapZeroEdges)(ids, makeIsZeroOnly(["0", "1", "6"]))).toEqual(["1", "2", "3", "4", "5", "6"]);
});
it("keeps interior zero buckets", function () {
var ids = ["0", "1", "2", "3", "4", "5", "6", "7", "8"];
expect((0, _heatmap.cropHeatmapZeroEdges)(ids, makeIsZeroOnly(["0", "1", "3", "7", "8"]))).toEqual(["1", "2", "3", "4", "5", "6", "7"]);
});
it("keeps one zero bucket below and above a five-bucket non-zero span", function () {
var ids = ["0", "1", "2", "3", "4", "5", "6", "7", "8"];
expect((0, _heatmap.cropHeatmapZeroEdges)(ids, makeIsZeroOnly(["0", "1", "7", "8"]))).toEqual(["1", "2", "3", "4", "5", "6", "7"]);
});
it("expands around a narrow non-zero range to keep at least five buckets", function () {
var ids = ["0", "1", "2", "3", "4", "5", "6", "7", "8"];
var zeroIds = ["0", "1", "2", "4", "5", "6", "7", "8"];
expect((0, _heatmap.cropHeatmapZeroEdges)(ids, makeIsZeroOnly(zeroIds))).toEqual(["1", "2", "3", "4", "5"]);
});
it("keeps the full scale when all buckets are zero", function () {
var ids = ["0", "1", "2", "3", "4", "5", "6"];
expect((0, _heatmap.cropHeatmapZeroEdges)(ids, makeIsZeroOnly(ids))).toEqual(ids);
});
it("does not crop when there are fewer than the minimum visible buckets", function () {
var ids = ["0", "1", "2"];
expect((0, _heatmap.cropHeatmapZeroEdges)(ids, makeIsZeroOnly(["0", "2"]))).toEqual(ids);
});
it("does not crop without a zero-only predicate", function () {
var ids = ["0", "1", "2", "3", "4", "5"];
expect((0, _heatmap.cropHeatmapZeroEdges)(ids)).toEqual(ids);
});
});
});