@netdata/charts
Version:
Netdata frontend SDK and chart utilities
59 lines • 2.34 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 makeGetClosestRow from "./makeGetClosestRow";
describe("makeGetClosestRow", function () {
var mockChart;
beforeEach(function () {
mockChart = {
getPayload: jest.fn(function () {
return {
data: [[1000, 10], [2000, 20], [3000, 30], [4000, 40], [5000, 50]]
};
})
};
makeGetClosestRow(mockChart);
});
it("adds getClosestRow method to chart", function () {
expect(_typeof(mockChart.getClosestRow)).toBe("function");
});
it("adds invalidateClosestRowCache method to chart", function () {
expect(_typeof(mockChart.invalidateClosestRowCache)).toBe("function");
});
it("returns -1 for empty data", function () {
mockChart.getPayload.mockReturnValue({
data: []
});
var result = mockChart.getClosestRow(1500);
expect(result).toBe(-1);
});
it("returns first index for timestamp before data", function () {
var result = mockChart.getClosestRow(500);
expect(result).toBe(0);
});
it("returns last index for timestamp after data", function () {
var result = mockChart.getClosestRow(6000);
expect(result).toBe(4);
});
it("returns exact match when timestamp exists", function () {
var result = mockChart.getClosestRow(3000);
expect(result).toBe(2);
});
it("returns closest row for timestamp between data points", function () {
var result = mockChart.getClosestRow(2100);
expect(result).toBe(1);
});
it("caches results for same timestamp", function () {
mockChart.getClosestRow(2500);
mockChart.getClosestRow(2500);
expect(mockChart.getPayload).toHaveBeenCalledTimes(1);
});
it("invalidates cache and recalculates", function () {
mockChart.getClosestRow(2500);
mockChart.invalidateClosestRowCache();
mockChart.getClosestRow(2500);
expect(mockChart.getPayload).toHaveBeenCalledTimes(2);
});
it("finds closest when multiple equidistant points", function () {
var result = mockChart.getClosestRow(2500);
expect([1, 2]).toContain(result);
});
});