@netdata/charts
Version:
Netdata frontend SDK and chart utilities
497 lines (496 loc) • 19.6 kB
JavaScript
;
var _testUtilities = require("@jest/testUtilities");
var _index = _interopRequireDefault(require("./index"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
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); }
var mockDygraph = {
updateOptions: jest.fn(),
destroy: jest.fn(),
resize: jest.fn(),
setSelection: jest.fn(),
xAxisRange: jest.fn(function () {
return [1617946860000, 1617947750000];
}),
getArea: jest.fn(function () {
return {
w: 800,
h: 400
};
}),
toDomXCoord: jest.fn(function (x) {
return x;
}),
xAxisExtremes: jest.fn(function () {
return [1617946860000, 1617947750000];
}),
renderGraph_: jest.fn(function (x) {
return x;
})
};
jest.mock("dygraphs", function () {
return jest.fn(function () {
return mockDygraph;
});
});
describe("dygraphChart", function () {
beforeEach(function () {
jest.clearAllMocks();
});
it("creates chart instance with required methods", function () {
var _makeTestChart = (0, _testUtilities.makeTestChart)(),
sdk = _makeTestChart.sdk,
chart = _makeTestChart.chart;
var instance = (0, _index["default"])(sdk, chart);
expect(instance).toHaveProperty("mount");
expect(instance).toHaveProperty("unmount");
expect(instance).toHaveProperty("render");
expect(instance).toHaveProperty("getDygraph");
expect(instance).toHaveProperty("getChartWidth");
expect(instance).toHaveProperty("getChartHeight");
expect(_typeof(instance.mount)).toBe("function");
expect(_typeof(instance.unmount)).toBe("function");
expect(_typeof(instance.render)).toBe("function");
});
it("mounts and creates dygraph instance", function () {
var _makeTestChart2 = (0, _testUtilities.makeTestChart)({
attributes: {
theme: "dark",
outOfLimits: false
}
}),
sdk = _makeTestChart2.sdk,
chart = _makeTestChart2.chart;
chart.getPayload = function () {
return {
data: [[1617946860000, 10, 20]],
labels: ["time", "load1", "load5"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
var element = document.createElement("div");
expect(function () {
return instance.mount(element);
}).not.toThrow();
var Dygraph = require("dygraphs");
expect(Dygraph).toHaveBeenCalledWith(element, expect.any(Array), expect.any(Object));
expect(element.classList.contains("dark")).toBe(true);
});
it("handles empty data by creating placeholder chart", function () {
var _makeTestChart3 = (0, _testUtilities.makeTestChart)({
attributes: {
theme: "light",
outOfLimits: true
}
}),
sdk = _makeTestChart3.sdk,
chart = _makeTestChart3.chart;
chart.getPayload = function () {
return {
data: [],
labels: []
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
var element = document.createElement("div");
instance.mount(element);
var Dygraph = require("dygraphs");
expect(Dygraph).toHaveBeenCalledWith(element, [[0]], expect.objectContaining({
labels: ["X"]
}));
});
it("prevents multiple mounts on same element", function () {
var _makeTestChart4 = (0, _testUtilities.makeTestChart)({
attributes: {
theme: "dark"
}
}),
sdk = _makeTestChart4.sdk,
chart = _makeTestChart4.chart;
chart.getPayload = function () {
return {
data: [[1617946860000, 10]],
labels: ["time", "value"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
var element = document.createElement("div");
instance.mount(element);
var Dygraph = require("dygraphs");
var firstCallCount = Dygraph.mock.calls.length;
instance.mount(element);
expect(Dygraph.mock.calls.length).toBe(firstCallCount);
});
it("renders without errors when not mounted", function () {
var _makeTestChart5 = (0, _testUtilities.makeTestChart)(),
sdk = _makeTestChart5.sdk,
chart = _makeTestChart5.chart;
var instance = (0, _index["default"])(sdk, chart);
expect(function () {
return instance.render();
}).not.toThrow();
});
it("skips render when highlighting, panning, or processing", function () {
var _makeTestChart6 = (0, _testUtilities.makeTestChart)({
attributes: {
highlighting: true,
panning: false,
processing: false
}
}),
sdk = _makeTestChart6.sdk,
chart = _makeTestChart6.chart;
chart.getPayload = function () {
return {
data: [[1617946860000, 10]],
labels: ["time", "value"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
var element = document.createElement("div");
instance.mount(element);
instance.render();
expect(mockDygraph.updateOptions).not.toHaveBeenCalled();
});
it("unmounts without errors", function () {
var _makeTestChart7 = (0, _testUtilities.makeTestChart)(),
sdk = _makeTestChart7.sdk,
chart = _makeTestChart7.chart;
chart.getPayload = function () {
return {
data: [[1617946860000, 10]],
labels: ["time", "value"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
var element = document.createElement("div");
instance.mount(element);
expect(function () {
return instance.unmount();
}).not.toThrow();
expect(mockDygraph.destroy).toHaveBeenCalled();
});
it("returns dygraph instance from getDygraph", function () {
var _makeTestChart8 = (0, _testUtilities.makeTestChart)(),
sdk = _makeTestChart8.sdk,
chart = _makeTestChart8.chart;
chart.getPayload = function () {
return {
data: [[1617946860000, 10]],
labels: ["time", "value"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
var element = document.createElement("div");
expect(instance.getDygraph()).toBeNull();
instance.mount(element);
expect(instance.getDygraph()).toBe(mockDygraph);
});
it("calculates chart dimensions from dygraph when mounted", function () {
var _makeTestChart9 = (0, _testUtilities.makeTestChart)(),
sdk = _makeTestChart9.sdk,
chart = _makeTestChart9.chart;
chart.getPayload = function () {
return {
data: [[1617946860000, 10]],
labels: ["time", "value"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
var element = document.createElement("div");
instance.mount(element);
expect(instance.getChartWidth()).toBe(800);
expect(instance.getChartHeight()).toBe(400);
});
it("enables dygraph stepPlot when the stepPlot attribute is set", function () {
var _makeTestChart0 = (0, _testUtilities.makeTestChart)({
attributes: {
theme: "dark",
chartType: "line",
stepPlot: true
}
}),
sdk = _makeTestChart0.sdk,
chart = _makeTestChart0.chart;
chart.getPayload = function () {
return {
data: [[1617946860000, 1]],
labels: ["time", "value"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
instance.mount(document.createElement("div"));
var Dygraph = require("dygraphs");
expect(Dygraph).toHaveBeenCalledWith(expect.any(Object), expect.any(Array), expect.objectContaining({
stepPlot: true
}));
});
it("does not enable dygraph stepPlot by default", function () {
var _makeTestChart1 = (0, _testUtilities.makeTestChart)({
attributes: {
theme: "dark",
chartType: "line"
}
}),
sdk = _makeTestChart1.sdk,
chart = _makeTestChart1.chart;
chart.getPayload = function () {
return {
data: [[1617946860000, 10]],
labels: ["time", "value"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
instance.mount(document.createElement("div"));
var Dygraph = require("dygraphs");
expect(Dygraph).toHaveBeenCalledWith(expect.any(Object), expect.any(Array), expect.objectContaining({
stepPlot: false
}));
});
it("preserves compact scaled heatmap y-axis labels", function () {
var ids = ["0.005", "1000", "+Inf"];
var _makeTestChart10 = (0, _testUtilities.makeTestChart)({
attributes: {
chartType: "heatmap",
groupBy: ["dimension"],
selectedLegendDimensions: [],
viewDimensions: {
ids: ids,
names: ids,
priorities: ids.map(function (_, index) {
return index;
}),
units: ids.map(function () {
return "";
}),
contexts: ids.map(function () {
return "";
}),
grouped: ["dimension"]
}
}
}),
sdk = _makeTestChart10.sdk,
chart = _makeTestChart10.chart;
chart.updateDimensions();
chart.getPayload = function () {
return {
data: [[1617946860000, 1, 2, 3]],
labels: ["time", "0.005", "1000", "+Inf"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
instance.mount(document.createElement("div"));
var Dygraph = require("dygraphs");
var options = Dygraph.mock.calls[Dygraph.mock.calls.length - 1][2];
var formatter = options.axes.y.axisLabelFormatter;
expect(formatter("5m")).toBe("5m");
expect(formatter("1k")).toBe("1k");
expect(formatter("+Inf")).toBe("+Inf");
expect(formatter("0.005")).toBe(0.005);
});
it("uses cropped heatmap bucket count for y-axis range", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
var ids, _makeTestChart11, sdk, chart, instance, Dygraph, options;
return _regenerator().w(function (_context) {
while (1) switch (_context.n) {
case 0:
ids = ["0", "1", "2", "3", "4", "5", "6"];
_makeTestChart11 = (0, _testUtilities.makeTestChart)({
attributes: {
chartType: "heatmap",
groupBy: ["dimension"],
selectedLegendDimensions: [],
viewDimensions: {
ids: ids,
names: ids,
priorities: ids.map(function (_, index) {
return index;
}),
units: ids.map(function () {
return "";
}),
contexts: ids.map(function () {
return "";
}),
grouped: ["dimension"]
}
}
}), sdk = _makeTestChart11.sdk, chart = _makeTestChart11.chart;
_context.n = 1;
return (0, _testUtilities.loadHeatmapPayload)(chart, ids, [[0, 0, 1, 0, 2, 0, 0]], {
timestamp: 1617946860000
});
case 1:
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
instance = (0, _index["default"])(sdk, chart);
instance.mount(document.createElement("div"));
Dygraph = require("dygraphs");
options = Dygraph.mock.calls[Dygraph.mock.calls.length - 1][2];
expect(options.valueRange).toEqual([0, 5]);
case 2:
return _context.a(2);
}
}, _callee);
})));
it("returns axis range from dygraph", function () {
var _makeTestChart12 = (0, _testUtilities.makeTestChart)(),
sdk = _makeTestChart12.sdk,
chart = _makeTestChart12.chart;
chart.getPayload = function () {
return {
data: [[1617946860000, 10]],
labels: ["time", "value"]
};
};
chart.getDateWindow = function () {
return [1617946860000, 1617947750000];
};
chart.formatXAxis = function (x) {
return x.toString();
};
chart.getThemeAttribute = function () {
return "#333";
};
chart.getUnitSign = function () {
return "";
};
var instance = (0, _index["default"])(sdk, chart);
var element = document.createElement("div");
instance.mount(element);
expect(instance.getXAxisRange()).toEqual([1617946860000, 1617947750000]);
});
});