diff --git a/jest.config.js b/jest.config.js index 36b82bc6..a36ca0ca 100644 --- a/jest.config.js +++ b/jest.config.js @@ -6,6 +6,7 @@ module.exports = { testURL: 'http://localhost', modulePathIgnorePatterns: [ 'node_modules', + 'utils/build.js', 'utils/windowUtils.js', 'utils/promiseUtils.js', 'utils/networkUtils.js', @@ -14,6 +15,14 @@ module.exports = { 'utils/**.js', 'reducers/utils/**.js', ], + // coverageThreshold: { + // global: { + // branches: 100, + // functions: 100, + // lines: 100, + // statements: 100, + // }, + // }, setupFiles: [ './support/setupJest.js', ], diff --git a/src/reducers/utils/pendingTxsExamples.js b/src/reducers/utils/__mocks__/pendingTxs.js similarity index 100% rename from src/reducers/utils/pendingTxsExamples.js rename to src/reducers/utils/__mocks__/pendingTxs.js diff --git a/src/reducers/utils/__tests__/__snapshots__/index.test.js.snap b/src/reducers/utils/__tests__/__snapshots__/index.test.js.snap deleted file mode 100644 index 5efb9857..00000000 --- a/src/reducers/utils/__tests__/__snapshots__/index.test.js.snap +++ /dev/null @@ -1,55 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`reducers utils observeChanges shoud be the same - returns false 1`] = `false`; - -exports[`reducers utils observeChanges shoud be the same - returns false 2`] = `false`; - -exports[`reducers utils observeChanges shoud be the same - returns false 3`] = `false`; - -exports[`reducers utils observeChanges shoud be the same - returns false 4`] = `false`; - -exports[`reducers utils observeChanges shoud be the same - returns false 5`] = `false`; - -exports[`reducers utils observeChanges shoud be the same - returns false 6`] = `false`; - -exports[`reducers utils observeChanges shoud be the same - returns false 7`] = `false`; - -exports[`reducers utils observeChanges shoud be the same - returns false 8`] = `false`; - -exports[`reducers utils observeChanges shoud be the same - returns false 9`] = `false`; - -exports[`reducers utils observeChanges shoud be the same - returns false 10`] = `false`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 1`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 2`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 3`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 4`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 5`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 6`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 7`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 8`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 9`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 10`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 11`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 12`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 13`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 14`] = `true`; - -exports[`reducers utils observeChanges shoul NOT be the same - returns true 15`] = `true`; - -exports[`reducers utils observeChanges test filter 1`] = `false`; - -exports[`reducers utils observeChanges test filter 2`] = `true`; diff --git a/src/reducers/utils/__tests__/index.test.js b/src/reducers/utils/__tests__/index.test.js index 48c5d93b..d1b06099 100644 --- a/src/reducers/utils/__tests__/index.test.js +++ b/src/reducers/utils/__tests__/index.test.js @@ -1,148 +1,135 @@ -import * as reducerUtils from '../index'; +import * as utils from '../index'; describe('reducers utils', () => { - it('observeChanges shoud be the same - returns false', () => { - const data = [ - // example of same data (false) - { - previous: {}, - current: {}, - }, - { - previous: 1, - current: 1, - }, - { - previous: [], - current: [], - }, - { - previous: [1, 1, 1], - current: [1, 1, 1], - }, - { - previous: 'a', - current: 'a', - }, - { - previous: { one: 1 }, - current: { one: 1 }, - }, - { - previous: { one: { two: 1 } }, - current: { one: { two: 1 } }, - }, - { - previous: { one: { two: [1, 2, 3] } }, - current: { one: { two: [1, 2, 3] } }, - }, - { - previous: { one: { two: [1, { three: 1 }, 3] } }, - current: { one: { two: [1, { three: 1 }, 3] } }, - }, - { - previous: { one: { two: [1, { three: 1 }, { four: 3, five: { six: 3 } }] } }, - current: { one: { two: [1, { three: 1 }, { four: 3, five: { six: 3 } }] } }, - }, - ]; - - data.forEach((item) => { - expect(reducerUtils.observeChanges( - item.previous, item.current, - )).toMatchSnapshot(); - }); + it('observer changes should BE THE SAME - return false', () => { + expect(utils.observeChanges( + {}, + {}, + )).toBe(false); + + expect(utils.observeChanges( + [], + [], + )).toBe(false); + + expect(utils.observeChanges( + [1, 1, 1], + [1, 1, 1], + )).toBe(false); + + expect(utils.observeChanges( + 'a', + 'a', + )).toBe(false); + + expect(utils.observeChanges( + { one: 1 }, + { one: 1 }, + )).toBe(false); + + expect(utils.observeChanges( + { one: { two: 1 } }, + { one: { two: 1 } }, + )).toBe(false); + + expect(utils.observeChanges( + { one: { two: [1, 2, 3] } }, + { one: { two: [1, 2, 3] } }, + )).toBe(false); + + expect(utils.observeChanges( + { one: { two: [1, { three: 1 }, 3] } }, + { one: { two: [1, { three: 1 }, 3] } }, + )).toBe(false); + + expect(utils.observeChanges( + { one: { two: [1, { three: 1 }, { four: 3, five: { six: 3 } }] } }, + { one: { two: [1, { three: 1 }, { four: 3, five: { six: 3 } }] } }, + )).toBe(false); }); - it('observeChanges shoul NOT be the same - returns true', () => { - const data = [ - // example of different data (true) - { - previous: null, - current: {}, - }, - { - previous: { one: 1 }, - current: {}, - }, - { - previous: { one: 1, three: 3 }, - current: { one: 1, two: 2 }, - }, - { - previous: [{}, {}], - current: [], - }, - { - previous: [1, 1, 1], - current: [1, 1], - }, - { - previous: 'a', - current: 'b', - }, - { - previous: ['a'], - current: ['b'], - }, - { - previous: 1, - current: '1', - }, - { - previous: { one: 1 }, - current: { one: 2 }, - }, - { - previous: { one: { two: 1 } }, - current: { one: { two: 2 } }, - }, - { - previous: { one: { two: 1 } }, - current: { one: { two: 2 } }, - }, - { - previous: { one: { two: [1, 2, 3] } }, - current: { one: { two: [1, 1, 3] } }, - }, - { - previous: { one: { two: [1, { three: 1 }, 3] } }, - current: { one: { two: [1, { three: 2 }, 3] } }, - }, - { - previous: { one: { two: [1, { three: 1 }, { four: 3, five: { six: 3 } }] } }, - current: { one: { two: [1, { three: 1 }, { four: 3, five: { six: 1 } }] } }, - }, - { - previous: { one: { two: [1, { three: 1 }, { four: 3, five: { sixxx: 3 } }] } }, - current: { one: { two: [1, { three: 1 }, { four: 3, five: { six: 1 } }] } }, - }, - ]; - - data.forEach((item) => { - expect(reducerUtils.observeChanges( - item.previous, item.current, - )).toMatchSnapshot(); - }); + it('observer should NOT be the same - return true', () => { + expect(utils.observeChanges( + null, + {}, + )).toBe(true); + + expect(utils.observeChanges( + { one: 1 }, + {}, + )).toBe(true); + + expect(utils.observeChanges( + { one: 1, three: 3 }, { one: 1, two: 2 }, + )).toBe(true); + + expect(utils.observeChanges( + [{}, {}], + [], + )).toBe(true); + + expect(utils.observeChanges( + [1, 1, 1], + [1, 1], + )).toBe(true); + + expect(utils.observeChanges( + 'a', + 'b', + )).toBe(true); + + expect(utils.observeChanges( + ['a'], + ['b'], + )).toBe(true); + + expect(utils.observeChanges( + 1, + '1', + )).toBe(true); + + expect(utils.observeChanges( + { one: 1 }, + { one: 2 }, + )).toBe(true); + + expect(utils.observeChanges( + { one: { two: 1 } }, + { one: { two: 2 } }, + )).toBe(true); + + expect(utils.observeChanges( + { one: { two: [1, 2, 3] } }, + { one: { two: [1, 1, 3] } }, + )).toBe(true); + + expect(utils.observeChanges( + { one: { two: [1, { three: 1 }, 3] } }, + { one: { two: [1, { three: 2 }, 3] } }, + )).toBe(true); + + expect(utils.observeChanges( + { one: { two: [1, { three: 1 }, { four: 3, five: { six: 3 } }] } }, + { one: { two: [1, { three: 1 }, { four: 3, five: { six: 1 } }] } }, + )).toBe(true); + + expect(utils.observeChanges( + { one: { two: [1, { three: 1 }, { four: 3, five: { sixxx: 3 } }] } }, + { one: { two: [1, { three: 1 }, { four: 3, five: { six: 1 } }] } }, + )).toBe(true); }); it('observeChanges test filter', () => { - const data = [ - { - previous: { one: { two: 2, three: 3 } }, - current: { one: { two: 2, three: 4 } }, - filter: { one: ['two'] }, - }, - { - previous: { one: { two: 2, three: 3 } }, - current: { one: { two: 1, three: 3 } }, - filter: { one: ['two'] }, - }, - ]; - - data.forEach((item) => { - expect(reducerUtils.observeChanges( - item.previous, item.current, item.filter, - )).toMatchSnapshot(); - }); + expect(utils.observeChanges( + { one: { two: 2, three: 3 } }, + { one: { two: 2, three: 4 } }, + { one: ['two'] }, + )).toBe(false); + + expect(utils.observeChanges( + { one: { two: 2, three: 3 } }, + { one: { two: 1, three: 3 } }, + { one: ['two'] }, + )).toBe(true); }); }); diff --git a/src/utils/__tests__/cryptoUriParser.js b/src/utils/__tests__/cryptoUriParser.js new file mode 100644 index 00000000..4ba4aa80 --- /dev/null +++ b/src/utils/__tests__/cryptoUriParser.js @@ -0,0 +1,11 @@ +import * as utils from '../cryptoUriParser'; + +describe('crypto uri parser', () => { + it('parseUri', () => { + expect(utils.parseUri('http://www.trezor.io')).toEqual({ address: '//www.trezor.io' }); // TODO: Error in function + expect(utils.parseUri('www.trezor.io')).toEqual({ address: 'www.trezor.io' }); + expect(utils.parseUri('www.trezor.io/TT')).toEqual({ address: 'www.trezor.io/TT' }); + expect(utils.parseUri('www.trezor.io/TT?param1=aha')).toEqual({ address: 'www.trezor.io/TT', param1: 'aha' }); + expect(utils.parseUri('www.trezor.io/TT?param1=aha¶m2=hah')).toEqual({ address: 'www.trezor.io/TT', param1: 'aha', param2: 'hah' }); + }); +}); diff --git a/src/utils/__tests__/cryptoUriParser.test.js b/src/utils/__tests__/cryptoUriParser.test.js new file mode 100644 index 00000000..4ba4aa80 --- /dev/null +++ b/src/utils/__tests__/cryptoUriParser.test.js @@ -0,0 +1,11 @@ +import * as utils from '../cryptoUriParser'; + +describe('crypto uri parser', () => { + it('parseUri', () => { + expect(utils.parseUri('http://www.trezor.io')).toEqual({ address: '//www.trezor.io' }); // TODO: Error in function + expect(utils.parseUri('www.trezor.io')).toEqual({ address: 'www.trezor.io' }); + expect(utils.parseUri('www.trezor.io/TT')).toEqual({ address: 'www.trezor.io/TT' }); + expect(utils.parseUri('www.trezor.io/TT?param1=aha')).toEqual({ address: 'www.trezor.io/TT', param1: 'aha' }); + expect(utils.parseUri('www.trezor.io/TT?param1=aha¶m2=hah')).toEqual({ address: 'www.trezor.io/TT', param1: 'aha', param2: 'hah' }); + }); +}); diff --git a/src/utils/__tests__/device.test.js b/src/utils/__tests__/device.test.js index 52b1aec2..be96bea5 100644 --- a/src/utils/__tests__/device.test.js +++ b/src/utils/__tests__/device.test.js @@ -1,4 +1,5 @@ import * as utils from 'utils/device'; +import colors from 'config/colors'; describe('device utils', () => { it('get status', () => { @@ -64,14 +65,14 @@ describe('device utils', () => { }); it('get status color', () => { - expect(utils.getStatusColor(0)).toBe('#494949'); - expect(utils.getStatusColor(null)).toBe('#494949'); - expect(utils.getStatusColor('sdsdsdsd')).toBe('#494949'); - expect(utils.getStatusColor('used-in-other-window')).toBe('#EB8A00'); - expect(utils.getStatusColor('connected')).toBe('#01B757'); - expect(utils.getStatusColor('unacquired')).toBe('#EB8A00'); - expect(utils.getStatusColor('disconnected')).toBe('#ED1212'); - expect(utils.getStatusColor('unavailable')).toBe('#ED1212'); + expect(utils.getStatusColor(0)).toBe(colors.TEXT_PRIMARY); + expect(utils.getStatusColor(null)).toBe(colors.TEXT_PRIMARY); + expect(utils.getStatusColor('sdsdsdsd')).toBe(colors.TEXT_PRIMARY); + expect(utils.getStatusColor('used-in-other-window')).toBe(colors.WARNING_PRIMARY); + expect(utils.getStatusColor('connected')).toBe(colors.GREEN_PRIMARY); + expect(utils.getStatusColor('unacquired')).toBe(colors.WARNING_PRIMARY); + expect(utils.getStatusColor('disconnected')).toBe(colors.ERROR_PRIMARY); + expect(utils.getStatusColor('unavailable')).toBe(colors.ERROR_PRIMARY); }); it('get status name', () => { diff --git a/src/utils/__tests__/ethUtils.test.js b/src/utils/__tests__/ethUtils.test.js index e3fb8c1b..efa8871d 100644 --- a/src/utils/__tests__/ethUtils.test.js +++ b/src/utils/__tests__/ethUtils.test.js @@ -20,6 +20,7 @@ describe('eth utils', () => { }); it('padLeftEven', () => { + // TODO: add more tests expect(utils.padLeftEven('2540be3ff')).toBe('02540be3ff'); }); @@ -39,6 +40,14 @@ describe('eth utils', () => { }); it('calculate gas price', () => { + // TODO: add more tests expect(utils.calcGasPrice(new BigNumber(9898998989), 9)).toBe('89090990901'); }); + + it('validate address', () => { + // TODO: add more tests + expect(utils.validateAddress('')).toBe('Address is not set'); + expect(utils.validateAddress('aaa')).toBe('Address is not valid'); + expect(utils.validateAddress('BB9bc244D798123fDe783fCc1C72d3Bb8C189413')).toBe('Address is not valid'); + }); }); diff --git a/src/utils/__tests__/formatUtils.test.js b/src/utils/__tests__/formatUtils.test.js index f83776e4..4d57d0d1 100644 --- a/src/utils/__tests__/formatUtils.test.js +++ b/src/utils/__tests__/formatUtils.test.js @@ -1,41 +1,24 @@ import * as utils from '../formatUtils'; describe('format utils', () => { - // TODO: check this weird function - it('formatAmount', () => { - expect(utils.formatAmount(0, { isBitcoin: false, shortcut: 'mbtc' }, 'mbtc')).toBe('0 mbtc'); - expect(utils.formatAmount(1000000, { isBitcoin: true }, 'mbtc')).toBe('10 mBTC'); - expect(utils.formatAmount(0.5, { isBitcoin: true }, 'mbtc')).toBe('0.000005 mBTC'); - expect(utils.formatAmount(1, { isBitcoin: false, shortcut: 'eth' }, null)).toBe('1e-8 eth'); - expect(utils.formatAmount(99999, { isBitcoin: false, shortcut: 'tau' }, null)).toBe('0.00099999 tau'); + it('to decimal amount', () => { + expect(utils.toDecimalAmount(0, 1)).toBe('0'); + expect(utils.toDecimalAmount(1, 1)).toBe('0.1'); + expect(utils.toDecimalAmount(1000, 1)).toBe('100'); + expect(utils.toDecimalAmount(1000, 2)).toBe('10'); + expect(utils.toDecimalAmount(1000, 3)).toBe('1'); + expect(utils.toDecimalAmount('1', 'a')).toBe('NaN'); + expect(utils.toDecimalAmount('a', 'a')).toBe('0'); + expect(utils.toDecimalAmount('a', '1')).toBe('0'); }); - it('format time', () => { - expect(utils.formatTime(0)).toBe('No time estimate'); - expect(utils.formatTime(1)).toBe('1 minutes'); // TODO: should be minute - expect(utils.formatTime(2)).toBe('2 minutes'); - expect(utils.formatTime(45)).toBe('45 minutes'); - expect(utils.formatTime(100)).toBe('1 hour 40 minutes'); - expect(utils.formatTime(999)).toBe('16 hours 39 minutes'); - }); - - it('btckb2satoshib', () => { - expect(utils.btckb2satoshib(0)).toBe(0); - expect(utils.btckb2satoshib(1)).toBe(100000); - expect(utils.btckb2satoshib(2)).toBe(200000); - expect(utils.btckb2satoshib(100)).toBe(10000000); - expect(utils.btckb2satoshib(999)).toBe(99900000); - }); - - it('string to hex', () => { - expect(utils.stringToHex('test')).toBe('0074006500730074'); - expect(utils.stringToHex('0001')).toBe('0030003000300031'); - expect(utils.stringToHex('test99999')).toBe('007400650073007400390039003900390039'); - }); - - it('hex to string', () => { - expect(utils.hexToString('0074006500730074')).toBe('test'); - expect(utils.hexToString('0030003000300031')).toBe('0001'); - expect(utils.hexToString('007400650073007400390039003900390039')).toBe('test99999'); + it('from decimal amount', () => { + expect(utils.fromDecimalAmount(0, 1)).toBe('0'); + expect(utils.fromDecimalAmount(10, 1)).toBe('100'); + expect(utils.fromDecimalAmount(10, 2)).toBe('1000'); + expect(utils.fromDecimalAmount(10, 3)).toBe('10000'); + expect(utils.fromDecimalAmount('1', 'a')).toBe('NaN'); + expect(utils.fromDecimalAmount('a', 'a')).toBe('0'); + expect(utils.fromDecimalAmount('a', '1')).toBe('0'); }); }); diff --git a/src/utils/__tests__/notification.test.js b/src/utils/__tests__/notification.test.js index f145bee3..d2a076a1 100644 --- a/src/utils/__tests__/notification.test.js +++ b/src/utils/__tests__/notification.test.js @@ -1,12 +1,30 @@ import * as utils from 'utils/notification'; +import colors from 'config/colors'; +import icons from 'config/icons'; describe('notification utils', () => { - it('get colors from status', () => { - expect(utils.getPrimaryColor('info')).toBe('#1E7FF0'); - expect(utils.getPrimaryColor('warning')).toBe('#EB8A00'); - expect(utils.getPrimaryColor('error')).toBe('#ED1212'); - expect(utils.getPrimaryColor('success')).toBe('#01B757'); + it('get primary color from status', () => { + expect(utils.getPrimaryColor('info')).toBe(colors.INFO_PRIMARY); + expect(utils.getPrimaryColor('warning')).toBe(colors.WARNING_PRIMARY); + expect(utils.getPrimaryColor('error')).toBe(colors.ERROR_PRIMARY); + expect(utils.getPrimaryColor('success')).toBe(colors.SUCCESS_PRIMARY); expect(utils.getPrimaryColor('kdsjflds')).toBe(null); expect(utils.getPrimaryColor('')).toBe(null); }); + + it('get secondary color from status', () => { + expect(utils.getSecondaryColor('info')).toBe(colors.INFO_SECONDARY); + expect(utils.getSecondaryColor('warning')).toBe(colors.WARNING_SECONDARY); + expect(utils.getSecondaryColor('error')).toBe(colors.ERROR_SECONDARY); + expect(utils.getSecondaryColor('success')).toBe(colors.SUCCESS_SECONDARY); + expect(utils.getSecondaryColor('kdsjflds')).toBe(null); + expect(utils.getSecondaryColor('')).toBe(null); + }); + + it('get icon according to status', () => { + expect(utils.getIcon('info')).toEqual(icons.INFO); + expect(utils.getIcon('error')).toEqual(icons.ERROR); + expect(utils.getIcon('success')).toEqual(icons.SUCCESS); + expect(utils.getIcon('warning')).toEqual(icons.WARNING); + }); }); diff --git a/src/utils/cryptoUriParser.js b/src/utils/cryptoUriParser.js index defb6a4f..0e0a77fd 100644 --- a/src/utils/cryptoUriParser.js +++ b/src/utils/cryptoUriParser.js @@ -10,7 +10,7 @@ export type parsedURI = { }; // Parse a string read from a bitcoin QR code into an object -export const parseUri = (uri: string): ?parsedURI => { +const parseUri = (uri: string): ?parsedURI => { const str = stripPrefix(uri); const query: Array = str.split('?'); const values: Object = (query.length > 1) ? parseQuery(query[1]) : {}; @@ -37,3 +37,7 @@ const parseQuery = (str: string): {} => str.split('&') } return vals; }, {}); + +export { + parseUri, +}; \ No newline at end of file diff --git a/src/utils/formatUtils.js b/src/utils/formatUtils.js index 37231d7c..4cea3e00 100644 --- a/src/utils/formatUtils.js +++ b/src/utils/formatUtils.js @@ -2,58 +2,6 @@ import BigNumber from 'bignumber.js'; -const currencyUnitsConstant: string = 'mbtc2'; - -export const formatAmount = (n: number, coinInfo: any, currencyUnits: string = currencyUnitsConstant): string => { - const amount = (n / 1e8); - if (coinInfo.isBitcoin && currencyUnits === 'mbtc' && amount <= 0.1 && n !== 0) { - const s = (n / 1e5).toString(); - return `${s} mBTC`; - } - const s = amount.toString(); - return `${s} ${coinInfo.shortcut}`; -}; - -export const formatTime = (n: number): string => { - const hours = Math.floor(n / 60); - const minutes = n % 60; - - if (!n) return 'No time estimate'; - let res = ''; - if (hours !== 0) { - res += `${hours} hour`; - if (hours > 1) { - res += 's'; - } - res += ' '; - } - if (minutes !== 0) { - res += `${minutes} minutes`; - } - return res; -}; - -export const btckb2satoshib = (n: number): number => Math.round(n * 1e5); - -export const stringToHex = (str: string): string => { - let result: string = ''; - let hex: string; - for (let i = 0; i < str.length; i++) { - hex = str.charCodeAt(i).toString(16); - result += (`000${hex}`).slice(-4); - } - return result; -}; - -export const hexToString = (hex: string): string => { - let str = ''; - for (let i = 0; i < hex.length; i += 2) { - const v = parseInt(hex.substr(i, 2), 16); - if (v) str += String.fromCharCode(v); - } - return str; -}; - export const toDecimalAmount = (amount: string | number, decimals: number): string => { try { const bAmount = new BigNumber(amount);