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
5 changes: 5 additions & 0 deletions .changeset/orange-cities-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/money-input': minor
---

Support zero-fraction currency variants (CZK0, HUF0, ILS0, KZT0, TRY0, TWD0) and fix empty-value fraction digits in MoneyInput
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,29 @@ describe('LocalizedMoneyInput.convertToMoneyValues', () => {
]);
});
});

describe('when called with a zero-fraction variant currency (CZK0)', () => {
it('should return centPrecision with 0 fraction digits when locale is provided', () => {
expect(
LocalizedMoneyInput.convertToMoneyValues(
{
CZK0: {
currencyCode: 'CZK0',
amount: '100',
},
},
'en'
)
).toEqual([
{
type: 'centPrecision',
currencyCode: 'CZK0',
centAmount: 100,
fractionDigits: 0,
},
]);
});
});
});

describe('LocalizedMoneyInput.parseMoneyValues', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/components/inputs/money-input/src/currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ const currencies = {
ZWL: { fractionDigits: 2 },
ZWN: { fractionDigits: 2 },
ZWR: { fractionDigits: 2 },
// Zero-fraction variants (non-ISO): base currency with 0 decimal places.
CZK0: { fractionDigits: 0 },
HUF0: { fractionDigits: 0 },
ILS0: { fractionDigits: 0 },
KZT0: { fractionDigits: 0 },
TRY0: { fractionDigits: 0 },
TWD0: { fractionDigits: 0 },
} as const;

export default currencies;
36 changes: 36 additions & 0 deletions packages/components/inputs/money-input/src/money-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@
});
});

describe('when a zero-fraction variant currency (CZK0) is used', () => {
it('should return centPrecision with 0 fraction digits and whole-number centAmount', () => {
expect(
MoneyInput.convertToMoneyValue(
{ currencyCode: 'CZK0', amount: '100' },
'en'
)
).toEqual({
type: 'centPrecision',
currencyCode: 'CZK0',
centAmount: 100,
fractionDigits: 0,
});
});
it('should warn that locale is required when locale is not passed', () => {
MoneyInput.convertToMoneyValue({ currencyCode: 'CZK0', amount: '50' });
expect(consoleWarnMock).toHaveBeenCalledWith(
'Warning: MoneyInput: A locale must be provided when currency has no fraction digits (CZK0)'
);
});
});

describe('when no amount is present', () => {
it('should return an invalid object', () => {
expect(
Expand Down Expand Up @@ -341,7 +363,7 @@
},
'de-CH'
)
).toEqual({ amount: '1’234.567', currencyCode: 'EUR' });

Check failure on line 366 in packages/components/inputs/money-input/src/money-input.spec.js

View workflow job for this annotation

GitHub Actions / build_lint_and_test

MoneyInput.parseMoneyValue › when called with a locale that uses right quote (0x2019) or single quote as fraction separator for 🇨🇭 › should parse the value according to the passed locale when separator is right quote

expect(received).toEqual(expected) // deep equality - Expected - 1 + Received + 1 Object { - "amount": "1’234.567", + "amount": "1'234.567", "currencyCode": "EUR", } at Object.toEqual (packages/components/inputs/money-input/src/money-input.spec.js:366:9)
});
});

Expand Down Expand Up @@ -373,6 +395,20 @@
).toEqual({ amount: '12.34', currencyCode: 'EUR' });
});
});
describe('when called with a zero-fraction variant currency (HUF0)', () => {
it('should format amount with 0 fraction digits for display', () => {
expect(
MoneyInput.parseMoneyValue(
{
currencyCode: 'HUF0',
centAmount: 500,
fractionDigits: 0,
},
'en'
)
).toEqual({ amount: '500', currencyCode: 'HUF0' });
});
});
describe('when called with a minimal highPrecision price', () => {
it('should turn it into a value', () => {
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ const createEmptyMoneyValue = (currencyCode: TCurrencyCode): TMoneyValue => ({
type: 'centPrecision',
currencyCode,
centAmount: NaN,
fractionDigits: 2,
fractionDigits: allCurrencies[currencyCode].fractionDigits ?? 2,
});

const getAmountAsNumberFromMoneyValue = (moneyValue: TMoneyValue) =>
Expand Down
Loading