1import { toFixed, getValueFormat, scaledUnits, formattedValueToString } from './valueFormats'; 2import { DecimalCount } from '../types/displayValue'; 3import { TimeZone } from '../types'; 4import { dateTime } from '../datetime'; 5 6interface ValueFormatTest { 7 id: string; 8 decimals?: DecimalCount; 9 scaledDecimals?: DecimalCount; 10 timeZone?: TimeZone; 11 value: number; 12 result: string; 13} 14 15describe('valueFormats', () => { 16 it.each` 17 format | decimals | value | expected 18 ${'currencyUSD'} | ${2} | ${1532.82} | ${'$1.53K'} 19 ${'currencyKRW'} | ${2} | ${1532.82} | ${'₩1.53K'} 20 ${'currencyIDR'} | ${2} | ${1532.82} | ${'Rp1.53K'} 21 ${'none'} | ${undefined} | ${3.23} | ${'3.23'} 22 ${'none'} | ${undefined} | ${0.0245} | ${'0.0245'} 23 ${'none'} | ${undefined} | ${1 / 3} | ${'0.333'} 24 ${'ms'} | ${4} | ${0.0024} | ${'0.0024 ms'} 25 ${'ms'} | ${0} | ${100} | ${'100 ms'} 26 ${'ms'} | ${2} | ${1250} | ${'1.25 s'} 27 ${'ms'} | ${1} | ${10000086.123} | ${'2.8 hour'} 28 ${'ms'} | ${undefined} | ${1000} | ${'1 s'} 29 ${'ms'} | ${0} | ${1200} | ${'1 s'} 30 ${'short'} | ${undefined} | ${1000} | ${'1 K'} 31 ${'short'} | ${undefined} | ${1200} | ${'1.20 K'} 32 ${'short'} | ${undefined} | ${1250} | ${'1.25 K'} 33 ${'short'} | ${undefined} | ${1000000} | ${'1 Mil'} 34 ${'short'} | ${undefined} | ${1500000} | ${'1.50 Mil'} 35 ${'short'} | ${undefined} | ${1000120} | ${'1.00 Mil'} 36 ${'short'} | ${undefined} | ${98765} | ${'98.8 K'} 37 ${'short'} | ${undefined} | ${9876543} | ${'9.88 Mil'} 38 ${'short'} | ${undefined} | ${9876543} | ${'9.88 Mil'} 39 ${'kbytes'} | ${undefined} | ${10000000} | ${'9.54 GiB'} 40 ${'deckbytes'} | ${undefined} | ${10000000} | ${'10 GB'} 41 ${'megwatt'} | ${3} | ${1000} | ${'1.000 GW'} 42 ${'kohm'} | ${3} | ${1000} | ${'1.000 MΩ'} 43 ${'Mohm'} | ${3} | ${1000} | ${'1.000 GΩ'} 44 ${'farad'} | ${3} | ${1000} | ${'1.000 kF'} 45 ${'µfarad'} | ${3} | ${1000} | ${'1.000 mF'} 46 ${'nfarad'} | ${3} | ${1000} | ${'1.000 µF'} 47 ${'pfarad'} | ${3} | ${1000} | ${'1.000 nF'} 48 ${'ffarad'} | ${3} | ${1000} | ${'1.000 pF'} 49 ${'henry'} | ${3} | ${1000} | ${'1.000 kH'} 50 ${'mhenry'} | ${3} | ${1000} | ${'1.000 H'} 51 ${'µhenry'} | ${3} | ${1000} | ${'1.000 mH'} 52 ${'a'} | ${0} | ${1532.82} | ${'1533 a'} 53 ${'b'} | ${0} | ${1532.82} | ${'1533 b'} 54 ${'prefix:b'} | ${undefined} | ${1532.82} | ${'b1533'} 55 ${'suffix:d'} | ${undefined} | ${1532.82} | ${'1533 d'} 56 ${'si:µF'} | ${2} | ${1234} | ${'1.23 mF'} 57 ${'si:µF'} | ${2} | ${1234000000} | ${'1.23 kF'} 58 ${'si:µF'} | ${2} | ${1234000000000000} | ${'1.23 GF'} 59 ${'count:xpm'} | ${2} | ${1234567} | ${'1.23M xpm'} 60 ${'count:x/min'} | ${2} | ${1234} | ${'1.23K x/min'} 61 ${'currency:@'} | ${2} | ${1234567} | ${'@1.23M'} 62 ${'currency:@'} | ${2} | ${1234} | ${'@1.23K'} 63 ${'time:YYYY'} | ${0} | ${dateTime(new Date(1999, 6, 2)).valueOf()} | ${'1999'} 64 ${'time:YYYY.MM'} | ${0} | ${dateTime(new Date(2010, 6, 2)).valueOf()} | ${'2010.07'} 65 ${'dateTimeAsIso'} | ${0} | ${dateTime(new Date(2010, 6, 2)).valueOf()} | ${'2010-07-02 00:00:00'} 66 ${'dateTimeAsUS'} | ${0} | ${dateTime(new Date(2010, 6, 2)).valueOf()} | ${'07/02/2010 12:00:00 am'} 67 ${'dateTimeAsSystem'} | ${0} | ${dateTime(new Date(2010, 6, 2)).valueOf()} | ${'2010-07-02 00:00:00'} 68 ${'dtdurationms'} | ${undefined} | ${100000} | ${'1 minute'} 69 `( 70 'With format=$format decimals=$decimals and value=$value then result shoudl be = $expected', 71 async ({ format, value, decimals, expected }) => { 72 const result = getValueFormat(format)(value, decimals, undefined, undefined); 73 const full = formattedValueToString(result); 74 expect(full).toBe(expected); 75 } 76 ); 77 78 it('Manually check a format', () => { 79 // helpful for adding tests one at a time with the debugger 80 const tests: ValueFormatTest[] = [ 81 { id: 'time:YYYY.MM', decimals: 0, value: dateTime(new Date(2010, 6, 2)).valueOf(), result: '2010.07' }, 82 ]; 83 const test = tests[0]; 84 const result = getValueFormat(test.id)(test.value, test.decimals, test.scaledDecimals); 85 const full = formattedValueToString(result); 86 expect(full).toBe(test.result); 87 }); 88 89 describe('normal cases', () => { 90 it('toFixed should handle number correctly if decimal is null', () => { 91 expect(toFixed(100)).toBe('100'); 92 93 expect(toFixed(100.4)).toBe('100'); 94 expect(toFixed(100.5)).toBe('101'); 95 }); 96 97 it('toFixed should handle number correctly if decimal is not null', () => { 98 expect(toFixed(100, 1)).toBe('100.0'); 99 100 expect(toFixed(100.37, 1)).toBe('100.4'); 101 expect(toFixed(100.63, 1)).toBe('100.6'); 102 103 expect(toFixed(100.4, 2)).toBe('100.40'); 104 expect(toFixed(100.5, 2)).toBe('100.50'); 105 }); 106 }); 107 108 describe('format edge cases', () => { 109 const negInf = Number.NEGATIVE_INFINITY.toLocaleString(); 110 const posInf = Number.POSITIVE_INFINITY.toLocaleString(); 111 112 it('toFixed should handle non number input gracefully', () => { 113 expect(toFixed(NaN)).toBe('NaN'); 114 expect(toFixed(Number.NEGATIVE_INFINITY)).toBe(negInf); 115 expect(toFixed(Number.POSITIVE_INFINITY)).toBe(posInf); 116 }); 117 118 it('scaledUnits should handle non number input gracefully', () => { 119 const disp = scaledUnits(5, ['a', 'b', 'c']); 120 expect(disp(NaN).text).toBe('NaN'); 121 expect(disp(Number.NEGATIVE_INFINITY).text).toBe(negInf); 122 expect(disp(Number.POSITIVE_INFINITY).text).toBe(posInf); 123 }); 124 }); 125 126 describe('toFixed and negative decimals', () => { 127 it('should treat as zero decimals', () => { 128 const str = toFixed(186.123, -2); 129 expect(str).toBe('186'); 130 }); 131 }); 132 133 describe('Resolve old units', () => { 134 it('resolve farenheit', () => { 135 const fmt0 = getValueFormat('farenheit'); 136 const fmt1 = getValueFormat('fahrenheit'); 137 expect(fmt0).toEqual(fmt1); 138 }); 139 }); 140}); 141