UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

348 lines (347 loc) 17.1 kB
"use strict"; var _react = require("@testing-library/react"); var _testUtilities = require("@jest/testUtilities"); var _selectors = require("./selectors"); 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); } describe("Chart Provider Selectors", function () { describe("useChart", function () { it("returns chart from context", function () { var _renderHookWithChart = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useChart)(); }), result = _renderHookWithChart.result, chart = _renderHookWithChart.chart; expect(result.current).toBe(chart); expect(_typeof(result.current.getId)).toBe("function"); }); }); describe("useForceUpdate", function () { it("returns function that triggers re-render", function () { var _renderHook = (0, _react.renderHook)(function () { return (0, _selectors.useForceUpdate)(); }), result = _renderHook.result; expect(_typeof(result.current)).toBe("function"); }); it("increments counter when called", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() { var renderCount, _renderHook2, result, initialRenderCount; return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: renderCount = 0; _renderHook2 = (0, _react.renderHook)(function () { renderCount++; return (0, _selectors.useForceUpdate)(); }), result = _renderHook2.result; initialRenderCount = renderCount; _context2.n = 1; return (0, _react.act)(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() { return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: result.current(); case 1: return _context.a(2); } }, _callee); }))); case 1: expect(renderCount).toBeGreaterThan(initialRenderCount); case 2: return _context2.a(2); } }, _callee2); }))); }); describe("useAttributeValue", function () { it("returns attribute value from chart", function () { var _renderHookWithChart2 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useAttributeValue)("sparkline"); }, { attributes: { sparkline: true } }), result = _renderHookWithChart2.result; expect(result.current).toBe(true); }); it("returns default value when attribute is undefined", function () { var _renderHookWithChart3 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useAttributeValue)("nonexistent", "default"); }), result = _renderHookWithChart3.result; expect(result.current).toBe("default"); }); it("supports nested attributes with fallback values", function () { var _renderHookWithChart4 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useAttributeValue)("drawer.showAdvancedStats", false); }, { attributes: { drawer: { showAdvancedStats: true } } }), result = _renderHookWithChart4.result; expect(result.current).toBe(true); }); it("returns fallback for nested attributes when parent missing", function () { var _renderHookWithChart5 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useAttributeValue)("drawer.showAdvancedStats", false); }), result = _renderHookWithChart5.result; expect(result.current).toBe(false); }); }); describe("useEmpty", function () { it("returns true when chart data is empty", function () { var _renderHookWithChart6 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useEmpty)(); }, { mockData: { result: { data: [] } } }), result = _renderHookWithChart6.result; expect(result.current).toBe(true); }); it("returns false when chart has data", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() { var _renderHookWithChart7, result, chart; return _regenerator().w(function (_context4) { while (1) switch (_context4.n) { case 0: _renderHookWithChart7 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useEmpty)(); }), result = _renderHookWithChart7.result, chart = _renderHookWithChart7.chart; _context4.n = 1; return (0, _react.act)(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() { return _regenerator().w(function (_context3) { while (1) switch (_context3.n) { case 0: chart.fetch(); _context3.n = 1; return new Promise(function (resolve) { return setTimeout(resolve, 50); }); case 1: return _context3.a(2); } }, _callee3); }))); case 1: expect(result.current).toBe(false); case 2: return _context4.a(2); } }, _callee4); }))); }); describe("useTitle", function () { it("returns title from chart attributes", function () { var _renderHookWithChart8 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useTitle)(); }, { attributes: { title: "Test Chart Title", contextScope: ["system.cpu"] } }), result = _renderHookWithChart8.result; expect(result.current).toBe("Test Chart Title"); }); it("prefers explicit title over chartTitleByContextMap value", function () { var _renderHookWithChart9 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useTitle)(); }, { attributes: { title: "Web 01", contextScope: ["fping.latency"] } }), result = _renderHookWithChart9.result; expect(result.current).toBe("Web 01"); }); it("falls back to chartTitleByContextMap when title is empty", function () { var _renderHookWithChart0 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useTitle)(); }, { attributes: { title: "", contextScope: ["fping.latency"] } }), result = _renderHookWithChart0.result; expect(result.current).toBe("FPing Latency"); }); it("returns explicit title when context is not in the map", function () { var _renderHookWithChart1 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useTitle)(); }, { attributes: { title: "My Custom", contextScope: ["system.cpu"] } }), result = _renderHookWithChart1.result; expect(result.current).toBe("My Custom"); }); }); describe("useName", function () { it("returns name from chart attributes", function () { var _renderHookWithChart10 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useName)(); }, { attributes: { name: "Test Chart Name", contextScope: ["system.cpu"] } }), result = _renderHookWithChart10.result; expect(result.current).toBe("Test Chart Name"); }); it("returns contextScope when no name provided", function () { var _renderHookWithChart11 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useName)(); }, { attributes: { contextScope: ["system.cpu", "system.memory"] } }), result = _renderHookWithChart11.result; expect(result.current).toBe("system.cpu, system.memory"); }); }); describe("useIsMinimal", function () { it("returns true when design flavour is minimal", function () { var _renderHookWithChart12 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useIsMinimal)(); }, { attributes: { designFlavour: "minimal" } }), result = _renderHookWithChart12.result; expect(result.current).toBe(true); }); it("returns false when design flavour is not minimal", function () { var _renderHookWithChart13 = (0, _testUtilities.renderHookWithChart)(function () { return (0, _selectors.useIsMinimal)(); }, { attributes: { designFlavour: "default" } }), result = _renderHookWithChart13.result; expect(result.current).toBe(false); }); }); describe("getValueByPeriod", function () { var chart; var mockData = [[1617946860000, 25, 50, 75], [1617946920000, 30, 55, 70], [1617946980000, 20, 45, 80], [1617947040000, 35, 60, 65]]; beforeEach(function () { var _makeTestChart = (0, _testUtilities.makeTestChart)(), testChart = _makeTestChart.chart; chart = testChart; jest.spyOn(chart, "getPayload").mockReturnValue({ data: mockData }); jest.spyOn(chart, "getDimensionIndex").mockReturnValue(0); jest.spyOn(chart, "isDimensionVisible").mockReturnValue(true); jest.spyOn(chart, "getVisibleDimensionIds").mockReturnValue(["dim1"]); jest.spyOn(chart, "getRowDimensionValue").mockImplementation(function (id, row) { var dimIndex = chart.getDimensionIndex(id); return row[dimIndex + 1]; }); }); describe("highlight period", function () { it("returns null when no highlight range is set", function () { chart.updateAttribute("overlays", {}); var result = _selectors.getValueByPeriod.highlight({ chart: chart, id: "dim1", valueKey: "avg" }); expect(result).toBeNull(); }); it("filters data by highlight range and calculates min value", function () { var highlightRange = [1617946920, 1617946980]; chart.updateAttribute("overlays", { highlight: { range: highlightRange } }); var result = _selectors.getValueByPeriod.highlight({ chart: chart, id: "dim1", valueKey: "min" }); expect(result).toBe(20); }); it("filters data by highlight range and calculates avg value", function () { var highlightRange = [1617946920, 1617946980]; chart.updateAttribute("overlays", { highlight: { range: highlightRange } }); var result = _selectors.getValueByPeriod.highlight({ chart: chart, id: "dim1", valueKey: "avg" }); expect(result).toBe(25); }); it("filters data by highlight range and calculates max value", function () { var highlightRange = [1617946920, 1617946980]; chart.updateAttribute("overlays", { highlight: { range: highlightRange } }); var result = _selectors.getValueByPeriod.highlight({ chart: chart, id: "dim1", valueKey: "max" }); expect(result).toBe(30); }); it("returns null when no data matches highlight range", function () { var highlightRange = [1617900000, 1617900060]; chart.updateAttribute("overlays", { highlight: { range: highlightRange } }); var result = _selectors.getValueByPeriod.highlight({ chart: chart, id: "dim1", valueKey: "avg" }); expect(result).toBeNull(); }); it("handles dimension not visible by using first visible dimension", function () { jest.spyOn(chart, "isDimensionVisible").mockReturnValue(false); jest.spyOn(chart, "getVisibleDimensionIds").mockReturnValue(["visibleDim"]); jest.spyOn(chart, "getDimensionIndex").mockReturnValue(1); var highlightRange = [1617946860, 1617947040]; chart.updateAttribute("overlays", { highlight: { range: highlightRange } }); var result = _selectors.getValueByPeriod.highlight({ chart: chart, id: "hiddenDim", valueKey: "avg" }); expect(result).toBe(52.5); }); }); }); });