From 2610868589a9de0a9067047e69dcd252b2b5f985 Mon Sep 17 00:00:00 2001 From: fragosti Date: Tue, 16 Oct 2018 16:13:23 -0700 Subject: Add tests for format and use toFixed instead of round for usd --- packages/instant/package.json | 1 + packages/instant/src/util/format.ts | 2 +- packages/instant/test/util/format.test.ts | 97 +++++++++++++++++++++++++++++++ yarn.lock | 4 ++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 packages/instant/test/util/format.test.ts diff --git a/packages/instant/package.json b/packages/instant/package.json index befedfc17..b991d0e05 100644 --- a/packages/instant/package.json +++ b/packages/instant/package.json @@ -63,6 +63,7 @@ "@0xproject/tslint-config": "^1.0.8", "@types/enzyme": "^3.1.14", "@types/enzyme-adapter-react-16": "^1.0.3", + "@types/jest": "^23.3.5", "@types/lodash": "^4.14.116", "@types/node": "*", "@types/react": "^16.4.16", diff --git a/packages/instant/src/util/format.ts b/packages/instant/src/util/format.ts index de7eb62e6..b62c968fb 100644 --- a/packages/instant/src/util/format.ts +++ b/packages/instant/src/util/format.ts @@ -40,6 +40,6 @@ export const format = { if (_.isUndefined(ethUnitAmount) || _.isUndefined(ethUsdPrice)) { return defaultText; } - return `$${ethUnitAmount.mul(ethUsdPrice).round(decimalPlaces)}`; + return `$${ethUnitAmount.mul(ethUsdPrice).toFixed(decimalPlaces)}`; }, }; diff --git a/packages/instant/test/util/format.test.ts b/packages/instant/test/util/format.test.ts new file mode 100644 index 000000000..073b86b19 --- /dev/null +++ b/packages/instant/test/util/format.test.ts @@ -0,0 +1,97 @@ +import { BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; + +import { ethDecimals } from '../../src/constants'; +import { format } from '../../src/util/format'; + +const BIG_NUMBER_ONE = new BigNumber(1); +const BIG_NUMBER_DECIMAL = new BigNumber(0.432414); +const BIG_NUMBER_IRRATIONAL = new BigNumber(5.3014059295032); +const ONE_ETH_IN_BASE_UNITS = Web3Wrapper.toBaseUnitAmount(BIG_NUMBER_ONE, ethDecimals); +const DECIMAL_ETH_IN_BASE_UNITS = Web3Wrapper.toBaseUnitAmount(BIG_NUMBER_DECIMAL, ethDecimals); +const IRRATIONAL_ETH_IN_BASE_UNITS = Web3Wrapper.toBaseUnitAmount(BIG_NUMBER_IRRATIONAL, ethDecimals); +const BIG_NUMBER_FAKE_ETH_USD_PRICE = new BigNumber(2.534); + +describe('format', () => { + describe('ethBaseAmount', () => { + it('converts 1 ETH in base units to the string `1 ETH`', () => { + expect(format.ethBaseAmount(ONE_ETH_IN_BASE_UNITS)).toBe('1 ETH'); + }); + it('converts .432414 ETH in base units to the string `.4324 ETH`', () => { + expect(format.ethBaseAmount(DECIMAL_ETH_IN_BASE_UNITS)).toBe('0.4324 ETH'); + }); + it('converts 5.3014059295032 ETH in base units to the string `5.3014 ETH`', () => { + expect(format.ethBaseAmount(IRRATIONAL_ETH_IN_BASE_UNITS)).toBe('5.3014 ETH'); + }); + it('returns defaultText param when ethBaseAmount is not defined', () => { + const defaultText = 'defaultText'; + expect(format.ethBaseAmount(undefined, 4, defaultText)).toBe(defaultText); + }); + it('it allows for configurable decimal places', () => { + expect(format.ethBaseAmount(DECIMAL_ETH_IN_BASE_UNITS, 2)).toBe('0.43 ETH'); + }); + }); + describe('ethUnitAmount', () => { + it('converts BigNumber(1) to the string `1 ETH`', () => { + expect(format.ethUnitAmount(BIG_NUMBER_ONE)).toBe('1 ETH'); + }); + it('converts BigNumer(.432414) to the string `.4324 ETH`', () => { + expect(format.ethUnitAmount(BIG_NUMBER_DECIMAL)).toBe('0.4324 ETH'); + }); + it('converts BigNumber(5.3014059295032) to the string `5.3014 ETH`', () => { + expect(format.ethUnitAmount(BIG_NUMBER_IRRATIONAL)).toBe('5.3014 ETH'); + }); + it('returns defaultText param when ethUnitAmount is not defined', () => { + const defaultText = 'defaultText'; + expect(format.ethUnitAmount(undefined, 4, defaultText)).toBe(defaultText); + expect(format.ethUnitAmount(BIG_NUMBER_ONE, 4, defaultText)).toBe('1 ETH'); + }); + it('it allows for configurable decimal places', () => { + expect(format.ethUnitAmount(BIG_NUMBER_DECIMAL, 2)).toBe('0.43 ETH'); + }); + }); + describe('ethBaseAmountInUsd', () => { + it('correctly formats 1 ETH to usd according to some price', () => { + expect(format.ethBaseAmountInUsd(ONE_ETH_IN_BASE_UNITS, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$2.53'); + }); + it('correctly formats .432414 ETH to usd according to some price', () => { + expect(format.ethBaseAmountInUsd(DECIMAL_ETH_IN_BASE_UNITS, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$1.10'); + }); + it('correctly formats 5.3014059295032 ETH to usd according to some price', () => { + expect(format.ethBaseAmountInUsd(IRRATIONAL_ETH_IN_BASE_UNITS, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe( + '$13.43', + ); + }); + it('returns defaultText param when ethBaseAmountInUsd or ethUsdPrice is not defined', () => { + const defaultText = 'defaultText'; + expect(format.ethBaseAmountInUsd(undefined, undefined, 2, defaultText)).toBe(defaultText); + expect(format.ethBaseAmountInUsd(BIG_NUMBER_ONE, undefined, 2, defaultText)).toBe(defaultText); + expect(format.ethBaseAmountInUsd(undefined, BIG_NUMBER_ONE, 2, defaultText)).toBe(defaultText); + }); + it('it allows for configurable decimal places', () => { + expect(format.ethBaseAmountInUsd(DECIMAL_ETH_IN_BASE_UNITS, BIG_NUMBER_FAKE_ETH_USD_PRICE, 4)).toBe( + '$1.0957', + ); + }); + }); + describe('ethUnitAmountInUsd', () => { + it('correctly formats 1 ETH to usd according to some price', () => { + expect(format.ethUnitAmountInUsd(BIG_NUMBER_ONE, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$2.53'); + }); + it('correctly formats .432414 ETH to usd according to some price', () => { + expect(format.ethUnitAmountInUsd(BIG_NUMBER_DECIMAL, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$1.10'); + }); + it('correctly formats 5.3014059295032 ETH to usd according to some price', () => { + expect(format.ethUnitAmountInUsd(BIG_NUMBER_IRRATIONAL, BIG_NUMBER_FAKE_ETH_USD_PRICE)).toBe('$13.43'); + }); + it('returns defaultText param when ethUnitAmountInUsd or ethUsdPrice is not defined', () => { + const defaultText = 'defaultText'; + expect(format.ethUnitAmountInUsd(undefined, undefined, 2, defaultText)).toBe(defaultText); + expect(format.ethUnitAmountInUsd(BIG_NUMBER_ONE, undefined, 2, defaultText)).toBe(defaultText); + expect(format.ethUnitAmountInUsd(undefined, BIG_NUMBER_ONE, 2, defaultText)).toBe(defaultText); + }); + it('it allows for configurable decimal places', () => { + expect(format.ethUnitAmountInUsd(BIG_NUMBER_DECIMAL, BIG_NUMBER_FAKE_ETH_USD_PRICE, 4)).toBe('$1.0957'); + }); + }); +}); diff --git a/yarn.lock b/yarn.lock index af3f9938b..82b5743b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1022,6 +1022,10 @@ version "0.4.30" resolved "https://registry.yarnpkg.com/@types/istanbul/-/istanbul-0.4.30.tgz#073159320ab3296b2cfeb481f756a1f8f4c9c8e4" +"@types/jest@^23.3.5": + version "23.3.5" + resolved "https://registry.npmjs.org/@types/jest/-/jest-23.3.5.tgz#870a1434208b60603745bfd214fc3fc675142364" + "@types/js-combinatorics@^0.5.29": version "0.5.29" resolved "https://registry.yarnpkg.com/@types/js-combinatorics/-/js-combinatorics-0.5.29.tgz#47a7819a0b6925b6dc4bd2c2278a7e6329b29387" -- cgit