|
1 |
| -import { looselyParseAmount, getNumberFormat, setNumberFormat } from './util'; |
| 1 | +import { |
| 2 | + looselyParseAmount, |
| 3 | + getNumberFormat, |
| 4 | + setNumberFormat, |
| 5 | + currencyToAmount, |
| 6 | +} from './util'; |
2 | 7 |
|
3 | 8 | describe('utility functions', () => {
|
4 | 9 | test('looseParseAmount works with basic numbers', () => {
|
@@ -108,4 +113,63 @@ describe('utility functions', () => {
|
108 | 113 | formatter = getNumberFormat().formatter;
|
109 | 114 | expect(formatter.format(Number('1234.56'))).toBe('1’235');
|
110 | 115 | });
|
| 116 | + |
| 117 | + test('currencyToAmount works with basic numbers', () => { |
| 118 | + expect(currencyToAmount('3')).toBe(3); |
| 119 | + expect(currencyToAmount('3.4')).toBe(3.4); |
| 120 | + expect(currencyToAmount('3.45')).toBe(3.45); |
| 121 | + expect(currencyToAmount('3.45060')).toBe(3.4506); |
| 122 | + }); |
| 123 | + |
| 124 | + test('currencyToAmount works with varied formats', () => { |
| 125 | + setNumberFormat({ format: 'comma-dot', hideFraction: true }); |
| 126 | + expect(currencyToAmount('3,45')).toBe(3.45); |
| 127 | + expect(currencyToAmount('3,456')).toBe(3456); |
| 128 | + expect(currencyToAmount('3,45000')).toBe(345000); |
| 129 | + expect(currencyToAmount("3'456.78")).toBe(3456.78); |
| 130 | + expect(currencyToAmount("3'456.78000")).toBe(3456.78); |
| 131 | + expect(currencyToAmount('1,00,000.99')).toBe(100000.99); |
| 132 | + expect(currencyToAmount('1,00,000.99000')).toBe(100000.99); |
| 133 | + }); |
| 134 | + |
| 135 | + test('currencyToAmount works with leading decimal characters', () => { |
| 136 | + expect(currencyToAmount('.45')).toBe(0.45); |
| 137 | + expect(currencyToAmount(',45')).toBe(0.45); |
| 138 | + }); |
| 139 | + |
| 140 | + test('currencyToAmount works with negative numbers', () => { |
| 141 | + expect(currencyToAmount('-3')).toBe(-3); |
| 142 | + expect(currencyToAmount('-3.45')).toBe(-3.45); |
| 143 | + expect(currencyToAmount('-3,45')).toBe(-3.45); |
| 144 | + }); |
| 145 | + |
| 146 | + test('currencyToAmount works with non-fractional numbers', () => { |
| 147 | + setNumberFormat({ format: 'comma-dot', hideFraction: false }); |
| 148 | + expect(currencyToAmount('3.')).toBe(3); |
| 149 | + expect(currencyToAmount('3,')).toBe(3); |
| 150 | + expect(currencyToAmount('3,000')).toBe(3000); |
| 151 | + expect(currencyToAmount('3,000.')).toBe(3000); |
| 152 | + }); |
| 153 | + |
| 154 | + test('currencyToAmount works with hidden fractions', () => { |
| 155 | + setNumberFormat({ format: 'comma-dot', hideFraction: true }); |
| 156 | + expect(currencyToAmount('3.45')).toBe(3.45); |
| 157 | + expect(currencyToAmount('3.456')).toBe(3.456); |
| 158 | + expect(currencyToAmount('3.4500')).toBe(3.45); |
| 159 | + expect(currencyToAmount('3.')).toBe(3); |
| 160 | + expect(currencyToAmount('3,')).toBe(3); |
| 161 | + expect(currencyToAmount('3,000')).toBe(3000); |
| 162 | + expect(currencyToAmount('3,000.')).toBe(3000); |
| 163 | + }); |
| 164 | + |
| 165 | + test('currencyToAmount works with dot-comma', () => { |
| 166 | + setNumberFormat({ format: 'dot-comma', hideFraction: false }); |
| 167 | + expect(currencyToAmount('3,45')).toBe(3.45); |
| 168 | + expect(currencyToAmount('3,456')).toBe(3.456); |
| 169 | + expect(currencyToAmount('3,4500')).toBe(3.45); |
| 170 | + expect(currencyToAmount('3,')).toBe(3); |
| 171 | + expect(currencyToAmount('3.')).toBe(3); |
| 172 | + expect(currencyToAmount('3.000')).toBe(3000); |
| 173 | + expect(currencyToAmount('3.000,')).toBe(3000); |
| 174 | + }); |
111 | 175 | });
|
0 commit comments