diff --git a/test/helpers.money.test.ts b/test/helpers.money.test.ts index ff426ed..1f13e21 100644 --- a/test/helpers.money.test.ts +++ b/test/helpers.money.test.ts @@ -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); + }); + }); }); \ No newline at end of file