chore(localize): make number formatting tests more scalable

This commit is contained in:
Mikhail Bashkirov 2019-06-27 09:57:51 +02:00
parent 2f352008b1
commit 86c9824f07
5 changed files with 228 additions and 260 deletions

View file

@ -10,45 +10,36 @@ const currencySymbol = currency => ({ style: 'currency', currencyDisplay: 'symbo
describe('formatNumber', () => {
afterEach(localizeTearDown);
it('displays the appropriate amount of decimal places based on currencies spec http://www.currency-iso.org/en/home/tables/table-a1.html', async () => {
it('displays the appropriate amount of decimal places based on currencies spec http://www.currency-iso.org/en/home/tables/table-a1.html', () => {
const clean = str => str.replace(/[a-zA-Z]+/g, '').trim();
expect(clean(formatNumber(123456.789, currencyCode('JPY')))).to.equal('123,457');
expect(clean(formatNumber(123456.789, currencyCode('EUR')))).to.equal('123,456.79');
expect(clean(formatNumber(123456.789, currencyCode('BHD')))).to.equal('123,456.789');
});
it('can display currency as code', () => {
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
});
it('can display currency as symbol', () => {
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
localize.locale = 'nl-NL';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123.457 ¥');
localize.locale = 'fr-FR';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123 456,79 EUR');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123 457 ¥');
localize.locale = 'de-DE';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123.457 ¥');
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
});
it('can display currency as code', async () => {
localize.locale = 'nl-NL';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123.456,79 USD');
});
it('can display currency as symbol', async () => {
localize.locale = 'nl-NL';
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123.456,79 €');
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123.456,79 $');
});
it('uses minus (and not dash) to indicate negative numbers ', async () => {
it('uses minus (and not dash) to indicate negative numbers ', () => {
expect(formatNumber(-12, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('-12');
});
it('rounds (negative) numbers e.g. `roundMode: round`', async () => {
expect(formatNumber(12.4, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('12');
expect(formatNumber(12.6, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('13');
it('rounds (negative) numbers e.g. `roundMode: round`', () => {
expect(formatNumber(12.4, { roundMode: 'round' })).to.equal('12');
expect(formatNumber(12.6, { roundMode: 'round' })).to.equal('13');
expect(formatNumber(-12.4, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('-12');
expect(formatNumber(-12.6, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('-13');
expect(formatNumber(-12.4, { roundMode: 'round' })).to.equal('-12');
expect(formatNumber(-12.6, { roundMode: 'round' })).to.equal('-13');
});
it("rounds (negative) numbers up when `roundMode: 'ceiling'`", async () => {
it("rounds (negative) numbers up when `roundMode: 'ceiling'`", () => {
expect(formatNumber(12.4, { roundMode: 'ceiling' })).to.equal('13');
expect(formatNumber(12.6, { roundMode: 'ceiling' })).to.equal('13');
@ -56,7 +47,7 @@ describe('formatNumber', () => {
expect(formatNumber(-12.6, { roundMode: 'ceiling' })).to.equal('-12');
});
it('rounds (negative) numbers down when `roundMode: floor`', async () => {
it('rounds (negative) numbers down when `roundMode: floor`', () => {
expect(formatNumber(12.4, { roundMode: 'floor' })).to.equal('12');
expect(formatNumber(12.6, { roundMode: 'floor' })).to.equal('12');
@ -64,24 +55,28 @@ describe('formatNumber', () => {
expect(formatNumber(-12.6, { roundMode: 'floor' })).to.equal('-13');
});
it('returns empty string when NaN', async () => {
it('returns empty string when NaN', () => {
expect(formatNumber('foo')).to.equal('');
});
it('returns empty string when number is undefined', async () => {
it('returns empty string when number is undefined', () => {
expect(formatNumber(undefined)).to.equal('');
});
it('uses `localize.formatNumberOptions.returnIfNaN`', async () => {
it('uses `localize.formatNumberOptions.returnIfNaN`', () => {
const savedReturnIfNaN = localize.formatNumberOptions.returnIfNaN;
localize.formatNumberOptions.returnIfNaN = '-';
expect(formatNumber('foo')).to.equal('-');
localize.formatNumberOptions.returnIfNaN = savedReturnIfNaN;
});
it("can set what to returns when NaN via `returnIfNaN: 'foo'`", async () => {
it("can set what to returns when NaN via `returnIfNaN: 'foo'`", () => {
expect(formatNumber('foo', { returnIfNaN: '-' })).to.equal('-');
});
it('uses `localize.locale`', async () => {
it('uses `localize.locale`', () => {
expect(formatNumber(123456.789, { style: 'decimal', maximumFractionDigits: 2 })).to.equal(
'123,456.79',
);
@ -91,7 +86,7 @@ describe('formatNumber', () => {
);
});
it('can set locale to use', async () => {
it('can set locale to use', () => {
expect(
formatNumber(123456.789, { locale: 'en-GB', style: 'decimal', maximumFractionDigits: 2 }),
).to.equal('123,456.79');
@ -100,7 +95,7 @@ describe('formatNumber', () => {
).to.equal('123.456,79');
});
it('can specify max decimal places by `maximumFractionDigits: 3`', async () => {
it('can specify max decimal places by `maximumFractionDigits: 3`', () => {
expect(formatNumber(123456.789)).to.equal('123,456.789');
expect(formatNumber(123456.789, { style: 'decimal', maximumFractionDigits: 3 })).to.equal(
'123,456.789',
@ -110,7 +105,7 @@ describe('formatNumber', () => {
);
});
it('can specify min decimal places by `minimumFractionDigits: 3`', async () => {
it('can specify min decimal places by `minimumFractionDigits: 3`', () => {
expect(formatNumber(12.3)).to.equal('12.3');
expect(formatNumber(12.3456, { style: 'decimal', minimumFractionDigits: 3 })).to.equal(
'12.346',
@ -118,16 +113,16 @@ describe('formatNumber', () => {
expect(formatNumber(12.3, { style: 'decimal', minimumFractionDigits: 3 })).to.equal('12.300');
});
it('can specify to show at least x digits by `minimumIntegerDigits: 5`', async () => {
it('can specify to show at least x digits by `minimumIntegerDigits: 5`', () => {
expect(formatNumber(123)).to.equal('123');
expect(formatNumber(123, { minimumIntegerDigits: 5 })).to.equal('00,123');
});
it('can display 0 decimal places', async () => {
it('can display 0 decimal places', () => {
expect(formatNumber(12.4, { style: 'decimal', maximumFractionDigits: 0 })).to.equal('12');
});
it('formats numbers correctly', async () => {
it('formats numbers correctly', () => {
localize.locale = 'nl-NL';
expect(formatNumber(0, { style: 'decimal', minimumFractionDigits: 2 })).to.equal('0,00');
expect(formatNumber(0.1, { style: 'decimal', minimumFractionDigits: 2 })).to.equal('0,10');
@ -172,7 +167,7 @@ describe('formatNumber', () => {
).to.equal('112.345.678,00');
});
it('formats 2-digit decimals correctly', async () => {
it('formats 2-digit decimals correctly', () => {
localize.locale = 'nl-NL';
Array.from(new Array(100), (val, index) => index).forEach(i => {
const iString = `${i}`;
@ -185,32 +180,118 @@ describe('formatNumber', () => {
});
describe('normalization', () => {
it('supports British locale', async () => {
describe('en-GB', () => {
it('supports basics', () => {
localize.locale = 'en-GB';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('JPY 123,457');
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('¥123,457');
});
});
it('supports US locale', async () => {
describe('en-US', () => {
it('supports basics', () => {
localize.locale = 'en-US';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('JPY 123,457');
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('¥123,457');
});
});
it('supports Bulgarian locale', async () => {
describe('en-AU', () => {
it('supports basics', () => {
localize.locale = 'en-AU';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('JPY 123,457');
// expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79'); // TODO: fix
// expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79'); // TODO: fix
// expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('¥123,457'); // TODO: fix
});
});
describe('en-PH', () => {
it('supports basics', () => {
localize.locale = 'en-PH';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('EUR 123,456.79');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('USD 123,456.79');
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('JPY 123,457');
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('€123,456.79');
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('$123,456.79');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('¥123,457');
});
});
describe('nl-NL', () => {
it('supports basics', () => {
localize.locale = 'nl-NL';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123.456,79 USD');
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123.457 JPY');
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123.456,79 €');
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123.456,79 $');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123.457 ¥');
});
});
describe('nl-BE', () => {
it('supports basics', () => {
localize.locale = 'nl-BE';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123.456,79 EUR');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123.456,79 USD');
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123.457 JPY');
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123.456,79 €');
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123.456,79 $');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123.457 ¥');
});
});
describe('fr-FR', () => {
it('supports basics', () => {
localize.locale = 'fr-FR';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123 456,79 EUR');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123 456,79 USD');
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123 457 JPY');
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123 456,79 €');
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123 456,79 $');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123 457 ¥');
});
});
describe('fr-BE', () => {
it('supports basics', () => {
localize.locale = 'fr-FR';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123 456,79 EUR');
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123 456,79 USD');
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123 457 JPY');
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123 456,79 €');
expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123 456,79 $');
expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123 457 ¥');
});
});
describe('bg-BG', () => {
it('supports basics', () => {
localize.locale = 'bg-BG';
expect(formatNumber(123456.789, currencyCode('EUR'))).to.equal('123 456,79 EUR');
expect(formatNumber(1234567890.789, currencyCode('USD'))).to.equal('1 234 567 890,79 USD');
expect(formatNumber(12.789, currencyCode('EUR'))).to.equal('12,79 EUR');
expect(formatNumber(12, currencyCode('USD'))).to.equal('12,00 USD');
expect(formatNumber(12.789, { style: 'decimal' })).to.equal('12,789');
expect(formatNumber(12, { style: 'decimal', minimumFractionDigits: 3 })).to.equal('12,000');
expect(formatNumber(20000, { style: 'decimal', minimumFractionDigits: 3 })).to.equal(
'20 000,000',
);
expect(formatNumber(123456.789, currencyCode('USD'))).to.equal('123 456,79 USD');
expect(formatNumber(123456.789, currencyCode('JPY'))).to.equal('123 457 JPY');
expect(formatNumber(123456.789, currencySymbol('EUR'))).to.equal('123 456,79 €');
// expect(formatNumber(123456.789, currencySymbol('USD'))).to.equal('123 456,79 $'); // TODO: fix
// expect(formatNumber(123456.789, currencySymbol('JPY'))).to.equal('123 457 ¥'); // TODO: fix
});
it('normalizes group separator', () => {
localize.locale = 'bg-BG';
expect(formatNumber(1.234, currencyCode('EUR'))).to.equal('1,23 EUR');
expect(formatNumber(1234.567, currencyCode('EUR'))).to.equal('1 234,57 EUR');
});
});
});
});

View file

@ -4,212 +4,99 @@ import { localizeTearDown } from '../../test-helpers.js';
import { formatNumberToParts } from '../../src/number/formatNumberToParts.js';
const c = v => ({ type: 'currency', value: v });
const d = v => ({ type: 'decimal', value: v });
const i = v => ({ type: 'integer', value: v });
const f = v => ({ type: 'fraction', value: v });
const g = v => ({ type: 'group', value: v });
const l = v => ({ type: 'literal', value: v });
const m = { type: 'minusSign', value: '-' };
const stringifyParts = parts => parts.map(part => part.value).join('');
describe('formatNumberToParts', () => {
afterEach(localizeTearDown);
describe('formats based on ISO standards', () => {
describe("style: 'currency'", () => {
const specs = [
['nl-NL', 'EUR', 1234.5, '1.234,50 EUR'],
['nl-NL', 'USD', 1234.5, '1.234,50 USD'],
['nl-NL', 'EUR', -1234.5, '-1.234,50 EUR'],
['nl-BE', 'EUR', 1234.5, '1.234,50 EUR'],
['nl-BE', 'USD', 1234.5, '1.234,50 USD'],
['nl-BE', 'EUR', -1234.5, '-1.234,50 EUR'],
['en-GB', 'EUR', 1234.5, 'EUR 1,234.50'],
['en-GB', 'USD', 1234.5, 'USD 1,234.50'],
['en-GB', 'EUR', -1234.5, '-EUR 1,234.50'],
['de-DE', 'EUR', 1234.5, '1.234,50 EUR'],
['de-DE', 'USD', 1234.5, '1.234,50 USD'],
['de-DE', 'EUR', -1234.5, '-1.234,50 EUR'],
['fr-BE', 'EUR', 1234.5, '1 234,50 EUR'],
['fr-BE', 'USD', 1234.5, '1 234,50 USD'],
['fr-BE', 'EUR', -1234.5, '-1 234,50 EUR'],
['en-GB', 'EUR', 1234.5, [c('EUR'), l(' '), i('1'), g(','), i('234'), d('.'), f('50')]],
['en-GB', 'EUR', -1234.5, [m, c('EUR'), l(' '), i('1'), g(','), i('234'), d('.'), f('50')]],
['nl-NL', 'EUR', 1234.5, [i('1'), g('.'), i('234'), d(','), f('50'), l(' '), c('EUR')]],
['nl-NL', 'EUR', -1234.5, [m, i('1'), g('.'), i('234'), d(','), f('50'), l(' '), c('EUR')]],
['nl-BE', 'EUR', 1234.5, [i('1'), g('.'), i('234'), d(','), f('50'), l(' '), c('EUR')]],
['nl-BE', 'EUR', -1234.5, [m, i('1'), g('.'), i('234'), d(','), f('50'), l(' '), c('EUR')]],
['fr-FR', 'EUR', 1234.5, [i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('EUR')]],
['fr-FR', 'EUR', -1234.5, [m, i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('EUR')]],
['fr-BE', 'EUR', 1234.5, [i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('EUR')]],
['fr-BE', 'EUR', -1234.5, [m, i('1'), g(' '), i('234'), d(','), f('50'), l(' '), c('EUR')]],
];
specs.forEach(spec => {
const [locale, currency, amount, expectedResult] = spec;
it(`formats ${locale} ${currency} ${amount} as ${expectedResult}`, () => {
specs.forEach(([locale, currency, amount, expectedResult]) => {
it(`formats ${locale} ${currency} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
localize.locale = locale;
const parts = formatNumberToParts(amount, {
expect(
formatNumberToParts(amount, {
style: 'currency',
currencyDisplay: 'code',
currency,
currencyDisplay: 'code',
});
const joinedParts = parts.map(p => p.value).join('');
expect(joinedParts).to.equal(expectedResult);
}),
).to.deep.equal(expectedResult);
});
});
});
it('supports currency symbol with dutch locale', async () => {
localize.locale = 'nl-NL';
const formattedToParts = formatNumberToParts(3500, {
style: 'currency',
currency: 'EUR',
currencyDisplay: 'symbol',
describe("style: 'decimal'", () => {
describe('no minimumFractionDigits', () => {
const specs = [
['en-GB', 3500, [i('3'), g(','), i('500')]],
['en-GB', -3500, [m, i('3'), g(','), i('500')]],
['nl-NL', 3500, [i('3'), g('.'), i('500')]],
['nl-NL', -3500, [m, i('3'), g('.'), i('500')]],
['nl-BE', 3500, [i('3'), g('.'), i('500')]],
['nl-BE', -3500, [m, i('3'), g('.'), i('500')]],
['fr-FR', 3500, [i('3'), g(' '), i('500')]],
['fr-FR', -3500, [m, i('3'), g(' '), i('500')]],
['fr-BE', 3500, [i('3'), g(' '), i('500')]],
['fr-BE', -3500, [m, i('3'), g(' '), i('500')]],
];
specs.forEach(([locale, amount, expectedResult]) => {
it(`formats ${locale} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
localize.locale = locale;
expect(
formatNumberToParts(amount, {
style: 'decimal',
}),
).to.deep.equal(expectedResult);
});
});
expect(formattedToParts).to.eql([
{ type: 'integer', value: '3' },
{ type: 'group', value: '.' },
{ type: 'integer', value: '500' },
{ type: 'decimal', value: ',' },
{ type: 'fraction', value: '00' },
{ type: 'literal', value: ' ' },
{ type: 'currency', value: '€' },
]);
});
it('supports currency symbol with french locale', async () => {
localize.locale = 'fr-FR';
const formattedToParts = formatNumberToParts(3500, {
style: 'currency',
currency: 'EUR',
currencyDisplay: 'symbol',
});
expect(Object.keys(formattedToParts).length).to.equal(7);
expect(formattedToParts[0].type).to.equal('integer');
expect(formattedToParts[0].value).to.equal('3');
expect(formattedToParts[1].type).to.equal('group');
expect(formattedToParts[1].value).to.equal(' ');
expect(formattedToParts[5].type).to.equal('literal');
expect(formattedToParts[5].value).to.equal(' ');
expect(formattedToParts[6].type).to.equal('currency');
expect(formattedToParts[6].value).to.equal('€');
});
describe('minimumFractionDigits: 2', () => {
const specs = [
['en-GB', 3500, [i('3'), g(','), i('500'), d('.'), f('00')]],
['en-GB', -3500, [m, i('3'), g(','), i('500'), d('.'), f('00')]],
['nl-NL', 3500, [i('3'), g('.'), i('500'), d(','), f('00')]],
['nl-NL', -3500, [m, i('3'), g('.'), i('500'), d(','), f('00')]],
['nl-BE', 3500, [i('3'), g('.'), i('500'), d(','), f('00')]],
['nl-BE', -3500, [m, i('3'), g('.'), i('500'), d(','), f('00')]],
['fr-FR', 3500, [i('3'), g(' '), i('500'), d(','), f('00')]],
['fr-FR', -3500, [m, i('3'), g(' '), i('500'), d(','), f('00')]],
['fr-BE', 3500, [i('3'), g(' '), i('500'), d(','), f('00')]],
['fr-BE', -3500, [m, i('3'), g(' '), i('500'), d(','), f('00')]],
];
it('supports currency symbol with British locale', async () => {
localize.locale = 'en-GB';
const formattedToParts = formatNumberToParts(3500, {
style: 'currency',
currency: 'EUR',
currencyDisplay: 'symbol',
});
expect(Object.keys(formattedToParts).length).to.equal(6);
expect(formattedToParts[2].type).to.equal('group');
expect(formattedToParts[2].value).to.equal(',');
expect(formattedToParts[4].type).to.equal('decimal');
expect(formattedToParts[4].value).to.equal('.');
expect(formattedToParts[5].type).to.equal('fraction');
expect(formattedToParts[5].value).to.equal('00');
});
it('supports currency code with dutch locale', async () => {
localize.locale = 'nl-NL';
const formattedToParts = formatNumberToParts(3500, {
style: 'currency',
currency: 'EUR',
currencyDisplay: 'code',
});
expect(Object.keys(formattedToParts).length).to.equal(7);
expect(formattedToParts[1].type).to.equal('group');
expect(formattedToParts[1].value).to.equal('.');
expect(formattedToParts[3].type).to.equal('decimal');
expect(formattedToParts[3].value).to.equal(',');
expect(formattedToParts[5].type).to.equal('literal');
expect(formattedToParts[5].value).to.equal(' ');
expect(formattedToParts[6].type).to.equal('currency');
expect(formattedToParts[6].value).to.equal('EUR');
});
it('supports currency code with french locale', async () => {
localize.locale = 'fr-FR';
const formattedToParts = formatNumberToParts(3500, {
style: 'currency',
currency: 'EUR',
currencyDisplay: 'code',
});
expect(Object.keys(formattedToParts).length).to.equal(7);
expect(formattedToParts[1].type).to.equal('group');
expect(formattedToParts[1].value).to.equal(' ');
expect(formattedToParts[3].type).to.equal('decimal');
expect(formattedToParts[3].value).to.equal(',');
expect(formattedToParts[4].type).to.equal('fraction');
expect(formattedToParts[4].value).to.equal('00');
});
it('supports currency code with British locale', async () => {
localize.locale = 'en-GB';
const formattedToParts = formatNumberToParts(3500, {
style: 'currency',
currency: 'EUR',
currencyDisplay: 'code',
});
expect(Object.keys(formattedToParts).length).to.equal(7);
expect(formattedToParts[3].type).to.equal('group');
expect(formattedToParts[3].value).to.equal(',');
expect(formattedToParts[5].type).to.equal('decimal');
expect(formattedToParts[5].value).to.equal('.');
});
it('supports currency with dutch locale and 2 decimals', async () => {
localize.locale = 'nl-NL';
const formattedToParts = formatNumberToParts(3500, {
specs.forEach(([locale, amount, expectedResult]) => {
it(`formats ${locale} ${amount} as "${stringifyParts(expectedResult)}"`, () => {
localize.locale = locale;
expect(
formatNumberToParts(amount, {
style: 'decimal',
minimumFractionDigits: 2,
});
expect(Object.keys(formattedToParts).length).to.equal(5);
expect(formattedToParts[0].type).to.equal('integer');
expect(formattedToParts[0].value).to.equal('3');
expect(formattedToParts[1].type).to.equal('group');
expect(formattedToParts[1].value).to.equal('.');
expect(formattedToParts[2].type).to.equal('integer');
expect(formattedToParts[2].value).to.equal('500');
expect(formattedToParts[3].type).to.equal('decimal');
expect(formattedToParts[3].value).to.equal(',');
expect(formattedToParts[4].type).to.equal('fraction');
expect(formattedToParts[4].value).to.equal('00');
});
it('supports currency with french locale and 2 decimals', async () => {
localize.locale = 'fr-FR';
const formattedToParts = formatNumberToParts(3500, {
style: 'decimal',
minimumFractionDigits: 2,
});
expect(Object.keys(formattedToParts).length).to.equal(5);
expect(formattedToParts[1].type).to.equal('group');
expect(formattedToParts[1].value).to.equal(' ');
expect(formattedToParts[3].type).to.equal('decimal');
expect(formattedToParts[3].value).to.equal(',');
});
it('supports currency with british locale and 2 decimals', async () => {
localize.locale = 'en-GB';
const formattedToParts = formatNumberToParts(3500, {
style: 'decimal',
minimumFractionDigits: 2,
});
expect(Object.keys(formattedToParts).length).to.equal(5);
expect(formattedToParts[1].type).to.equal('group');
expect(formattedToParts[1].value).to.equal(',');
expect(formattedToParts[3].type).to.equal('decimal');
expect(formattedToParts[3].value).to.equal('.');
});
it('supports currency with dutch locale without decimals', async () => {
localize.locale = 'nl-NL';
const formattedToParts = formatNumberToParts(3500, { style: 'decimal' });
expect(Object.keys(formattedToParts).length).to.equal(3);
expect(formattedToParts[1].type).to.equal('group');
expect(formattedToParts[1].value).to.equal('.');
expect(formattedToParts[2].type).to.equal('integer');
expect(formattedToParts[2].value).to.equal('500');
});
it('supports currency with french locale without decimals', async () => {
localize.locale = 'fr-FR';
const formattedToParts = formatNumberToParts(3500, { style: 'decimal' });
expect(Object.keys(formattedToParts).length).to.equal(3);
expect(formattedToParts[1].type).to.equal('group');
expect(formattedToParts[1].value).to.equal(' ');
});
it('supports currency with british locale without decimals', async () => {
localize.locale = 'en-GB';
const formattedToParts = formatNumberToParts(3500, { style: 'decimal' });
expect(Object.keys(formattedToParts).length).to.equal(3);
expect(formattedToParts[1].type).to.equal('group');
expect(formattedToParts[1].value).to.equal(',');
}),
).to.deep.equal(expectedResult);
});
});
});
});
});

View file

@ -3,7 +3,7 @@ import { expect } from '@open-wc/testing';
import { getDecimalSeparator } from '../../src/number/getDecimalSeparator.js';
describe('getDecimalSeparator', () => {
it('returns decimal separator for locale', async () => {
it('returns decimal separator for locale', () => {
expect(getDecimalSeparator('en-GB')).to.equal('.');
expect(getDecimalSeparator('nl-NL')).to.equal(',');
expect(getDecimalSeparator('fr-FR')).to.equal(',');

View file

@ -3,7 +3,7 @@ import { expect } from '@open-wc/testing';
import { getFractionDigits } from '../../src/number/getFractionDigits.js';
describe('getFractionDigits', () => {
it('returns number of fraction digits for currency', async () => {
it('returns number of fraction digits for currency', () => {
expect(getFractionDigits('JPY')).to.equal(0);
expect(getFractionDigits('EUR')).to.equal(2);
expect(getFractionDigits('BHD')).to.equal(3);

View file

@ -3,7 +3,7 @@ import { expect } from '@open-wc/testing';
import { getGroupSeparator } from '../../src/number/getGroupSeparator.js';
describe('getGroupSeparator', () => {
it('returns group separator for locale', async () => {
it('returns group separator for locale', () => {
expect(getGroupSeparator('en-GB')).to.equal(',');
expect(getGroupSeparator('nl-NL')).to.equal('.');
expect(getGroupSeparator('fr-FR')).to.equal(' ');