|
1 |
| -import { formatNumber } from '@/pages/shared/lib/utils'; |
| 1 | +import { |
| 2 | + formatNumber, |
| 3 | + formatCurrency, |
| 4 | + getCurrencySymbol, |
| 5 | +} from '@/pages/shared/lib/utils'; |
2 | 6 |
|
3 | 7 | describe('formatNumber', () => {
|
4 | 8 | it('should display right format for integers', () => {
|
@@ -43,3 +47,52 @@ describe('formatNumber', () => {
|
43 | 47 | expect(formatNumber(0.000100009, 9)).toEqual('0.000100009');
|
44 | 48 | });
|
45 | 49 | });
|
| 50 | + |
| 51 | +describe('getCurrencySymbol', () => { |
| 52 | + it('should return currency symbol for common cases', () => { |
| 53 | + expect(getCurrencySymbol('USD')).toEqual('$'); |
| 54 | + expect(getCurrencySymbol('EUR')).toEqual('€'); |
| 55 | + expect(getCurrencySymbol('MXN')).toEqual('MX$'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return currency symbol for non-standard currencies', () => { |
| 59 | + expect(getCurrencySymbol('abc')).toEqual('ABC'); |
| 60 | + expect(getCurrencySymbol('ZZZ')).toEqual('ZZZ'); |
| 61 | + expect(getCurrencySymbol('AB')).toEqual('AB'); |
| 62 | + expect(getCurrencySymbol('ABCD')).toEqual('ABCD'); |
| 63 | + expect(getCurrencySymbol('ABcDe')).toEqual('ABCDE'); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('formatCurrency', () => { |
| 68 | + it('should display right format for common cases', () => { |
| 69 | + expect(formatCurrency(5, 'USD')).toEqual('$5.00'); |
| 70 | + expect(formatCurrency(0.5, 'USD')).toEqual('$0.50'); |
| 71 | + expect(formatCurrency(5.34, 'USD')).toEqual('$5.34'); |
| 72 | + expect(formatCurrency(5.34, 'EUR')).toEqual('€5.34'); |
| 73 | + expect(formatCurrency(5.34, 'MXN')).toEqual('MX$5.34'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should support custom precision', () => { |
| 77 | + expect(formatCurrency(5.12, 'USD', 2)).toEqual('$5.12'); |
| 78 | + expect(formatCurrency(5.12, 'USD', 4)).toEqual('$5.12'); |
| 79 | + expect(formatCurrency(5.19, 'EUR', 1)).toEqual('€5.2'); |
| 80 | + expect(formatCurrency(5.12058, 'USD', 4)).toEqual('$5.1206'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should display right format in different locales', () => { |
| 84 | + expect(formatCurrency(5.12, 'USD', 2, 'en-US')).toEqual('$5.12'); |
| 85 | + expect(formatCurrency(5.12, 'USD', 2, 'en-IN')).toEqual('$5.12'); |
| 86 | + expect(formatCurrency(5.34, 'USD', 2, 'fr')).toEqual('5,34\xa0$US'); // '\xa0' is a normal non-breaking space |
| 87 | + expect(formatCurrency(5.45, 'EUR', 2, 'en-US')).toEqual('€5.45'); |
| 88 | + expect(formatCurrency(5.67, 'EUR', 2, 'fr-FR')).toEqual('5,67\xa0€'); |
| 89 | + expect(formatCurrency(5.89, 'MXN', 2, 'es-MX')).toEqual('$5.89'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should support non-standard currencies', () => { |
| 93 | + expect(formatCurrency(5.12, 'ABC')).toEqual('ABC\xa05.12'); |
| 94 | + expect(formatCurrency(5.12, 'ZZZ')).toEqual('ZZZ\xa05.12'); |
| 95 | + expect(formatCurrency(5.12, 'AB')).toEqual('AB\xa05.12'); |
| 96 | + expect(formatCurrency(5.12, 'ABCDE')).toEqual('ABCDE\xa05.12'); |
| 97 | + }); |
| 98 | +}); |
0 commit comments