UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

212 lines (211 loc) 6.7 kB
"use strict"; var _heatmap = _interopRequireDefault(require("./heatmap")); var _testUtilities = require("@jest/testUtilities"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } describe("heatmap plotter", function () { var chartUI; var plotter; var ctx; beforeEach(function () { var _makeTestChart = (0, _testUtilities.makeTestChart)({ attributes: { chartType: "heatmap", dimensionIds: ["dim1", "dim2", "dim3"] } }), chart = _makeTestChart.chart; ctx = { fillStyle: "black", strokeStyle: "black", fillRect: jest.fn(), strokeRect: jest.fn() }; plotter = { seriesIndex: 0, drawingContext: ctx, allSeriesPoints: [[{ canvasx: 10 }, { canvasx: 20 }, { canvasx: 30 }], [{ canvasx: 15 }, { canvasx: 25 }, { canvasx: 35 }], [{ canvasx: 18 }, { canvasx: 28 }, { canvasx: 38 }]], dygraph: { layout_: { setNames: ["dim1", "dim2", "dim3"] }, toDomYCoord: jest.fn(function (index) { return 50 + index * 20; }) } }; chartUI = { chart: chart }; jest.spyOn(chart, "getVisibleDimensionIds").mockReturnValue(["dim1", "dim2", "dim3"]); jest.spyOn(chart, "getDimensionIndex").mockImplementation(function (name) { var _map$name; var map = { dim1: 0, dim2: 1, dim3: 2 }; return (_map$name = map[name]) !== null && _map$name !== void 0 ? _map$name : -1; }); jest.spyOn(chart, "getHeatmapYIndex").mockImplementation(function (name) { return chart.getDimensionIndex(name); }); jest.spyOn(chart, "getDimensionValue").mockReturnValue(42); jest.spyOn(chart, "getAttribute").mockImplementation(function (attr) { if (attr === "max") return 100; return undefined; }); }); it("returns early if no chartUI", function () { var plotterFunc = (0, _heatmap["default"])(null); expect(function () { return plotterFunc(plotter); }).not.toThrow(); expect(ctx.fillRect).not.toHaveBeenCalled(); }); it("returns early if seriesIndex is not 0", function () { plotter.seriesIndex = 1; var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(ctx.fillRect).not.toHaveBeenCalled(); }); it("renders heatmap rectangles for visible dimensions", function () { var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(ctx.fillRect).toHaveBeenCalled(); expect(ctx.strokeRect).toHaveBeenCalled(); }); it("calculates bar width from minimum separation", function () { var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(ctx.fillRect).toHaveBeenCalledWith(expect.any(Number), expect.any(Number), 10, expect.any(Number)); }); it("skips series with unknown dimension index", function () { chartUI.chart.getHeatmapYIndex.mockReturnValue(-1); var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(ctx.fillRect).not.toHaveBeenCalled(); }); it("draws rows using sorted heatmap y-index while reading values by series id", function () { plotter.allSeriesPoints = [[{ canvasx: 10 }, { canvasx: 20 }], [{ canvasx: 10 }, { canvasx: 20 }], [{ canvasx: 10 }, { canvasx: 20 }]]; plotter.dygraph.layout_.setNames = ["+Inf", "0.3", "2"]; plotter.dygraph.toDomYCoord = jest.fn(function (index) { return 50 + index * 20; }); chartUI.chart.getHeatmapYIndex.mockImplementation(function (id) { var _indexes$id; var indexes = { "0.3": 0, 2: 1, "+Inf": 2 }; return (_indexes$id = indexes[id]) !== null && _indexes$id !== void 0 ? _indexes$id : -1; }); chartUI.chart.getDimensionValue.mockImplementation(function (id) { var values = { "0.3": 1, 2: 2, "+Inf": 3 }; return values[id]; }); var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(ctx.fillRect.mock.calls[0][1]).toBe(80); expect(ctx.fillRect.mock.calls[2][1]).toBe(40); expect(ctx.fillRect.mock.calls[4][1]).toBe(60); expect(chartUI.chart.getDimensionValue).toHaveBeenCalledWith("+Inf", 0, { allowNull: true }); expect(chartUI.chart.getDimensionValue).toHaveBeenCalledWith("0.3", 0, { allowNull: true }); expect(chartUI.chart.getDimensionValue).toHaveBeenCalledWith("2", 0, { allowNull: true }); }); it("reads values by dygraph payload row index when points are clipped", function () { plotter.allSeriesPoints = [[{ canvasx: 10, idx: 4 }, { canvasx: 20, idx: 5 }]]; plotter.dygraph.layout_.setNames = ["dim1"]; var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(chartUI.chart.getDimensionValue).toHaveBeenCalledWith("dim1", 4, { allowNull: true }); expect(chartUI.chart.getDimensionValue).toHaveBeenCalledWith("dim1", 5, { allowNull: true }); }); it("uses color based on dimension value", function () { var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(ctx.fillStyle).toContain("rgb("); expect(ctx.strokeStyle).toBe("transparent"); }); it("calculates height based on series position", function () { plotter.dygraph.toDomYCoord = jest.fn(function (index) { if (index === 0) return 50; if (index === 1) return 70; if (index === 2) return 90; return 110; }); var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(ctx.fillRect).toHaveBeenCalledWith(expect.any(Number), expect.any(Number), expect.any(Number), 20); }); it("handles empty allSeriesPoints", function () { plotter.allSeriesPoints = []; plotter.dygraph.layout_.setNames = []; var plotterFunc = (0, _heatmap["default"])(chartUI); expect(function () { return plotterFunc(plotter); }).not.toThrow(); expect(ctx.fillRect).not.toHaveBeenCalled(); }); it("handles two data points", function () { plotter.allSeriesPoints = [[{ canvasx: 10 }, { canvasx: 110 }]]; plotter.dygraph.layout_.setNames = ["dim1"]; var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(ctx.fillRect).toHaveBeenCalledWith(expect.any(Number), expect.any(Number), 100, expect.any(Number)); }); });