UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

259 lines (258 loc) 8.97 kB
"use strict"; var _makeControllers = _interopRequireDefault(require("./makeControllers")); var _testUtilities = require("@jest/testUtilities"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } describe("makeControllers", function () { var chart; var controllers; beforeEach(function () { var _makeTestChart = (0, _testUtilities.makeTestChart)(), testChart = _makeTestChart.chart; chart = testChart; controllers = (0, _makeControllers["default"])(chart); }); it("returns controller functions", function () { expect(controllers).toHaveProperty("updateGroupByAttribute"); expect(controllers).toHaveProperty("updatePostGroupByAttribute"); expect(controllers).toHaveProperty("updateChartTypeAttribute"); expect(controllers).toHaveProperty("updateNodesAttribute"); expect(controllers).toHaveProperty("updateInstancesAttribute"); expect(controllers).toHaveProperty("updateDimensionsAttribute"); expect(controllers).toHaveProperty("updateLabelsAttribute"); expect(controllers).toHaveProperty("updateAggregationMethodAttribute"); expect(controllers).toHaveProperty("updatePostAggregationMethodAttribute"); expect(controllers).toHaveProperty("updateTimeAggregationMethodAttribute"); expect(controllers).toHaveProperty("updateContextScopeAttribute"); expect(controllers).toHaveProperty("updateNodeLabelsFilter"); expect(controllers).toHaveProperty("resetPristine"); expect(controllers).toHaveProperty("removePristine"); expect(controllers).toHaveProperty("toggleFullscreen"); }); it("updateAggregationMethodAttribute updates when value changes", function () { chart.updateAttribute("aggregationMethod", "avg"); var spy = jest.spyOn(chart, "updateAttributes"); var triggerSpy = jest.spyOn(chart, "trigger"); controllers.updateAggregationMethodAttribute("sum"); expect(spy).toHaveBeenCalledWith({ aggregationMethod: "sum", processing: true }); expect(triggerSpy).toHaveBeenCalledWith("fetch", { processing: true }); }); it("updateAggregationMethodAttribute skips when value unchanged", function () { chart.updateAttribute("aggregationMethod", "avg"); var spy = jest.spyOn(chart, "updateAttributes"); controllers.updateAggregationMethodAttribute("avg"); expect(spy).not.toHaveBeenCalled(); }); it("updatePostAggregationMethodAttribute updates when value changes", function () { var spy = jest.spyOn(chart, "updateAttributes"); controllers.updatePostAggregationMethodAttribute("sum"); expect(spy).toHaveBeenCalledWith({ postAggregationMethod: "sum", processing: true }); }); it("updateContextScopeAttribute updates when value changes", function () { var spy = jest.spyOn(chart, "updateAttributes"); controllers.updateContextScopeAttribute("new-context"); expect(spy).toHaveBeenCalledWith({ contextScope: ["new-context"], processing: true }); }); it("updateContextScopeAttribute skips when value unchanged", function () { chart.updateAttribute("contextScope", ["same-context"]); var spy = jest.spyOn(chart, "updateAttributes"); controllers.updateContextScopeAttribute("same-context"); expect(spy).not.toHaveBeenCalled(); }); it("updateTimeAggregationMethodAttribute formats value with alias", function () { var spy = jest.spyOn(chart, "updateAttributes"); controllers.updateTimeAggregationMethodAttribute({ alias: "_avg", method: "time" }); expect(spy).toHaveBeenCalledWith({ groupingMethod: "time_avg", processing: true }); }); it("updateTimeAggregationMethodAttribute uses method without alias", function () { var spy = jest.spyOn(chart, "updateAttributes"); controllers.updateTimeAggregationMethodAttribute({ method: "time" }); expect(spy).toHaveBeenCalledWith({ groupingMethod: "time", processing: true }); }); it("toggleFullscreen toggles fullscreen state", function () { var spy = jest.spyOn(chart, "updateAttribute"); controllers.toggleFullscreen(); expect(spy).toHaveBeenCalledWith("fullscreen", true); }); it("removePristine updates pristine attribute", function () { var spy = jest.spyOn(chart, "updateAttribute"); controllers.removePristine(); expect(spy).toHaveBeenCalledWith("pristine", {}); }); describe("updateGroupByAttribute", function () { beforeEach(function () { chart.fetch = jest.fn(function () { return Promise.resolve(); }); }); it("updates groupBy with allowed values", function () { var selected = [{ value: "node", isLabel: false }, { value: "instance", isLabel: false }]; var spy = jest.spyOn(chart, "updateAttributes"); var fetchSpy = jest.spyOn(chart, "fetch"); controllers.updateGroupByAttribute(selected); expect(spy).toHaveBeenCalledWith({ groupByLabel: [], groupBy: ["node", "instance"], processing: true }); expect(fetchSpy).toHaveBeenCalledWith({ processing: true }); }); it("handles label selections and adds label to groupBy", function () { var selected = [{ value: "dimension", isLabel: false }, { value: "custom_label", isLabel: true }]; var spy = jest.spyOn(chart, "updateAttributes"); controllers.updateGroupByAttribute(selected); expect(spy).toHaveBeenCalledWith({ groupByLabel: ["custom_label"], groupBy: ["dimension", "label"], processing: true }); }); it("falls back to dimension when no valid groupBy selected", function () { chart.updateAttributes({ groupBy: ["node"] }); var selected = [{ value: "invalid", isLabel: false }]; var spy = jest.spyOn(chart, "updateAttributes"); controllers.updateGroupByAttribute(selected); expect(spy).toHaveBeenCalledWith({ groupByLabel: [], groupBy: ["dimension"], processing: true }); }); it("skips update when values unchanged", function () { chart.updateAttributes({ groupBy: ["node"], groupByLabel: [] }); var selected = [{ value: "node", isLabel: false }]; var spy = jest.spyOn(chart, "updateAttributes"); var fetchSpy = jest.spyOn(chart, "fetch"); controllers.updateGroupByAttribute(selected); expect(spy).not.toHaveBeenCalled(); expect(fetchSpy).not.toHaveBeenCalled(); }); it("filters out non-allowed groupBy values", function () { var selected = [{ value: "node", isLabel: false }, { value: "invalid_value", isLabel: false }, { value: "instance", isLabel: false }]; var spy = jest.spyOn(chart, "updateAttributes"); controllers.updateGroupByAttribute(selected); expect(spy).toHaveBeenCalledWith({ groupByLabel: [], groupBy: ["node", "instance"], processing: true }); }); it("handles mixed labels and groupBy values", function () { var selected = [{ value: "node", isLabel: false }, { value: "label1", isLabel: true }, { value: "label2", isLabel: true }, { value: "instance", isLabel: false }]; var spy = jest.spyOn(chart, "updateAttributes"); controllers.updateGroupByAttribute(selected); expect(spy).toHaveBeenCalledWith({ groupByLabel: ["label1", "label2"], groupBy: ["node", "instance", "label"], processing: true }); }); }); describe("updateNodesAttribute", function () { it("updates selectedNodes", function () { var selected = [{ value: "node1", isInstance: false }, { value: "node2", isInstance: false }]; var spy = jest.spyOn(chart, "updateAttributes"); var triggerSpy = jest.spyOn(chart, "trigger"); controllers.updateNodesAttribute(selected); expect(spy).toHaveBeenCalledWith({ selectedNodes: ["node1", "node2"], processing: true }); expect(triggerSpy).toHaveBeenCalledWith("fetch", { processing: true }); }); }); describe("updateDimensionsAttribute", function () { it("updates selectedDimensions", function () { var selected = [{ value: "cpu" }, { value: "memory" }]; var spy = jest.spyOn(chart, "updateAttributes"); var triggerSpy = jest.spyOn(chart, "trigger"); controllers.updateDimensionsAttribute(selected); expect(spy).toHaveBeenCalledWith({ selectedDimensions: ["cpu", "memory"], processing: true }); expect(triggerSpy).toHaveBeenCalledWith("fetch", { processing: true }); }); }); });