Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/components/__tests__/CurrencyInput-abbreviated.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,40 @@ describe('<CurrencyInput/> abbreviated', () => {
expect(screen.getByRole('textbox')).toHaveValue('£1,599,000,000');
});

it('should handle 4.1m without floating-point precision issues', () => {
render(<CurrencyInput prefix="£" onValueChange={onValueChangeSpy} />);
userEvent.type(screen.getByRole('textbox'), '4.1m');

expect(onValueChangeSpy).toHaveBeenLastCalledWith('4100000', undefined, {
float: 4100000,
formatted: '£4,100,000',
value: '4100000',
});

expect(screen.getByRole('textbox')).toHaveValue('£4,100,000');
});

it('should handle other problematic decimal abbreviations', () => {
render(<CurrencyInput prefix="$" onValueChange={onValueChangeSpy} decimalsLimit={3} />);

userEvent.type(screen.getByRole('textbox'), '1.025m');
expect(onValueChangeSpy).toHaveBeenLastCalledWith('1025000', undefined, {
float: 1025000,
formatted: '$1,025,000',
value: '1025000',
});
expect(screen.getByRole('textbox')).toHaveValue('$1,025,000');

userEvent.clear(screen.getByRole('textbox'));
userEvent.type(screen.getByRole('textbox'), '2.1k');
expect(onValueChangeSpy).toHaveBeenLastCalledWith('2100', undefined, {
float: 2100,
formatted: '$2,100',
value: '2100',
});
expect(screen.getByRole('textbox')).toHaveValue('$2,100');
});

it('should not abbreviate any other letters', () => {
render(<CurrencyInput prefix="£" onValueChange={onValueChangeSpy} />);
userEvent.type(screen.getByRole('textbox'), '1.5e');
Expand Down
32 changes: 32 additions & 0 deletions src/components/utils/__tests__/cleanValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,38 @@ describe('cleanValue', () => {
).toEqual('');
});

it('should handle floating-point precision issues in abbreviations', () => {
expect(
cleanValue({
value: '4.1m',
})
).toEqual('4100000');

expect(
cleanValue({
value: '-4.11B',
})
).toEqual('-4110000000');

expect(
cleanValue({
value: '1.025m',
})
).toEqual('1025000');

expect(
cleanValue({
value: '2.1k',
})
).toEqual('2100');

expect(
cleanValue({
value: '3.1m',
})
).toEqual('3100000');
});

it('should ignore abbreviations if disableAbbreviations is true', () => {
expect(
cleanValue({
Expand Down
105 changes: 105 additions & 0 deletions src/components/utils/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import * as utils from '../index';

describe('utils index exports', () => {
it('should export cleanValue function', () => {
expect(utils.cleanValue).toBeDefined();
expect(typeof utils.cleanValue).toBe('function');
});

it('should export fixedDecimalValue function', () => {
expect(utils.fixedDecimalValue).toBeDefined();
expect(typeof utils.fixedDecimalValue).toBe('function');
});

it('should export formatValue function', () => {
expect(utils.formatValue).toBeDefined();
expect(typeof utils.formatValue).toBe('function');
});

it('should export getLocaleConfig function', () => {
expect(utils.getLocaleConfig).toBeDefined();
expect(typeof utils.getLocaleConfig).toBe('function');
});

it('should export getSuffix function', () => {
expect(utils.getSuffix).toBeDefined();
expect(typeof utils.getSuffix).toBe('function');
});

it('should export isNumber function', () => {
expect(utils.isNumber).toBeDefined();
expect(typeof utils.isNumber).toBe('function');
});

it('should export padTrimValue function', () => {
expect(utils.padTrimValue).toBeDefined();
expect(typeof utils.padTrimValue).toBe('function');
});

it('should export repositionCursor function', () => {
expect(utils.repositionCursor).toBeDefined();
expect(typeof utils.repositionCursor).toBe('function');
});

it('should export safeMultiply function', () => {
expect(utils.safeMultiply).toBeDefined();
expect(typeof utils.safeMultiply).toBe('function');
});

describe('exported functions should work correctly', () => {
it('cleanValue should clean values', () => {
const result = utils.cleanValue({ value: '1,000' });
expect(result).toBe('1000');
});

it('fixedDecimalValue should fix decimals', () => {
const result = utils.fixedDecimalValue('123', '.', 2);
expect(result).toBe('1.23');
});

it('formatValue should format values', () => {
const result = utils.formatValue({ value: '1000' });
expect(result).toBe('1,000');
});

it('getLocaleConfig should return locale config', () => {
const result = utils.getLocaleConfig({ locale: 'en-US', currency: 'USD' });
expect(result).toHaveProperty('prefix');
expect(result).toHaveProperty('groupSeparator');
expect(result).toHaveProperty('decimalSeparator');
});

it('getSuffix should get suffix', () => {
const result = utils.getSuffix('100%', { groupSeparator: ',', decimalSeparator: '.' });
expect(result).toBe('%');
});

it('isNumber should validate numbers', () => {
expect(utils.isNumber('123')).toBe(true);
expect(utils.isNumber('abc')).toBe(false);
});

it('padTrimValue should pad/trim values', () => {
const result = utils.padTrimValue('1.5', '.', 2);
expect(result).toBe('1.50');
});

it('repositionCursor should calculate cursor position', () => {
const result = utils.repositionCursor({
selectionStart: 1,
value: '1000',
lastKeyStroke: '1',
stateValue: '',
groupSeparator: ',',
});
expect(result).toHaveProperty('modifiedValue');
expect(result).toHaveProperty('cursorPosition');
expect(typeof result.modifiedValue).toBe('string');
});

it('safeMultiply should multiply safely', () => {
const result = utils.safeMultiply(4.1, 1000000);
expect(result).toBe(4100000);
});
});
});
53 changes: 53 additions & 0 deletions src/components/utils/__tests__/parseAbbrValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ describe('abbrValue', () => {
expect(abbrValue(123456, '.')).toEqual('0.123456M');
expect(abbrValue(123456, '.', 2)).toEqual('0.12M');
});

describe('floating-point precision in abbreviation', () => {
it('should handle values with potential precision issues', () => {
expect(abbrValue(4100000)).toEqual('4.1M');
expect(abbrValue(1025000)).toEqual('1.025M');
expect(abbrValue(3100000)).toEqual('3.1M');
expect(abbrValue(2100000)).toEqual('2.1M');
});

it('should handle k abbreviations', () => {
expect(abbrValue(1500)).toEqual('1.5k');
expect(abbrValue(2100)).toEqual('2.1k');
});
});
});

describe('parseAbbrValue', () => {
Expand Down Expand Up @@ -77,4 +91,43 @@ describe('parseAbbrValue', () => {
expect(parseAbbrValue('1,2k', ',')).toEqual(1200);
expect(parseAbbrValue('2,3m', ',')).toEqual(2300000);
});

describe('floating-point precision fixes', () => {
it('should handle 4.1M without precision issues', () => {
expect(parseAbbrValue('4.1m')).toBe(4100000);
expect(parseAbbrValue('4.1M')).toBe(4100000);
});

it('should handle 1.025M without precision issues', () => {
expect(parseAbbrValue('1.025m')).toBe(1025000);
});

it('should handle 4.111M without precision issues', () => {
expect(parseAbbrValue('4.111m')).toBe(4111000);
});

it('should handle 3.1M without precision issues', () => {
expect(parseAbbrValue('3.1m')).toBe(3100000);
});

it('should handle 2.1M without precision issues', () => {
expect(parseAbbrValue('2.1m')).toBe(2100000);
});

it('should handle problematic decimal values with k', () => {
expect(parseAbbrValue('1.5k')).toBe(1500);
expect(parseAbbrValue('2.1k')).toBe(2100);
});

it('should handle problematic decimal values with b', () => {
expect(parseAbbrValue('1.1b')).toBe(1100000000);
expect(parseAbbrValue('2.5b')).toBe(2500000000);
});

it('should handle negative abbreviated values', () => {
expect(parseAbbrValue('-4.1m')).toBe(-4100000);
expect(parseAbbrValue('-1.5k')).toBe(-1500);
expect(parseAbbrValue('-2.5b')).toBe(-2500000000);
});
});
});
Loading
Loading