@netdata/charts
Version:
Netdata frontend SDK and chart utilities
85 lines (84 loc) • 2.74 kB
JavaScript
;
var _makeIntls = _interopRequireDefault(require("./makeIntls"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
describe("makeIntls", function () {
var mockNavigator;
beforeEach(function () {
mockNavigator = {
language: "en-US"
};
Object.defineProperty(global, "navigator", {
value: mockNavigator,
configurable: true
});
global.Intl = {
DateTimeFormat: jest.fn(function () {
return {
format: jest.fn(function (date) {
return "formatted-date";
})
};
})
};
});
it("creates intl formatters object", function () {
var intls = (0, _makeIntls["default"])();
expect(intls).toHaveProperty("update");
expect(intls).toHaveProperty("formatTime");
expect(intls).toHaveProperty("formatDate");
expect(intls).toHaveProperty("formatXAxis");
expect(intls).toHaveProperty("destroy");
});
it("updates timezone", function () {
var intls = (0, _makeIntls["default"])();
expect(function () {
return intls.update("UTC");
}).not.toThrow();
expect(global.Intl.DateTimeFormat).toHaveBeenCalled();
});
it("formats time", function () {
var intls = (0, _makeIntls["default"])();
intls.update("UTC");
var result = intls.formatTime(new Date());
expect(result).toBe("formatted-date");
});
it("formats date", function () {
var intls = (0, _makeIntls["default"])();
intls.update("UTC");
var result = intls.formatDate(new Date());
expect(result).toBe("formatted-date");
});
it("formats X axis for midnight", function () {
var intls = (0, _makeIntls["default"])();
intls.update("UTC");
var midnight = new Date("2023-01-01T00:00:00Z");
var result = intls.formatXAxis(midnight);
expect(result).toBe("formatted-date");
});
it("formats X axis for non-midnight", function () {
var intls = (0, _makeIntls["default"])();
intls.update("UTC");
var nonMidnight = new Date("2023-01-01T12:30:45Z");
var result = intls.formatXAxis(nonMidnight);
expect(result).toBe("formatted-date");
});
it("falls back to native formatters on error", function () {
global.Intl.DateTimeFormat = jest.fn(function () {
throw new Error("Intl error");
});
var intls = (0, _makeIntls["default"])();
intls.update("UTC");
expect(function () {
return intls.formatTime(new Date());
}).not.toThrow();
expect(function () {
return intls.formatDate(new Date());
}).not.toThrow();
});
it("destroys formatters", function () {
var intls = (0, _makeIntls["default"])();
expect(function () {
return intls.destroy();
}).not.toThrow();
});
});