You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.7 KiB
87 lines
2.7 KiB
import { describe, expect, expectTypeOf, it } from 'vitest';
|
|
import { createEngineLogger, LogLevel } from '$logr';
|
|
import {
|
|
AUTO_CURRENCY,
|
|
CURRENCY_ERRORS,
|
|
FMTS_CURR_MODULE,
|
|
createEngineCurrency,
|
|
createRates,
|
|
type EngineCurrency
|
|
} from '..';
|
|
|
|
describe('createEngineCurrency()', () => {
|
|
it('returns the public Curr facade', () => {
|
|
const curr = createEngineCurrency({ locale: 'es-ES' });
|
|
|
|
expect(curr.getLocale()).toBe('es-ES');
|
|
expect(curr.getCurrency()).toBe('EUR');
|
|
expect(curr.format).toBeTypeOf('function');
|
|
expect(curr.convert).toBeTypeOf('function');
|
|
expect(curr.ratesSupported).toBe(false);
|
|
expectTypeOf(curr).toEqualTypeOf<EngineCurrency>();
|
|
});
|
|
|
|
it('uses defaultCurrency when auto currency cannot resolve a region', () => {
|
|
const curr = createEngineCurrency({
|
|
locale: 'es',
|
|
currency: AUTO_CURRENCY,
|
|
defaultCurrency: 'MXN'
|
|
});
|
|
|
|
expect(curr.getCurrency()).toBe('MXN');
|
|
});
|
|
|
|
it('supports locale getters for lazy reactive integration', () => {
|
|
let locale = 'en-US';
|
|
const curr = createEngineCurrency({ locale: () => locale, currency: AUTO_CURRENCY });
|
|
|
|
expect(curr.getCurrency()).toBe('USD');
|
|
locale = 'en-GB';
|
|
expect(curr.getCurrency()).toBe('GBP');
|
|
});
|
|
|
|
it('allows manual currency override and returning to auto mode', () => {
|
|
const curr = createEngineCurrency({ locale: 'es-AR' });
|
|
|
|
expect(curr.getCurrency()).toBe('ARS');
|
|
curr.setCurrency('EUR');
|
|
expect(curr.getCurrency()).toBe('EUR');
|
|
expect(curr.isCurrencyAuto()).toBe(false);
|
|
curr.clearCurrency();
|
|
expect(curr.getCurrency()).toBe('ARS');
|
|
expect(curr.isCurrencyAuto()).toBe(true);
|
|
});
|
|
|
|
it('formats using Intl currency options', () => {
|
|
const curr = createEngineCurrency({ locale: 'en-US', currency: 'USD' });
|
|
|
|
expect(curr.format(12.5, { minDecimals: 2, maxDecimals: 2 })).toBe('$12.50');
|
|
expect(curr.formatAs(12.5, 'EUR', { currencyDisplay: 'code' })).toContain('EUR');
|
|
});
|
|
|
|
it('converts values through the rates provider', async () => {
|
|
const rates = createRates({
|
|
initial: {
|
|
base: 'EUR',
|
|
rates: { USD: 2 }
|
|
}
|
|
});
|
|
const curr = createEngineCurrency({ locale: 'es-ES', rates });
|
|
|
|
await expect(curr.convert(10, 'USD')).resolves.toBe(20);
|
|
await expect(curr.convertAs(10, 'USD', 'EUR')).resolves.toBe(5);
|
|
expect(curr.ratesSupported).toBe(true);
|
|
});
|
|
|
|
it('logs missing rates provider at debug level', async () => {
|
|
const logger = createEngineLogger({ level: LogLevel.DEBUG, transports: [] });
|
|
const curr = createEngineCurrency({ locale: 'es-ES', logger });
|
|
|
|
await expect(curr.convert(10, 'USD')).resolves.toBeUndefined();
|
|
|
|
const logs = logger.getLogs({ category: FMTS_CURR_MODULE, level: LogLevel.DEBUG });
|
|
expect(logs).toHaveLength(1);
|
|
expect(logs[0].message).toBe(CURRENCY_ERRORS.RATES_PROVIDER_MISSING);
|
|
});
|
|
});
|