@netdata/charts
Version:
Netdata frontend SDK and chart utilities
83 lines (82 loc) • 3.08 kB
JavaScript
;
var _makeChartUI = _interopRequireDefault(require("./makeChartUI"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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("makeChartUI", function () {
var chartUI;
var mockSDK;
var mockChart;
var mockElement;
beforeEach(function () {
mockSDK = {
trigger: jest.fn()
};
mockChart = {
getDateWindow: jest.fn(function () {
return [1000, 2000];
}),
trigger: jest.fn(),
on: jest.fn()
};
mockElement = {
offsetWidth: 800,
offsetHeight: 400
};
chartUI = (0, _makeChartUI["default"])(mockSDK, mockChart);
});
it("initializes with chart properties", function () {
expect(chartUI.sdk).toBe(mockSDK);
expect(chartUI.chart).toBe(mockChart);
expect(chartUI.getRenderedAt()).toBe(2000);
});
describe("mount", function () {
it("sets element and triggers mount events", function () {
chartUI.mount(mockElement);
expect(chartUI.getElement()).toBe(mockElement);
expect(mockSDK.trigger).toHaveBeenCalledWith("mountChartUI", mockChart);
expect(mockChart.trigger).toHaveBeenCalledWith("mountChartUI");
});
});
describe("unmount", function () {
beforeEach(function () {
chartUI.mount(mockElement);
});
it("clears element and triggers unmount events", function () {
chartUI.unmount();
expect(chartUI.getElement()).toBeNull();
expect(mockSDK.trigger).toHaveBeenCalledWith("unmountChartUI", mockChart);
expect(mockChart.trigger).toHaveBeenCalledWith("unmountChartUI");
});
});
describe("render", function () {
it("updates rendered timestamp", function () {
mockChart.getDateWindow.mockReturnValue([1500, 2500]);
chartUI.render();
expect(chartUI.getRenderedAt()).toBe(2500);
});
});
describe("dimensions", function () {
beforeEach(function () {
chartUI.mount(mockElement);
});
it("returns element dimensions when mounted", function () {
expect(chartUI.getChartWidth()).toBe(800);
expect(chartUI.getChartHeight()).toBe(400);
});
it("returns default dimensions when not mounted", function () {
chartUI.unmount();
expect(chartUI.getChartWidth()).toBe(300);
expect(chartUI.getChartHeight()).toBe(300);
});
});
describe("event listeners", function () {
it("inherits listener functionality", function () {
expect(_typeof(chartUI.on)).toBe("function");
expect(_typeof(chartUI.off)).toBe("function");
expect(_typeof(chartUI.trigger)).toBe("function");
});
it("registers for chart events", function () {
expect(mockChart.on).toHaveBeenCalledWith("visibleDimensionsChanged", expect.any(Function));
});
});
});