UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

318 lines (308 loc) 10 kB
import { getChartURLOptions, pointMultiplierByChartType, getLiveFetchBefore, getChartPayload, errorCodesToMessage } from "./helpers"; import { makeTestChart } from "@jest/testUtilities"; describe("API helpers", function () { describe("getChartURLOptions", function () { it("returns default options for default chart library", function () { var _makeTestChart = makeTestChart({ attributes: { eliminateZeroDimensions: false, urlOptions: [], chartLibrary: "dygraph" } }), chart = _makeTestChart.chart; var result = getChartURLOptions(chart); expect(result).toEqual(["jsonwrap", "flip", "ms", "jw-anomaly-rates", "minify"]); }); it("includes custom urlOptions", function () { var _makeTestChart2 = makeTestChart({ attributes: { eliminateZeroDimensions: false, urlOptions: ["custom-option"], chartLibrary: "dygraph" } }), chart = _makeTestChart2.chart; var result = getChartURLOptions(chart); expect(result).toContain("custom-option"); }); it("includes nonzero when eliminateZeroDimensions is true and not table", function () { var _makeTestChart3 = makeTestChart({ attributes: { eliminateZeroDimensions: true, urlOptions: [], chartLibrary: "dygraph" } }), chart = _makeTestChart3.chart; var result = getChartURLOptions(chart); expect(result).toContain("nonzero"); }); it("excludes nonzero for table chart library", function () { var _makeTestChart4 = makeTestChart({ attributes: { eliminateZeroDimensions: true, urlOptions: [], chartLibrary: "table" } }), chart = _makeTestChart4.chart; var result = getChartURLOptions(chart); expect(result).not.toContain("nonzero"); }); it("excludes nonzero for heatmap chart type", function () { var _makeTestChart5 = makeTestChart({ attributes: { eliminateZeroDimensions: true, urlOptions: [], chartLibrary: "dygraph", chartType: "heatmap" } }), chart = _makeTestChart5.chart; var result = getChartURLOptions(chart); expect(result).not.toContain("nonzero"); }); it("includes group-by-labels for groupBoxes library", function () { var _makeTestChart6 = makeTestChart({ attributes: { eliminateZeroDimensions: false, urlOptions: [], chartLibrary: "groupBoxes" } }), chart = _makeTestChart6.chart; var result = getChartURLOptions(chart); expect(result).toContain("group-by-labels"); }); it("uses default options when attributes are not specified", function () { var _makeTestChart7 = makeTestChart({ attributes: { chartLibrary: "dygraph" } }), chart = _makeTestChart7.chart; var result = getChartURLOptions(chart); // eliminateZeroDimensions is true by default, so "nonzero" is included expect(result).toEqual(["jsonwrap", "nonzero", "flip", "ms", "jw-anomaly-rates", "minify"]); }); }); describe("pointMultiplierByChartType", function () { it("exports correct multipliers", function () { expect(pointMultiplierByChartType).toEqual({ multiBar: 0.1, stackedBar: 0.1, table: 0.1, heatmap: 0.7, "default": 0.7 }); }); }); describe("getLiveFetchBefore", function () { it("rounds the live request anchor from fetchStartedAt", function () { var result = getLiveFetchBefore({ after: -300, renderedAt: null, hovering: false, fetchStartedAt: 1000500, viewUpdateEvery: 0 }); expect(result).toBe(1001); }); it("uses renderedAt when the chart is hovering", function () { var result = getLiveFetchBefore({ after: -300, renderedAt: 1000000, hovering: true, fetchStartedAt: 1001500, viewUpdateEvery: 0 }); expect(result).toBe(1000); }); it("does not anchor absolute windows", function () { var result = getLiveFetchBefore({ after: 1000, renderedAt: null, hovering: false, fetchStartedAt: 1000500, viewUpdateEvery: 0 }); expect(result).toBe(null); }); it("does not anchor view update windows kept relative for the API", function () { var result = getLiveFetchBefore({ after: -300, renderedAt: null, hovering: false, fetchStartedAt: 1000500, viewUpdateEvery: 600 }); expect(result).toBe(null); }); }); describe("getChartPayload", function () { it("returns correct payload structure", function () { var _makeTestChart8 = makeTestChart({ attributes: { after: -300, before: 0, groupingMethod: "average", groupingTime: "auto", renderedAt: null, hovering: false, fetchStartedAt: 1000000, chartType: "line", pixelsPerPoint: 4, chartLibrary: "dygraph" } }), chart = _makeTestChart8.chart; // Mock UI width chart.getUI().getChartWidth = function () { return 800; }; var result = getChartPayload(chart); expect(result).toHaveProperty("points"); expect(result).toHaveProperty("format", "json2"); expect(result).toHaveProperty("time_group", "average"); expect(result).toHaveProperty("time_resampling", "auto"); expect(result).toHaveProperty("after"); expect(result).toHaveProperty("before"); }); it("calculates points correctly", function () { var _makeTestChart9 = makeTestChart({ attributes: { after: -300, before: 0, groupingMethod: "average", groupingTime: "auto", chartType: "line", pixelsPerPoint: 4, chartLibrary: "dygraph" } }), chart = _makeTestChart9.chart; // Mock UI width chart.getUI().getChartWidth = function () { return 800; }; var result = getChartPayload(chart); // (800 / 4) * 0.7 = 140 expect(result.points).toBe(140); }); it("uses container width when available", function () { var _makeTestChart0 = makeTestChart({ attributes: { after: -300, before: 0, groupingMethod: "average", groupingTime: "auto", chartType: "line", pixelsPerPoint: 4, chartLibrary: "dygraph", containerWidth: 1000 } }), chart = _makeTestChart0.chart; // Mock UI width chart.getUI().getChartWidth = function () { return 800; }; var result = getChartPayload(chart); // (1000 / 4) * 0.7 = 175 expect(result.points).toBe(175); }); it("handles positive after/before values", function () { var _makeTestChart1 = makeTestChart({ attributes: { after: 1000, before: 2000, groupingMethod: "average", groupingTime: "auto", chartType: "line", pixelsPerPoint: 4, chartLibrary: "dygraph" } }), chart = _makeTestChart1.chart; // Mock UI width chart.getUI().getChartWidth = function () { return 800; }; var result = getChartPayload(chart); expect(result.after).toBe(1000); expect(result.before).toBe(2000); }); it("uses the same live request anchor as getLiveFetchBefore", function () { var _makeTestChart10 = makeTestChart({ attributes: { after: -300, before: 0, groupingMethod: "average", groupingTime: "auto", renderedAt: null, hovering: false, fetchStartedAt: 1000500, chartType: "line", pixelsPerPoint: 4, chartLibrary: "dygraph" } }), chart = _makeTestChart10.chart; chart.getUI().getChartWidth = function () { return 800; }; var result = getChartPayload(chart); expect(result.after).toBe(701); expect(result.before).toBe(1001); }); it("uses different multiplier for multiBar", function () { var _makeTestChart11 = makeTestChart({ attributes: { after: -300, before: 0, groupingMethod: "average", groupingTime: "auto", chartType: "multiBar", pixelsPerPoint: 4, chartLibrary: "dygraph" } }), chart = _makeTestChart11.chart; // Mock UI width chart.getUI().getChartWidth = function () { return 800; }; var result = getChartPayload(chart); // (800 / 4) * 0.1 = 20 expect(result.points).toBe(20); }); it("defaults to 300 points when calculation is NaN", function () { var _makeTestChart12 = makeTestChart({ attributes: { after: -300, before: 0, groupingMethod: "average", groupingTime: "auto", chartType: "line", pixelsPerPoint: NaN, chartLibrary: "dygraph" } }), chart = _makeTestChart12.chart; // Mock UI width chart.getUI().getChartWidth = function () { return 800; }; var result = getChartPayload(chart); expect(result.points).toBe(300); }); }); describe("errorCodesToMessage", function () { it("exports correct error messages", function () { expect(errorCodesToMessage).toEqual({ ErrAllNodesFailed: "All agents failed to return data" }); }); }); });