Skip to content
Merged
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
56 changes: 56 additions & 0 deletions tests/money-amount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,62 @@ describe('MoneyAmount decimal normalization', () => {
expect(result.amount).toBe('1.2345678');
});

it('supports explicit scale and round options via options object', () => {
const input: MoneyAmount = { assetCode: 'USDC', amount: '1.234567' };
const truncated = normalizeMoneyAmount(input, { scale: 2 });
expect(truncated.amount).toBe('1.23');

const rounded = normalizeMoneyAmount(input, { scale: 2, round: true });
expect(rounded.amount).toBe('1.23');
});

it('rounds half-up when rounding is explicitly requested', () => {
const input: MoneyAmount = { assetCode: 'USDC', amount: '1.235' };
const result = normalizeMoneyAmount(input, 2, { round: true });
expect(result.amount).toBe('1.24');
});

it('rounds sub-cent amounts to target scale when requested', () => {
const input: MoneyAmount = { assetCode: 'XLM', amount: '0.0000009' };
const result = normalizeMoneyAmount(input, 6, { round: true });
expect(result.amount).toBe('0.000001');
});

it('rounds amounts exceeding 7 decimal places to Stellar maximum precision', () => {
const roundUp: MoneyAmount = { assetCode: 'XLM', amount: '0.00000019' };
expect(normalizeMoneyAmount(roundUp).amount).toBe('0.0000002');

const roundDown: MoneyAmount = { assetCode: 'XLM', amount: '0.00000012' };
expect(normalizeMoneyAmount(roundDown).amount).toBe('0.0000001');
});

it('throws RangeError for invalid scale values', () => {
const input: MoneyAmount = { assetCode: 'USDC', amount: '10.50' };
expect(() => normalizeMoneyAmount(input, -1)).toThrow(RangeError);
expect(() => normalizeMoneyAmount(input, 8)).toThrow(RangeError);
expect(() => normalizeMoneyAmount(input, 2.5)).toThrow(RangeError);
});

it('produces output accepted by validateMoneyAmount', () => {
const standard: MoneyAmount = { assetCode: 'USDC', amount: '0075.5' };
expect(() => {
validateMoneyAmount(normalizeMoneyAmount(standard), 'test');
}).not.toThrow();

const subCent: MoneyAmount = { assetCode: 'XLM', amount: '0.0000001' };
expect(() => {
validateMoneyAmount(normalizeMoneyAmount(subCent), 'test');
}).not.toThrow();

const rounded: MoneyAmount = { assetCode: 'XLM', amount: '0.0000009' };
expect(() => {
validateMoneyAmount(
normalizeMoneyAmount(rounded, 6, { round: true }),
'test',
);
}).not.toThrow();
});

it('handles amounts with leading zeros', () => {
const input: MoneyAmount = {
assetCode: 'USDC',
Expand Down