@netdata/charts
Version:
Netdata frontend SDK and chart utilities
142 lines • 5.43 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 } 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 zero/falsy values", function () {
var getColor = 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 = 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("");
});
});
});