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