lion/packages/ui/components/input-amount/test/formatters.test.js
2022-10-31 16:55:07 +01:00

75 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { expect } from '@open-wc/testing';
import { localize } from '@lion/ui/localize.js';
import { localizeTearDown } from '@lion/ui/localize-test-helpers.js';
import { formatAmount } from '@lion/ui/input-amount.js';
describe('formatAmount()', () => {
afterEach(() => {
localizeTearDown();
});
it('formats number with options', async () => {
expect(
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, {
locale: 'en-GB',
minimumFractionDigits: 3,
maximumFractionDigits: 3,
}),
).to.equal('12.346');
expect(
formatAmount(-12.345678, {
locale: 'en-GB',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}),
).to.equal('12.35');
});
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');
});
});