import { describe, expect, it } from 'vitest'; import { createActiveFormat, createEngineFormat } from '..'; describe('createEngineFormat()', () => { it('composes number, currency, units and dates format engines', async () => { const formats = createEngineFormat({ locale: 'en-US', currency: { rates: undefined } }); expect(formats.getLocale()).toBe('en-US'); expect(formats.numbers.format(1234.5)).toBe('1,234.5'); expect(formats.currency.getCurrency()).toBe('USD'); expect(formats.units.getSystem()).toBe('imperial'); expect(formats.units.getDefaultUnit('distance')).toBe('mile'); expect(formats.dates.getHourCycle()).toBe(12); formats.setLocale('es-ES'); expect(formats.getLocale()).toBe('es-ES'); expect(formats.currency.getCurrency()).toBe('EUR'); expect(formats.units.getSystem()).toBe('metric'); expect(formats.units.getDefaultUnit('distance')).toBe('kilometer'); await expect(formats.currency.convert(1, 'EUR')).resolves.toBe(1); }); }); describe('createActiveFormat()', () => { it('fans out locale changes to every active format module', () => { const formats = createActiveFormat({ locale: 'en-US' }); expect(formats.getLocale()).toBe('en-US'); expect(formats.currency.getCurrency()).toBe('USD'); expect(formats.units.getSystem()).toBe('imperial'); expect(formats.dates.getHourCycle()).toBe(12); formats.setLocale('es-ES'); expect(formats.getLocale()).toBe('es-ES'); expect(formats.currency.getCurrency()).toBe('EUR'); expect(formats.units.getSystem()).toBe('metric'); expect(formats.dates.getDateOrder()).toBe('DMY'); formats.dispose(); }); it('exposes disposed flag, is idempotent, and ignores setLocale post-dispose', () => { const formats = createActiveFormat({ locale: 'en-US' }); expect(formats.disposed).toBe(false); formats.dispose(); expect(formats.disposed).toBe(true); expect(() => formats.dispose()).not.toThrow(); // setLocale is a silent no-op after dispose; the cached values stay // frozen at the last set value. const lastLocale = formats.getLocale(); formats.setLocale('fr-FR'); expect(formats.getLocale()).toBe(lastLocale); }); });