lion/packages/ui/components/input-amount/test/parsers.test.js
Bilal Özlü 9a4a8735b0
fix: editing negative numbers (#2538)
* fix: editing negative numbers

* fix: editing negative numbers (changeset added)
2025-07-17 14:20:26 +02:00

79 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 { parseAmount } from '@lion/ui/input-amount.js';
describe('parseAmount()', async () => {
const localizeManager = getLocalizeManager();
beforeEach(() => {
localizeManager.locale = 'en-GB';
});
afterEach(() => {
localizeTearDown();
});
it('with currency set to correct amount of decimals', async () => {
expect(
parseAmount('1.015', {
currency: 'EUR',
}),
).to.equal(1.02);
expect(
parseAmount('5.555', {
currency: 'EUR',
}),
).to.equal(5.56);
expect(
parseAmount('100.1235', {
currency: 'JPY',
}),
).to.equal(100);
expect(
parseAmount('100.1235', {
currency: 'JOD',
}),
).to.equal(100.124);
});
it('with no currency keeps all decimals', async () => {
expect(parseAmount('1.015')).to.equal(1.015);
});
// TODO: make it work with big numbers, e.g. make use of BigInt
it.skip('rounds up big numbers', async () => {
// eslint-disable-next-line no-loss-of-precision
expect(parseAmount('999999999999999999999,42')).to.equal(999999999999999999999.42);
// eslint-disable-next-line no-loss-of-precision
expect(parseAmount('12,345,678,987,654,321.42')).to.equal(12345678987654321.42);
});
it('returns undefined if an invalid value is entered', async () => {
expect(parseAmount('foo')).to.equal(undefined);
expect(parseAmount('foo1')).to.equal(undefined);
expect(parseAmount('EUR 1,50')).to.equal(undefined);
expect(parseAmount('--1')).to.equal(undefined);
});
it('ignores letters when "pasted" mode used', async () => {
expect(parseAmount('foo1', { mode: 'pasted' })).to.equal(1);
expect(parseAmount('EUR 1,50', { mode: 'pasted' })).to.equal(1.5);
});
it('parses based on locale when "user-edited" mode used combined with viewValueStates "formatted"', async () => {
expect(parseAmount('123,456.78', { mode: 'auto' })).to.equal(123456.78);
expect(
parseAmount('123,456.78', { mode: 'user-edited', viewValueStates: ['formatted'] }),
).to.equal(123456.78);
expect(parseAmount('123.456,78', { mode: 'auto' })).to.equal(123456.78);
expect(
parseAmount('123.456,78', { mode: 'user-edited', viewValueStates: ['formatted'] }),
).to.equal(123456.78);
});
it('parses negative numbers', async () => {
expect(parseAmount('15.00')).to.equal(-15);
expect(parseAmount('-333.33')).to.equal(-333.33);
});
});