lion/packages/ui/components/input-amount/test/formatters.test.js
gerjanvangeest e8e9c07ec5
fix(input-amount): returns Unparseable as a modelValue if a wrong value has been entered (#2242)
* fix(input-amount): returns Unparseable as a modelValue if a wrong value has been entered

* fix(input-amount): do not break when a large amount has been entered

* Update docs/components/input-amount/overview.md

Co-authored-by: Thijs Louisse <thijs.louisse@ing.com>

* Update packages/ui/components/input-amount/test/parsers.test.js

Co-authored-by: Thijs Louisse <thijs.louisse@ing.com>

---------

Co-authored-by: Thijs Louisse <thijs.louisse@ing.com>
2024-04-10 11:20:48 +02:00

88 lines
2.6 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 { getLocalizeManager } from '@lion/ui/localize-no-side-effects.js';
import { localizeTearDown } from '@lion/ui/localize-test-helpers.js';
import { formatAmount } from '@lion/ui/input-amount.js';
describe('formatAmount()', () => {
const localizeManager = getLocalizeManager();
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 () => {
localizeManager.locale = 'en-GB';
expect(formatAmount(12345678)).to.equal('12,345,678.00');
localizeManager.locale = 'nl-NL';
expect(formatAmount(12345678)).to.equal('12.345.678,00');
});
// TODO: make it work with big numbers, e.g. make use of BigInt
it.skip('rounds up big numbers', async () => {
expect(formatAmount(1e21, { locale: 'en-GB', currency: 'EUR' })).to.equal(
'1,000,000,000,000,000,000,000.00',
);
// eslint-disable-next-line no-loss-of-precision
expect(formatAmount(12345678987654321.42, { locale: 'en-GB', currency: 'EUR' })).to.equal(
'12,345,678,987,654,321.42',
);
});
});