Skip to content

Commit

Permalink
test(money): add tests for formatMoney
Browse files Browse the repository at this point in the history
  • Loading branch information
bashunaimiroy committed Nov 27, 2024
1 parent 2190066 commit b418abb
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions test/helpers.money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,90 @@ describe('convertSubunitsToFloat', () => {
expect(sdk.helpers.money.convertSubunitsToFloat(subunits, currency)).toBe(float);
});
});
});

describe('formatMoney', () => {
it('should not show the currency, when the currency matches locale', () => {
const currencyAndLocaleList = [
{
money: {
currency: 'CAD',
amount: 123456,
formatted: '$1,234.56'
},
locale: 'en-CA',
},
{
money: {
currency: 'GBP',
amount: 123456,
formatted: '£1,234.56'
},
locale: 'en-GB',
},
{
money: {
currency: 'USD',
amount: 123456,
formatted: '$1,234.56'
},
locale: 'en-US',
},
];

currencyAndLocaleList.forEach(({ money, locale }) => {
expect(sdk.helpers.money.formatMoney(money, locale)).toBe(money.formatted);
});
});

it('should show the currency, when the currency does not match locale', () => {
// Note: this is only true for currencies that share a symbol, like "$".
// a currency like GBP will not show the currency; the £ symbol is sufficient
const currencyAndLocaleList = [
{
money: {
currency: 'CAD',
amount: 123456,
formatted: 'CA$1,234.56'
},
locale: 'en-US',
},
{
money: {
currency: 'USD',
amount: 123456,
formatted: 'US$1,234.56'
},
locale: 'en-CA',
},
];

currencyAndLocaleList.forEach(({ money, locale }) => {
expect(sdk.helpers.money.formatMoney(money, locale)).toBe(money.formatted);
});
});
it('should default to en-US, when the locale is invalid', () => {
const currencyAndLocaleList = [
{
money: {
currency: 'CAD',
amount: 123456,
formatted: 'CA$1,234.56'
},
locale: 'fake-FAKE',
},
{
money: {
currency: 'USD',
amount: 123456,
formatted: '$1,234.56'
},
locale: 'fake-FAKE',
},
];

currencyAndLocaleList.forEach(({ money, locale }) => {
expect(sdk.helpers.money.formatMoney(money, locale)).toBe(money.formatted);
});
});
});

0 comments on commit b418abb

Please sign in to comment.