UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

141 lines (140 loc) 4.53 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, "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.getDimensionIndex.mockReturnValue(-1); var plotterFunc = (0, _heatmap["default"])(chartUI); plotterFunc(plotter); expect(ctx.fillRect).not.toHaveBeenCalled(); }); 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)); }); });