@netdata/charts
Version:
Netdata frontend SDK and chart utilities
189 lines (188 loc) • 5.6 kB
JavaScript
;
var _makeContainer = _interopRequireDefault(require("./makeContainer"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
describe("makeContainer", function () {
var container;
var mockSDK;
var mockParent;
beforeEach(function () {
mockSDK = {
trigger: jest.fn(),
getRoot: jest.fn(function () {
return {};
})
};
mockParent = {
getId: function getId() {
return "parent-id";
},
getAttributes: function getAttributes() {
return {
timezone: "UTC"
};
},
removeChild: jest.fn()
};
container = (0, _makeContainer["default"])({
sdk: mockSDK,
parent: mockParent,
attributes: {
id: "test-container"
}
});
});
it("creates a container with correct type", function () {
expect(container.type).toBe("container");
});
it("inherits node properties", function () {
expect(container.getId()).toBe("test-container");
expect(container.sdk).toBe(mockSDK);
});
describe("appendChild", function () {
it("adds child and triggers events", function () {
var mockChild = {
setParent: jest.fn(),
type: "chart"
};
container.appendChild(mockChild);
expect(mockChild.setParent).toHaveBeenCalledWith(container, {
inherit: true
});
expect(mockSDK.trigger).toHaveBeenCalledWith("nodeAdded", container, mockChild);
expect(mockSDK.trigger).toHaveBeenCalledWith("chartAdded", container, mockChild);
});
it("respects inherit option", function () {
var mockChild = {
setParent: jest.fn(),
type: "chart"
};
container.appendChild(mockChild, {
inherit: false
});
expect(mockChild.setParent).toHaveBeenCalledWith(container, {
inherit: false
});
});
});
describe("removeChild", function () {
it("removes child by id and triggers events", function () {
var mockChild = {
setParent: jest.fn(),
getId: function getId() {
return "child-id";
},
type: "chart"
};
container.appendChild(mockChild);
container.removeChild("child-id");
expect(container.getChildren()).toHaveLength(0);
});
});
describe("getChildren", function () {
it("returns empty array initially", function () {
expect(container.getChildren()).toEqual([]);
});
it("returns added children", function () {
var mockChild = {
setParent: jest.fn(),
getId: function getId() {
return "child-id";
},
type: "chart"
};
container.appendChild(mockChild);
expect(container.getChildren()).toEqual([mockChild]);
});
});
describe("getNode", function () {
it("finds matching child", function () {
var mockChild = {
setParent: jest.fn(),
match: jest.fn(function () {
return true;
}),
type: "chart"
};
container.appendChild(mockChild);
var result = container.getNode({
type: "chart"
});
expect(result).toBe(mockChild);
});
it("returns undefined when no match", function () {
var mockChild = {
setParent: jest.fn(),
match: jest.fn(function () {
return false;
}),
type: "chart"
};
container.appendChild(mockChild);
var result = container.getNode({
type: "other"
});
expect(result).toBeUndefined();
});
it("searches nested containers", function () {
var nestedChild = {
setParent: jest.fn(),
match: jest.fn(function () {
return true;
}),
type: "chart"
};
var nestedContainer = {
setParent: jest.fn(),
match: jest.fn(function () {
return false;
}),
type: "container",
getNode: jest.fn(function () {
return nestedChild;
})
};
container.appendChild(nestedContainer);
var result = container.getNode({
type: "chart"
});
expect(result).toBe(nestedChild);
});
});
describe("getNextColor", function () {
it("returns consistent color for same id", function () {
var mockGetNext = jest.fn(function () {
return "#ff0000";
});
var color1 = container.getNextColor(mockGetNext, "group1", "id1");
var color2 = container.getNextColor(mockGetNext, "group1", "id1");
expect(color1).toBe("#ff0000");
expect(color2).toBe("#ff0000");
expect(mockGetNext).toHaveBeenCalledTimes(1);
});
it("returns different colors for different ids", function () {
var mockGetNext = jest.fn().mockReturnValueOnce("#ff0000").mockReturnValueOnce("#00ff00");
var color1 = container.getNextColor(mockGetNext, "group1", "id1");
var color2 = container.getNextColor(mockGetNext, "group1", "id2");
expect(color1).toBe("#ff0000");
expect(color2).toBe("#00ff00");
expect(mockGetNext).toHaveBeenCalledTimes(2);
});
});
describe("destroy", function () {
it("destroys node and children", function () {
var mockChild = {
setParent: jest.fn(),
destroy: jest.fn(),
type: "chart"
};
container.appendChild(mockChild);
container.destroy();
expect(mockChild.destroy).toHaveBeenCalled();
expect(container.getChildren()).toEqual([]);
});
it("handles being called multiple times", function () {
container.destroy();
container.destroy();
});
});
});