|
3 | 3 | formatLocalizedDecimal, |
4 | 4 | formatBreakpointMods, |
5 | 5 | getElementLocale, |
| 6 | + getLocalizedInputWidthChars, |
6 | 7 | getUniqueId, |
7 | 8 | debounce, |
8 | 9 | isElementInView, |
@@ -114,18 +115,62 @@ test('formatBreakpointMods', () => { |
114 | 115 | expect(formatBreakpointMods({ default: 'column', lg: 'row' }, styles)).toEqual('pf-m-column pf-m-row-on-lg'); |
115 | 116 | }); |
116 | 117 |
|
117 | | -test('parseLocalizedDecimal accepts dot and comma decimal separators', () => { |
118 | | - const enFormatter = new Intl.NumberFormat('en'); |
119 | | - const esFormatter = new Intl.NumberFormat('es'); |
| 118 | +test('parseLocalizedDecimal accepts locale-formatted decimals', () => { |
| 119 | + const enFormatter = new Intl.NumberFormat('en', { useGrouping: false }); |
| 120 | + const esFormatter = new Intl.NumberFormat('es', { useGrouping: false }); |
| 121 | + const deFormatter = new Intl.NumberFormat('de-DE', { useGrouping: false }); |
120 | 122 |
|
121 | 123 | expect(parseLocalizedDecimal('50.2', enFormatter)).toBe(50.2); |
122 | 124 | expect(parseLocalizedDecimal('50,2', esFormatter)).toBe(50.2); |
123 | | - expect(parseLocalizedDecimal('1.234,56', esFormatter)).toBe(1234.56); |
124 | | - expect(parseLocalizedDecimal('1,234.56', enFormatter)).toBe(1234.56); |
| 125 | + expect(parseLocalizedDecimal('0,625', deFormatter)).toBe(0.625); |
125 | 126 | expect(parseLocalizedDecimal('', enFormatter)).toBeNaN(); |
126 | 127 | }); |
127 | 128 |
|
| 129 | +test('parseLocalizedDecimal rejects mismatched separators', () => { |
| 130 | + const enFormatter = new Intl.NumberFormat('en-US', { useGrouping: false }); |
| 131 | + const deFormatter = new Intl.NumberFormat('de-DE', { useGrouping: false }); |
| 132 | + |
| 133 | + expect(parseLocalizedDecimal('50,2', enFormatter)).toBeNaN(); |
| 134 | + expect(parseLocalizedDecimal('50.2', deFormatter)).toBeNaN(); |
| 135 | + expect(parseLocalizedDecimal('50,2', deFormatter)).toBe(50.2); |
| 136 | +}); |
| 137 | + |
| 138 | +test('parseLocalizedDecimal rejects grouping separators', () => { |
| 139 | + const enFormatter = new Intl.NumberFormat('en-US'); |
| 140 | + const esFormatter = new Intl.NumberFormat('es'); |
| 141 | + const deFormatter = new Intl.NumberFormat('de-DE'); |
| 142 | + const enINFormatter = new Intl.NumberFormat('en-IN'); |
| 143 | + |
| 144 | + expect(parseLocalizedDecimal('1,234.56', enFormatter)).toBeNaN(); |
| 145 | + expect(parseLocalizedDecimal('12.345,67', esFormatter)).toBeNaN(); |
| 146 | + expect(parseLocalizedDecimal('1.234,56', deFormatter)).toBeNaN(); |
| 147 | + expect(parseLocalizedDecimal('12,34,567', enINFormatter)).toBeNaN(); |
| 148 | +}); |
| 149 | + |
| 150 | +test('parseLocalizedDecimal rejects malformed numeric tokens', () => { |
| 151 | + const enFormatter = new Intl.NumberFormat('en-US'); |
| 152 | + |
| 153 | + expect(parseLocalizedDecimal('12abc', enFormatter)).toBeNaN(); |
| 154 | + expect(parseLocalizedDecimal('1,2abc', enFormatter)).toBeNaN(); |
| 155 | + expect(parseLocalizedDecimal('12-3', enFormatter)).toBeNaN(); |
| 156 | + expect(parseLocalizedDecimal('--12', enFormatter)).toBeNaN(); |
| 157 | + expect(parseLocalizedDecimal('+-12', enFormatter)).toBeNaN(); |
| 158 | + expect(parseLocalizedDecimal('12.3.4', enFormatter)).toBeNaN(); |
| 159 | + expect(parseLocalizedDecimal('abc', enFormatter)).toBeNaN(); |
| 160 | + |
| 161 | + expect(parseLocalizedDecimal('-50.2', enFormatter)).toBe(-50.2); |
| 162 | + expect(parseLocalizedDecimal('+12', enFormatter)).toBe(12); |
| 163 | + expect(parseLocalizedDecimal('1,234.56', enFormatter)).toBeNaN(); |
| 164 | +}); |
| 165 | + |
128 | 166 | test('formatLocalizedDecimal uses locale decimal separator', () => { |
129 | 167 | expect(formatLocalizedDecimal(50.2, new Intl.NumberFormat('en-US'))).toBe('50.2'); |
130 | 168 | expect(formatLocalizedDecimal(50.2, new Intl.NumberFormat('de-DE'))).toBe('50,2'); |
131 | 169 | }); |
| 170 | + |
| 171 | +test('getLocalizedInputWidthChars stays wide enough for shorter formatted values', () => { |
| 172 | + const formatter = new Intl.NumberFormat('de-DE', { useGrouping: false }); |
| 173 | + |
| 174 | + expect(getLocalizedInputWidthChars(formatter, 0, 1, '1')).toBe(formatLocalizedDecimal(0.99, formatter).length); |
| 175 | + expect(getLocalizedInputWidthChars(formatter, 0, 100, '5')).toBe(formatLocalizedDecimal(100, formatter).length); |
| 176 | +}); |
0 commit comments