aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/test
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-10-18 18:36:41 +0800
committerFabio Berger <me@fabioberger.com>2018-10-18 18:36:41 +0800
commit28863f9a6f8a28ddbfe588b9e4d03b1e31da6961 (patch)
treedb9bd874838a30ba82eacab0d4255fe3067eff94 /packages/instant/test
parent6ab6a9aa2b3511df60c078398168a84faf3e2385 (diff)
parentcdd650d0eb83153a992922c6ffff35b494f299d3 (diff)
downloaddexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar
dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.gz
dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.bz2
dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.lz
dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.xz
dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.tar.zst
dexon-sol-tools-28863f9a6f8a28ddbfe588b9e4d03b1e31da6961.zip
merge dev-section-redesign
Diffstat (limited to 'packages/instant/test')
-rw-r--r--packages/instant/test/components/zero_ex_instant.test.tsx12
-rw-r--r--packages/instant/test/util/asset_data.test.ts17
-rw-r--r--packages/instant/test/util/error.test.ts48
-rw-r--r--packages/instant/test/util/format.test.ts97
4 files changed, 169 insertions, 5 deletions
diff --git a/packages/instant/test/components/zero_ex_instant.test.tsx b/packages/instant/test/components/zero_ex_instant.test.tsx
index 5858732cf..e373bb002 100644
--- a/packages/instant/test/components/zero_ex_instant.test.tsx
+++ b/packages/instant/test/components/zero_ex_instant.test.tsx
@@ -4,10 +4,12 @@ import * as React from 'react';
configure({ adapter: new Adapter() });
-import { ZeroExInstant } from '../../src';
-
-describe('<ZeroExInstant />', () => {
- it('shallow renders without crashing', () => {
- shallow(<ZeroExInstant />);
+// TODO: Write non-trivial tests.
+// At time of writing we cannot render ZeroExInstant
+// because we are looking for a provider on window.
+// But in the future it will be dependency injected.
+describe('<Test />', () => {
+ it('runs a test', () => {
+ shallow(<div />);
});
});
diff --git a/packages/instant/test/util/asset_data.test.ts b/packages/instant/test/util/asset_data.test.ts
new file mode 100644
index 000000000..cf247142a
--- /dev/null
+++ b/packages/instant/test/util/asset_data.test.ts
@@ -0,0 +1,17 @@
+import { assetDataUtil } from '../../src/util/asset_data';
+
+const ZRX_ASSET_DATA = '0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498';
+
+describe('assetDataUtil', () => {
+ describe('bestNameForAsset', () => {
+ it('should return default string if assetData is undefined', () => {
+ expect(assetDataUtil.bestNameForAsset(undefined, 'xyz')).toEqual('xyz');
+ });
+ it('should return default string if assetData isnt found', () => {
+ expect(assetDataUtil.bestNameForAsset('fake', 'mah default')).toEqual('mah default');
+ });
+ it('should return ZRX for ZRX assetData', () => {
+ expect(assetDataUtil.bestNameForAsset(ZRX_ASSET_DATA, 'mah default')).toEqual('ZRX');
+ });
+ });
+});
diff --git a/packages/instant/test/util/error.test.ts b/packages/instant/test/util/error.test.ts
new file mode 100644
index 000000000..56f3a1e86
--- /dev/null
+++ b/packages/instant/test/util/error.test.ts
@@ -0,0 +1,48 @@
+import { AssetBuyerError } from '@0xproject/asset-buyer';
+
+import { errorUtil } from '../../src/util/error';
+
+const ZRX_ASSET_DATA = '0xf47261b0000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498';
+
+describe('errorUtil', () => {
+ describe('errorFlasher', () => {
+ it('should return error and asset name for InsufficientAssetLiquidity', () => {
+ const insufficientAssetError = new Error(AssetBuyerError.InsufficientAssetLiquidity);
+ expect(errorUtil.errorDescription(insufficientAssetError, ZRX_ASSET_DATA).message).toEqual(
+ 'Not enough ZRX available',
+ );
+ });
+ it('should return error default name for InsufficientAssetLiquidity', () => {
+ const insufficientZrxError = new Error(AssetBuyerError.InsufficientZrxLiquidity);
+ expect(errorUtil.errorDescription(insufficientZrxError).message).toEqual(
+ 'Not enough of this asset available',
+ );
+ });
+ it('should return asset name for InsufficientAssetLiquidity', () => {
+ const insufficientZrxError = new Error(AssetBuyerError.InsufficientZrxLiquidity);
+ expect(errorUtil.errorDescription(insufficientZrxError, ZRX_ASSET_DATA).message).toEqual(
+ 'Not enough ZRX available',
+ );
+ });
+ it('should return unavailable error and asset name for StandardRelayerApiError', () => {
+ const standardRelayerError = new Error(AssetBuyerError.StandardRelayerApiError);
+ expect(errorUtil.errorDescription(standardRelayerError, ZRX_ASSET_DATA).message).toEqual(
+ 'ZRX is currently unavailable',
+ );
+ });
+ it('should return error for AssetUnavailable error', () => {
+ const assetUnavailableError = new Error(
+ `${AssetBuyerError.AssetUnavailable}: For assetData ${ZRX_ASSET_DATA}`,
+ );
+ expect(errorUtil.errorDescription(assetUnavailableError, ZRX_ASSET_DATA).message).toEqual(
+ 'ZRX is currently unavailable',
+ );
+ });
+ it('should return default for AssetUnavailable error', () => {
+ const assetUnavailableError = new Error(`${AssetBuyerError.AssetUnavailable}: For assetData xyz`);
+ expect(errorUtil.errorDescription(assetUnavailableError, 'xyz').message).toEqual(
+ 'This asset is currently unavailable',
+ );
+ });
+ });
+});
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');
+ });
+ });
+});