|
| 1 | +import { ensureIntlSupport } from "./intl-loader"; |
| 2 | + |
| 3 | +describe("intl-loader", () => { |
| 4 | + let originalDateTimeFormat; |
| 5 | + |
| 6 | + beforeAll(() => { |
| 7 | + originalDateTimeFormat = Intl.DateTimeFormat; |
| 8 | + jest.spyOn(console, "info").mockImplementation(() => {}); |
| 9 | + jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 10 | + jest.spyOn(console, "error").mockImplementation(() => {}); |
| 11 | + }); |
| 12 | + |
| 13 | + afterAll(() => { |
| 14 | + Intl.DateTimeFormat = originalDateTimeFormat; |
| 15 | + console.info.mockRestore(); |
| 16 | + console.warn.mockRestore(); |
| 17 | + console.error.mockRestore(); |
| 18 | + }); |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + // Reset to original before each test to have a clean state |
| 23 | + // Note: some polyfill effects might persist if they touch other global objects |
| 24 | + Intl.DateTimeFormat = originalDateTimeFormat; |
| 25 | + }); |
| 26 | + |
| 27 | + it("should detect supported locales correctly", async () => { |
| 28 | + // 'en' should be supported |
| 29 | + await ensureIntlSupport("en"); |
| 30 | + expect(console.info).not.toHaveBeenCalledWith(expect.stringContaining("not supported")); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should handle locale normalization", async () => { |
| 34 | + await ensureIntlSupport("pt_BR"); |
| 35 | + // pt-BR is likely supported, but we just check it doesn't crash |
| 36 | + }); |
| 37 | + |
| 38 | + it("should load polyfill and provide Basque (eu) formatting", async () => { |
| 39 | + // We force a mock that says 'eu' is NOT supported |
| 40 | + const mockSupportedLocalesOf = jest.fn().mockImplementation((locales) => { |
| 41 | + const l = Array.isArray(locales) ? locales[0] : locales; |
| 42 | + if (l.startsWith('eu')) return []; |
| 43 | + return originalDateTimeFormat.supportedLocalesOf(locales); |
| 44 | + }); |
| 45 | + |
| 46 | + // We need to mock the property because it might be a getter |
| 47 | + Object.defineProperty(Intl, 'DateTimeFormat', { |
| 48 | + value: class extends originalDateTimeFormat { |
| 49 | + static supportedLocalesOf = mockSupportedLocalesOf; |
| 50 | + }, |
| 51 | + configurable: true |
| 52 | + }); |
| 53 | + |
| 54 | + await ensureIntlSupport("eu"); |
| 55 | + |
| 56 | + expect(mockSupportedLocalesOf).toHaveBeenCalled(); |
| 57 | + expect(console.info).toHaveBeenCalledWith(expect.stringContaining("not supported")); |
| 58 | + |
| 59 | + // After ensureIntlSupport, Intl.DateTimeFormat should have been replaced by the polyfill |
| 60 | + // since we used polyfill-force.js (or at least it was called). |
| 61 | + |
| 62 | + // Verify formatting using UTC to avoid timezone shifts |
| 63 | + const date = new Date(Date.UTC(2020, 5, 1)); // June 1st UTC |
| 64 | + const dtf = new Intl.DateTimeFormat("eu", { month: "short", timeZone: "UTC" }); |
| 65 | + const formatted = dtf.format(date); |
| 66 | + |
| 67 | + // Basque short month for June is 'eka.' |
| 68 | + expect(formatted.toLowerCase()).toContain("eka"); |
| 69 | + }); |
| 70 | +}); |
0 commit comments