chore(input-amount): improve tests

This commit is contained in:
Mikhail Bashkirov 2019-06-25 10:41:06 +02:00
parent 48d2e547d6
commit 0b8785ccf6
2 changed files with 57 additions and 10 deletions

View file

@ -1,25 +1,70 @@
import { expect } from '@open-wc/testing';
import { localize } from '@lion/localize';
import { localizeTearDown } from '@lion/localize/test-helpers.js';
import { formatAmount } from '../src/formatters.js';
describe('formatAmount()', () => {
it('formats number for specific locale', async () => {
localize.locale = 'en-GB';
expect(formatAmount(12345678)).to.equal('12,345,678.00');
expect(formatAmount(12.345678)).to.equal('12.35');
// TODO: Document that maximumFractionDigits >= minimumFractionDigits else a RangeError is thrown by Intl
afterEach(() => {
localizeTearDown();
});
// TODO: Document that maximumFractionDigits >= minimumFractionDigits else a RangeError is thrown by Intl
it('formats number with options', async () => {
expect(
formatAmount(12.345678, { minimumFractionDigits: 0, maximumFractionDigits: 1 }),
formatAmount(12.345678, {
locale: 'en-GB',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}),
).to.equal('12.35');
expect(
formatAmount(12.345678, {
locale: 'nl-NL',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}),
).to.equal('12,35');
expect(
formatAmount(12345678, {
locale: 'en-GB',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}),
).to.equal('12,345,678.00');
expect(
formatAmount(12345678, {
locale: 'nl-NL',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}),
).to.equal('12.345.678,00');
expect(
formatAmount(12.345678, {
locale: 'en-GB',
minimumFractionDigits: 0,
maximumFractionDigits: 1,
}),
).to.equal('12.3');
expect(
formatAmount(12.345678, { minimumFractionDigits: 3, maximumFractionDigits: 3 }),
formatAmount(12.345678, {
locale: 'en-GB',
minimumFractionDigits: 3,
maximumFractionDigits: 3,
}),
).to.equal('12.346');
});
it('formats the right amount of fraction digits for a certain currency', async () => {
expect(formatAmount(123456.78, { locale: 'en-GB', currency: 'EUR' })).to.equal('123,456.78');
expect(formatAmount(123456.78, { locale: 'en-GB', currency: 'JOD' })).to.equal('123,456.780');
});
it('fallbacks to global locale and EUR by default', async () => {
localize.locale = 'en-GB';
expect(formatAmount(12345678)).to.equal('12,345,678.00');
localize.locale = 'nl-NL';
expect(formatAmount(12345678)).to.equal('12.345.678,00');
expect(formatAmount(12345678, { locale: 'nl-NL' })).to.equal('12.345.678,00');
expect(formatAmount(123456.78, { locale: 'nl-NL' })).to.equal('123.456,78');
expect(formatAmount(123456.78, { locale: 'nl-NL', currency: 'JOD' })).to.equal('123.456,780');
});
});

View file

@ -114,7 +114,9 @@ describe('parseAmount()', () => {
it('ignores all non-number symbols (including currency)', () => {
expect(parseAmount('€ 1,234.56')).to.equal(1234.56);
expect(parseAmount('1,234.56 €')).to.equal(1234.56);
expect(parseAmount('EUR 1,234.56')).to.equal(1234.56);
expect(parseAmount('1,234.56 EUR')).to.equal(1234.56);
expect(parseAmount('Number is 1,234.56')).to.equal(1234.56);
});