formatters.spec.ts
1 import configuredStore from 'features/store'; 2 import { Wei } from 'libs/units'; 3 import { toFixedIfLarger, formatNumber, formatGasLimit, formatMnemonic } from 'utils/formatters'; 4 5 configuredStore.getState(); 6 7 describe('toFixedIfLarger', () => { 8 it('should return same value if decimal isnt longer than default', () => { 9 const numExample = 7.002; 10 expect(toFixedIfLarger(numExample)).toEqual(String(numExample)); 11 }); 12 13 it('should return shortened value rounded up if decimal is longer than default', () => { 14 const numExample = 7.1234567; 15 expect(toFixedIfLarger(numExample)).toEqual(String(7.123457)); 16 }); 17 it('should return shortened value if decimal is longer than passed fixedSize', () => { 18 const numExample = 7.12345678; 19 expect(toFixedIfLarger(numExample, 2)).toEqual(String(7.12)); 20 }); 21 }); 22 23 describe('formatNumber', () => { 24 const pairs = [ 25 { 26 input: '0.0127491', 27 output: '0.013', 28 digits: undefined 29 }, 30 { 31 input: '21.87468421', 32 output: '21.875', 33 digits: undefined 34 }, 35 { 36 input: '0', 37 output: '0', 38 digits: undefined 39 }, 40 { 41 input: '354.4728173', 42 output: '354.4728', 43 digits: 4 44 }, 45 { 46 input: '100.48391', 47 output: '100', 48 digits: 0 49 }, 50 { 51 input: '239.999632', 52 output: '240', 53 digits: 0 54 }, 55 { 56 input: '999.999', 57 output: '1,000', 58 digits: 0 59 }, 60 { 61 input: '0.9', 62 output: '1', 63 digits: 0 64 }, 65 { 66 input: '0.09', 67 output: '0.1', 68 digits: 1 69 } 70 ]; 71 72 pairs.forEach(pair => { 73 const digits = pair.digits; 74 it(`should convert ${pair.input.toString()} to ${ 75 pair.output 76 } when using ${digits} digits`, () => { 77 expect(formatNumber(pair.input, pair.digits)).toEqual(pair.output); 78 }); 79 }); 80 }); 81 82 describe('formatGasLimit', () => { 83 it('should fix transaction gas limit off-by-one errors', () => { 84 expect(formatGasLimit(Wei('21001'), 'ETH')).toEqual('21000'); 85 }); 86 87 it('should mark the gas limit `-1` if you exceed the limit per block', () => { 88 expect(formatGasLimit(Wei('999999999999999'), 'ETH')).toEqual('-1'); 89 }); 90 91 it('should not alter a valid gas limit', () => { 92 expect(formatGasLimit(Wei('1234'))).toEqual('1234'); 93 }); 94 }); 95 96 describe('formatMnemonic', () => { 97 const testPhraseNewLines = 98 'first\ncatalog\naway\nfaculty\njelly\nnow\nlife\nkingdom\npigeon\nraise\ngain\naccident'; 99 const testPhraseExtraSpaces = 100 'first catalog away faculty jelly now life kingdom pigeon raise gain accident '; 101 const testPhraseCommas = 102 'first,catalog,away,faculty,jelly,now,life,kingdom,pigeon,raise,gain,accident'; 103 const formattedTestPhrase = 104 'first catalog away faculty jelly now life kingdom pigeon raise gain accident'; 105 106 it('should format phrases with new lines as a phrase with just spaces', () => { 107 expect(formatMnemonic(testPhraseNewLines)).toEqual(formattedTestPhrase); 108 }); 109 110 it('should remove commas and replace with space characters', () => { 111 expect(formatMnemonic(testPhraseCommas)).toEqual(formattedTestPhrase); 112 }); 113 114 it('should trim any stray space characters throughout the phrase', () => { 115 expect(formatMnemonic(testPhraseExtraSpaces)).toEqual(formattedTestPhrase); 116 }); 117 });