UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

161 lines 7.75 kB
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } 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 React from "react"; import { render, screen } from "@testing-library/react"; import "@testing-library/jest-dom"; import HeadlessChart from "./index"; import makeDefaultSDK from "../../makeDefaultSDK"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; describe("HeadlessChart", function () { it("renders children components", function () { var sdk = makeDefaultSDK(); render(/*#__PURE__*/_jsx(HeadlessChart, { sdk: sdk, contextScope: ["system.load"], agent: true, children: /*#__PURE__*/_jsx("div", { "data-testid": "child", children: "Child Component" }) })); expect(screen.getByTestId("child")).toBeInTheDocument(); }); it("provides chart data via render prop", function () { var sdk = makeDefaultSDK(); render(/*#__PURE__*/_jsx(HeadlessChart, { sdk: sdk, contextScope: ["system.load"], agent: true, children: function children(_ref) { var data = _ref.data, helpers = _ref.helpers, chart = _ref.chart, state = _ref.state; return /*#__PURE__*/_jsxs("div", { "data-testid": "render-prop", children: [/*#__PURE__*/_jsx("div", { "data-testid": "helpers", children: typeof helpers.getDimensionIds === "function" ? "has helpers" : "no helpers" }), /*#__PURE__*/_jsx("div", { "data-testid": "chart", children: chart ? "has chart" : "no chart" }), /*#__PURE__*/_jsx("div", { "data-testid": "data", children: Array.isArray(data) ? "has data" : "no data" }), /*#__PURE__*/_jsx("div", { "data-testid": "state", children: _typeof(state) === "object" ? "has state" : "no state" })] }); } })); expect(screen.getByTestId("render-prop")).toBeInTheDocument(); expect(screen.getByTestId("helpers")).toHaveTextContent("has helpers"); expect(screen.getByTestId("chart")).toHaveTextContent("has chart"); expect(screen.getByTestId("data")).toHaveTextContent("has data"); expect(screen.getByTestId("state")).toHaveTextContent("has state"); }); it("creates chart with provided SDK", function () { var sdk = makeDefaultSDK(); var view = render(/*#__PURE__*/_jsx(HeadlessChart, { sdk: sdk, contextScope: ["system.load"], agent: true, host: "http://localhost:19999/api/v3", children: /*#__PURE__*/_jsx("div", { children: "Test" }) })); var _sdk$getRoot$getChild = sdk.getRoot().getChildren(), _sdk$getRoot$getChild2 = _slicedToArray(_sdk$getRoot$getChild, 1), chart = _sdk$getRoot$getChild2[0]; expect(chart.getAttributes()).toMatchObject({ contextScope: ["system.load"], agent: true, host: "http://localhost:19999/api/v3" }); view.unmount(); expect(sdk.getRoot().getChildren()).toEqual([]); }); it("updates an owned chart without recreating it", function () { var sdk = makeDefaultSDK(); var view = render(/*#__PURE__*/_jsx(HeadlessChart, { sdk: sdk, contextScope: ["system.load"], children: /*#__PURE__*/_jsx("div", { children: "Test" }) })); var _sdk$getRoot$getChild3 = sdk.getRoot().getChildren(), _sdk$getRoot$getChild4 = _slicedToArray(_sdk$getRoot$getChild3, 1), chart = _sdk$getRoot$getChild4[0]; view.rerender(/*#__PURE__*/_jsx(HeadlessChart, { sdk: sdk, contextScope: ["system.cpu"], children: /*#__PURE__*/_jsx("div", { children: "Test" }) })); expect(sdk.getRoot().getChildren()).toEqual([chart]); expect(chart.getAttribute("contextScope")).toEqual(["system.cpu"]); }); it("does not destroy a borrowed chart", function () { var sdk = makeDefaultSDK(); var chart = sdk.makeChart(); sdk.appendChild(chart); var view = render(/*#__PURE__*/_jsx(HeadlessChart, { chart: chart, children: /*#__PURE__*/_jsx("div", { children: "Test" }) })); view.unmount(); expect(sdk.getRoot().getChildren()).toEqual([chart]); }); it("creates default SDK when none provided", function () { render(/*#__PURE__*/_jsx(HeadlessChart, { contextScope: ["system.load"], agent: true, children: /*#__PURE__*/_jsx("div", { "data-testid": "default-sdk", children: "Default SDK" }) })); expect(screen.getByTestId("default-sdk")).toBeInTheDocument(); }); it("provides helper functions in render prop", function () { var sdk = makeDefaultSDK(); render(/*#__PURE__*/_jsx(HeadlessChart, { sdk: sdk, contextScope: ["system.load"], agent: true, children: function children(_ref2) { var helpers = _ref2.helpers; return /*#__PURE__*/_jsxs("div", { children: [/*#__PURE__*/_jsx("div", { "data-testid": "update-attribute", children: typeof helpers.updateAttribute === "function" ? "function" : "not function" }), /*#__PURE__*/_jsx("div", { "data-testid": "get-attribute", children: typeof helpers.getAttribute === "function" ? "function" : "not function" }), /*#__PURE__*/_jsx("div", { "data-testid": "get-dimension-ids", children: typeof helpers.getDimensionIds === "function" ? "function" : "not function" }), /*#__PURE__*/_jsx("div", { "data-testid": "format-time", children: typeof helpers.formatTime === "function" ? "function" : "not function" })] }); } })); expect(screen.getByTestId("update-attribute")).toHaveTextContent("function"); expect(screen.getByTestId("get-attribute")).toHaveTextContent("function"); expect(screen.getByTestId("get-dimension-ids")).toHaveTextContent("function"); expect(screen.getByTestId("format-time")).toHaveTextContent("function"); }); });