mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-10 11:05:47 +00:00
e761b9d1fb
* Add a new route for AddressBook * Further templating of the AddressBook view * Add initial functionality to handle a table of existing address labels * Make the linter happy * Adjust paths * Factor out TableRow and add common functionality * Add initial Redux boilerplate for addressBook | fix minor linting issues * Swap out terminology and types * Connect up to Redux * Connect data for AddressBookTable to Redux * Use temporary fields for addition * Remove alignment and index column * Stopping point * Adjust the sizing of rows to be consistent * Initial implementation of a dropdown for the address field * Minor styling to dropdown * Stopping point * Apply a focus concept onto the factory * Add keyboard controls for the address field dropdown * Adjust label of address field when it matches an addressBook entry * Properly handle attempting to blur a non-existent component * Minor styling changes on dropdown box * Standardize address casing, add accessibility to dropdown * Create an addressLabel component * Pass refs correctly and fix some typings * Exact version * Add module name mapping for shared/keycodes * addressBook reducer tests * Add functionality to DeterministicModal * Minor changes / Add test for addressBook selectors * Move out AddressBookTable to a component * Typing, translation and restructuring * More typing and translation fixes * More linting fixes * More type changes * Variable name for dropdown background * Fix TS type errors, lint errors, remove unused props * Used a different selector and removed method: AddressBookTable * Linter was mad * Linter mad again :( * Add a translation and adjust styling of AddressBookTable * Move the onBlur to a class method * Prevent the default behavior of up/down/enter for dropdown * Let's do it this way instead * Adjust the styling on DeterministicWalletModal labels * Change `AddressBookTable` into a pseudo-table using section and div * Use readable keys vs. keycodes * Put the dropdown in InputFactory and position it correctly * Sanitation of label adding and changing * Prevent duplicate labels in AddressBook and Row * Add a box shadow and use `invalid` class insted of custom * Use emphasis vs strong for address in dropdown * Display the label undernearth the input vs. changing it * Isolate AccountAddress into its own component * Introduce interactivity to AccountAddress * Fully incorporate with Redux * Validation for AccountAddress * Add validation notifications for address field on AddressBookTable * Minor formatting * Adjust wrappage for optimal flexboxxing * Make AddressBookTable responsive * Show an invalid input in "real time" instead of only on submit * Real time input validation for AddressBookTableRow * Responsive-ize the To address dropdown * Hide identicons instead at small enough screen sizes * Fix repsonsiveness of dropdown further * Fix responsiveness of table rows and inputs * Truncate account info and switch identicons to the right for consistency * Use classnames instead of targetting element directly for DWM * Display a notice if the entered query doesnt match a label and isnt an addr * Don't show an error on the To address if its a label entry * Display an error under AddressBookTableRow in real time * Display errors in real time for AddressBookTable temp inputs * Add realtime validation to AccountAddress * Ensure toChecksumAddress is used when entering labels to address manager * Show errors even after blurring. * Create a ducks/ implementation for addressBook * Duck-ize notifications * Duck-ize customTokens * Duck-ize deterministicWallets * Only show errors on address/label entry if they have been blurred * On certain inputs, show an invalid input immediately * spec files in same directory * Rename top-level redux directory * Duck-ize gas * Add displayed errors for labels with 0x and labels containing ens * Move ENS checking validation out * Add a saga for addLabelForAddress * Completely revamp the redux side of Address Manager and test it all * Adjust components to use new redux addressBook * Incorporate new redux into AddressBookTableRow and clean up for linter * Make linter and tests happy * Another reduxy overhaul * Still fixing it * More redux updates * Finalize redux stuff. * Incorporate new reduxy way into AddressBookTable & Row * Incorporate redux changes into Account Address * Small tests fix * Add and fix some selector tests * Addressing Will's comments * Shortened visibility class for line length reasons. * Incorporate ducks pattern on updates addressBook * Fix typeerror * Migrate messages to ducks * For Henry * Duckify onboardStatus * Duckify paritySigner * Duckify rates * Duckify transactions * Duckerize wallet * Reduckerate config * Adjust exports and tests of every duck so far * Duckify ENS * Duckerificate schedule * Duckificate swap * Actually use the new sagas; fix a circular dependency problem. * Duckify transaction (phew) * Add basics to redux/ directory * Remove non-ducked redux stuff * First sweep of redux/ directory * Combine redundant imports * Fix more linting stuff. * A few more type fixes * Welp... now I know not to use index. * Sweep components/ * Sweep through containers/ * Im really starting to get frustrated * The dawn of a new age. * Linter fixes. * De-flatten config/ reducers * Do my thang on config selectors * Adjust all references to config * Split up ens reducers * Wrap up de-nesting ENS * Big boy refactor * Split transaction into its reducers * Fix reducers in transaction/ * Stopping point * Adjust references to transaction from components * Fix references to selectors * Nest broadcast actions * Nest field actions * Nest meta actions * Nest network actions * Nest sign actions * Nest broadcast types * Nested fields types * Nest meta types * Nested network types * Nested sign types * Implement transaction saga changes * Huh? No prepush problems? * Update snappshot * Reintroduce deleted tests * A few missing tests found * Found three missing transaction tests * Found more tests * Found the rest of the tests, woohoo. * Renamed TypeKeys in TRANSACTION * Specify TRANSACTION_BROADCAST * Pretty up these imports * Specify TRANSACTION_FIELDS * Specify TRANSACTION_META * Specify TRANSACTION_NETWORK * Specify TRANSACTION_SIGN * Adjust imports and add translations * Update config snapshot * Post-merge * Temporary fix for DW/Config sagas so Daniel can continue smoke testing * Remove first circulat dependency * Fix more circular dependencies * Properly structure config indices * Further restructure config * Prepare for idea * Target directly from within features/ * Remove that circular dependency -- woohoo * Remove the circular dependency from Web3Wallet, temporarily comment some tests pending assistance * Un-comment the component-in-redux phenomenon * Move onLoad to the store file * Adjust addressBook imports/exports * Adjusted imports/exports for customTokens * Adjust imports/exports of deterministicWallets * Adjust imports/exports of ens * Restructure imports/exports of gas * Restructure imports/exports for message * Adjust imports/exports of notifications * Restructure onboardStatus imports/exports * Restructure paritySigner imports/exports * Restructure rates imports/exports * Restructure schedule imports/exports * Fix broadcastweb3handler test * Restructure swap imports/exports/ * Restructure transactionS imports/exports * Restructured wallet imports and exports * Hoist all necessary selectors aside from config/**/* and transaction/**/* * Hoist all top-level selectors from transaction * [Fix] Estimate Gas on Value Field Change (#1942) @ skubakdj * Implement right-click context menu (#1780) @ bryanwb * No Private Keys Online (#1466) @ wbobeirne * Fix Stuck Node on Metamask Logout (#1951) @ wbobeirne * [Fix] Make ENS Value Consistent (#1956) @ skubakdj * Auto token add (#1808) @ HenryNguyen5 * Electron Ledger + Trezor Support (#1836) @ wbobeirne * Fix Context Menu Popup Parameters (#1957) * Add RSK network w/ network agnostic refactors (#1939) @ wbobeirne * Change displayed notification back in helpers.tsx * Remove newline on shell files * Re-add newlines * Remove newling on .travis.yml * Prettier two files * Re-add index.scss import in OnboardModal * Restructure transaction subdirectories * Everything in transaction/ except for sagas * Restructure transaction imports/exports * Nest broadcast sagas * Nest fields * Nest meta * Nest network * Nest sign * Use generic names for reduxy stuff in the same directory to save space * Do everything every in the whole wide world
679 lines
23 KiB
TypeScript
679 lines
23 KiB
TypeScript
import BN from 'bn.js';
|
|
import { SagaIterator } from 'redux-saga';
|
|
import { call, select, put, take, apply } from 'redux-saga/effects';
|
|
import { cloneableGenerator, SagaIteratorClone } from 'redux-saga/utils';
|
|
|
|
import { Address, toTokenBase, Wei, fromTokenBase, fromWei } from 'libs/units';
|
|
import { isValidENSAddress, getIsValidAddressFunction } from 'libs/validators';
|
|
import configuredStore from 'features/store';
|
|
import { ICurrentValue } from 'features/types';
|
|
import * as derivedSelectors from 'features/selectors';
|
|
import * as configSelectors from 'features/config/selectors';
|
|
import { ensTypes, ensActions, ensSelectors } from 'features/ens';
|
|
import { walletSelectors } from 'features/wallet';
|
|
import { notificationsActions } from 'features/notifications';
|
|
import { transactionFieldsActions } from './fields';
|
|
import { transactionMetaActions, transactionMetaSelectors } from './meta';
|
|
import * as actions from './actions';
|
|
import * as sagas from './sagas';
|
|
import * as helpers from './helpers';
|
|
|
|
configuredStore.getState();
|
|
|
|
describe('transaction: Sagas', () => {
|
|
describe('Current', () => {
|
|
const itShouldBeDone = (gen: SagaIterator) => {
|
|
it('should be done', () => {
|
|
expect(gen.next().done).toEqual(true);
|
|
});
|
|
};
|
|
|
|
describe('setCurrentTo*', () => {
|
|
const isValidAddress = getIsValidAddressFunction(1);
|
|
const data = {} as any;
|
|
|
|
describe('with valid Ethereum address', () => {
|
|
const raw = '0xa';
|
|
const ethAddrPayload = {
|
|
raw,
|
|
value: Address(raw)
|
|
};
|
|
const ethAddrAction: any = {
|
|
payload: raw
|
|
};
|
|
|
|
data.validEthGen = sagas.setCurrentToSaga(ethAddrAction);
|
|
|
|
it('should select getIsValidAddressFn', () => {
|
|
expect(data.validEthGen.next().value).toEqual(
|
|
select(configSelectors.getIsValidAddressFn)
|
|
);
|
|
});
|
|
|
|
it('should call isValidAddress', () => {
|
|
expect(data.validEthGen.next(isValidAddress).value).toEqual(call(isValidAddress, raw));
|
|
});
|
|
|
|
it('should call isValidENSAddress', () => {
|
|
expect(data.validEthGen.next(raw).value).toEqual(call(isValidENSAddress, raw));
|
|
});
|
|
|
|
it('should call setField', () => {
|
|
expect(data.validEthGen.next(raw).value).toEqual(call(sagas.setField, ethAddrPayload));
|
|
});
|
|
});
|
|
|
|
describe('with invalid Ethereum address, valid ENS address', () => {
|
|
const raw = 'testing.eth';
|
|
const resolvedAddress = '0xa';
|
|
const [domain] = raw.split('.');
|
|
const ensAddrPayload = {
|
|
raw,
|
|
value: null
|
|
};
|
|
const ensAddrAction: any = {
|
|
payload: raw
|
|
};
|
|
data.validEnsGen = sagas.setCurrentToSaga(ensAddrAction);
|
|
|
|
it('should select getIsValidAddressFn', () => {
|
|
expect(data.validEnsGen.next().value).toEqual(
|
|
select(configSelectors.getIsValidAddressFn)
|
|
);
|
|
});
|
|
|
|
it('should call isValidAddress', () => {
|
|
expect(data.validEnsGen.next(isValidAddress).value).toEqual(call(isValidAddress, raw));
|
|
});
|
|
|
|
it('should call isValidENSAddress', () => {
|
|
expect(data.validEnsGen.next(false).value).toEqual(call(isValidENSAddress, raw));
|
|
});
|
|
|
|
it('should call setField', () => {
|
|
expect(data.validEnsGen.next(true).value).toEqual(call(sagas.setField, ensAddrPayload));
|
|
});
|
|
|
|
it('should put resolveDomainRequested', () => {
|
|
expect(data.validEnsGen.next().value).toEqual(
|
|
put(ensActions.resolveDomainRequested(domain))
|
|
);
|
|
});
|
|
|
|
it('should take ENS type keys', () => {
|
|
expect(data.validEnsGen.next().value).toEqual(
|
|
take([
|
|
ensTypes.ENSActions.RESOLVE_DOMAIN_FAILED,
|
|
ensTypes.ENSActions.RESOLVE_DOMAIN_SUCCEEDED,
|
|
ensTypes.ENSActions.RESOLVE_DOMAIN_CACHED
|
|
])
|
|
);
|
|
});
|
|
|
|
it('should select getResolvedAddress', () => {
|
|
expect(data.validEnsGen.next().value).toEqual(
|
|
select(ensSelectors.getResolvedAddress, true)
|
|
);
|
|
});
|
|
|
|
it('should call setField', () => {
|
|
expect(data.validEnsGen.next(resolvedAddress).value).toEqual(
|
|
call(sagas.setField, { raw, value: Address(resolvedAddress) })
|
|
);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('setField', () => {
|
|
const raw = '0xa';
|
|
const payload = {
|
|
raw,
|
|
value: Address(raw)
|
|
};
|
|
const etherTransaction = cloneableGenerator(sagas.setField)(payload);
|
|
it('should select etherTransaction', () => {
|
|
expect(etherTransaction.next().value).toEqual(select(derivedSelectors.isEtherTransaction));
|
|
});
|
|
|
|
it('should put setTokenTo field if its a token transaction ', () => {
|
|
const tokenTransaction = etherTransaction.clone();
|
|
|
|
expect(tokenTransaction.next(false).value).toEqual(
|
|
put(transactionMetaActions.setTokenTo(payload))
|
|
);
|
|
expect(tokenTransaction.next().done).toBe(true);
|
|
});
|
|
it('should put setToField if its an etherTransaction', () => {
|
|
expect(etherTransaction.next(true).value).toEqual(
|
|
put(transactionFieldsActions.setToField(payload))
|
|
);
|
|
expect(etherTransaction.next().done).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('valueHandler', () => {
|
|
const action: any = { payload: '5.1' };
|
|
const zeroAction: any = { payload: '0' };
|
|
const setter = transactionFieldsActions.setValueField;
|
|
const decimal = 1;
|
|
const gen: { [key: string]: SagaIteratorClone } = {};
|
|
|
|
const failCases = {
|
|
invalidDecimal: 0,
|
|
invalidNumber: {
|
|
decimal: 1,
|
|
action: { payload: 'x' }
|
|
},
|
|
invalidZeroToken: {
|
|
unit: 'GNT',
|
|
isEth: false,
|
|
setter: transactionMetaActions.setTokenValue
|
|
}
|
|
};
|
|
|
|
gen.pass = cloneableGenerator(sagas.valueHandler)(action, setter);
|
|
gen.zeroPass = cloneableGenerator(sagas.valueHandler)(zeroAction, setter);
|
|
gen.invalidNumber = cloneableGenerator(sagas.valueHandler)(
|
|
failCases.invalidNumber.action as any,
|
|
setter
|
|
);
|
|
gen.invalidZeroToken = cloneableGenerator(sagas.valueHandler)(
|
|
zeroAction,
|
|
transactionMetaActions.setTokenValue
|
|
);
|
|
const value = toTokenBase(action.payload, decimal);
|
|
const zeroValue = toTokenBase(zeroAction.payload, decimal);
|
|
const unit = 'eth';
|
|
const isEth = true;
|
|
|
|
it('should select getDecimal', () => {
|
|
expect(gen.pass.next().value).toEqual(select(transactionMetaSelectors.getDecimal));
|
|
expect(gen.zeroPass.next().value).toEqual(select(transactionMetaSelectors.getDecimal));
|
|
expect(gen.invalidNumber.next().value).toEqual(select(transactionMetaSelectors.getDecimal));
|
|
expect(gen.invalidZeroToken.next().value).toEqual(
|
|
select(transactionMetaSelectors.getDecimal)
|
|
);
|
|
});
|
|
|
|
it('should select getUnit', () => {
|
|
gen.invalidDecimal = gen.pass.clone();
|
|
expect(gen.pass.next(decimal).value).toEqual(select(derivedSelectors.getUnit));
|
|
expect(gen.zeroPass.next(decimal).value).toEqual(select(derivedSelectors.getUnit));
|
|
expect(gen.invalidNumber.next(decimal).value).toEqual(select(derivedSelectors.getUnit));
|
|
expect(gen.invalidDecimal.next(failCases.invalidDecimal).value).toEqual(
|
|
select(derivedSelectors.getUnit)
|
|
);
|
|
expect(gen.invalidZeroToken.next(decimal).value).toEqual(select(derivedSelectors.getUnit));
|
|
});
|
|
|
|
it('should select isEtherTransaction', () => {
|
|
expect(gen.pass.next(unit).value).toEqual(select(derivedSelectors.isEtherTransaction));
|
|
expect(gen.zeroPass.next(unit).value).toEqual(select(derivedSelectors.isEtherTransaction));
|
|
expect(gen.invalidNumber.next(unit).value).toEqual(
|
|
select(derivedSelectors.isEtherTransaction)
|
|
);
|
|
expect(gen.invalidDecimal.next(unit).value).toEqual(
|
|
select(derivedSelectors.isEtherTransaction)
|
|
);
|
|
expect(gen.invalidZeroToken.next(failCases.invalidZeroToken.unit).value).toEqual(
|
|
select(derivedSelectors.isEtherTransaction)
|
|
);
|
|
});
|
|
|
|
it('should fail on invalid number or decimal and put null as value', () => {
|
|
expect(gen.invalidNumber.next(isEth).value).toEqual(
|
|
put(setter({ raw: failCases.invalidNumber.action.payload, value: null }))
|
|
);
|
|
expect(gen.invalidDecimal.next(isEth).value).toEqual(
|
|
put(setter({ raw: action.payload, value: null }))
|
|
);
|
|
});
|
|
|
|
it('should fail if token is given zero value and put null as value', () => {
|
|
expect(gen.invalidZeroToken.next(failCases.invalidZeroToken.isEth).value).toEqual(
|
|
put(failCases.invalidZeroToken.setter({ raw: zeroAction.payload, value: null }))
|
|
);
|
|
});
|
|
|
|
it('should call isValid', () => {
|
|
expect(gen.pass.next(isEth).value).toEqual(call(helpers.validateInput, value, unit));
|
|
expect(gen.zeroPass.next(isEth).value).toEqual(
|
|
call(helpers.validateInput, zeroValue, unit)
|
|
);
|
|
});
|
|
|
|
it('should put setter', () => {
|
|
expect(gen.pass.next(true).value).toEqual(put(setter({ raw: action.payload, value })));
|
|
expect(gen.zeroPass.next(true).value).toEqual(
|
|
put(
|
|
setter({
|
|
raw: zeroAction.payload,
|
|
value: zeroValue
|
|
})
|
|
)
|
|
);
|
|
});
|
|
|
|
it('handles floats without lead zero', () => {
|
|
const leadZeroValue = {
|
|
decimal: 18,
|
|
action: {
|
|
payload: '.1'
|
|
}
|
|
};
|
|
const g = cloneableGenerator(sagas.valueHandler)(leadZeroValue.action as any, setter);
|
|
|
|
expect(g.next().value).toEqual(select(transactionMetaSelectors.getDecimal));
|
|
expect(g.next(leadZeroValue.decimal).value).toEqual(select(derivedSelectors.getUnit));
|
|
expect(g.next(unit).value).toEqual(select(derivedSelectors.isEtherTransaction));
|
|
expect(g.next(isEth).value).not.toEqual(
|
|
put(setter({ raw: leadZeroValue.action.payload, value: null }))
|
|
);
|
|
});
|
|
|
|
itShouldBeDone(gen.pass);
|
|
itShouldBeDone(gen.zeroPass);
|
|
});
|
|
|
|
describe('setCurrentValue*', () => {
|
|
const action: any = { payload: '5' };
|
|
const gen = sagas.setCurrentValueSaga(action);
|
|
it('should select isEtherTransaction', () => {
|
|
expect(gen.next().value).toEqual(select(derivedSelectors.isEtherTransaction));
|
|
});
|
|
it('should call valueHandler', () => {
|
|
expect(gen.next(derivedSelectors.isEtherTransaction).value).toEqual(
|
|
call(sagas.valueHandler, action, transactionFieldsActions.setValueField)
|
|
);
|
|
});
|
|
itShouldBeDone(gen);
|
|
});
|
|
|
|
describe('revalidateCurrentValue*', () => {
|
|
const sharedLogic = (
|
|
gen: SagaIterator,
|
|
etherTransaction: boolean,
|
|
currVal: any,
|
|
reparsedValue: boolean
|
|
) => {
|
|
it('should select isEtherTransaction', () => {
|
|
expect(gen.next().value).toEqual(select(derivedSelectors.isEtherTransaction));
|
|
});
|
|
|
|
it('should select getCurrentValue', () => {
|
|
expect(gen.next(etherTransaction).value).toEqual(
|
|
select(derivedSelectors.getCurrentValue)
|
|
);
|
|
});
|
|
|
|
it('should call reparseCurrentValue', () => {
|
|
expect(gen.next(currVal).value).toEqual(call(sagas.reparseCurrentValue, currVal));
|
|
});
|
|
|
|
it('should select getUnit', () => {
|
|
expect(gen.next(reparsedValue).value).toEqual(select(derivedSelectors.getUnit));
|
|
});
|
|
};
|
|
|
|
describe('when !reparsedValue', () => {
|
|
const etherTransaction = false;
|
|
const currVal = {
|
|
raw: 'raw1'
|
|
};
|
|
const reparsedValue = false;
|
|
const gen = sagas.revalidateCurrentValue();
|
|
|
|
sharedLogic(gen, etherTransaction, currVal, reparsedValue);
|
|
|
|
it('should put with setTokenValue', () => {
|
|
expect(gen.next().value).toEqual(
|
|
put(
|
|
transactionMetaActions.setTokenValue({
|
|
raw: currVal.raw,
|
|
value: null
|
|
})
|
|
)
|
|
);
|
|
});
|
|
|
|
itShouldBeDone(gen);
|
|
});
|
|
|
|
describe('when reparsedValue', () => {
|
|
const etherTransaction = true;
|
|
const currVal = {
|
|
raw: 'raw1'
|
|
};
|
|
const reparsedValue: any = {
|
|
value: 'value',
|
|
raw: 'raw'
|
|
};
|
|
const unit = 'unit';
|
|
const isValid = true;
|
|
const gen = sagas.revalidateCurrentValue();
|
|
sharedLogic(gen, etherTransaction, currVal, reparsedValue);
|
|
|
|
it('should call validateInput', () => {
|
|
expect(gen.next(unit).value).toEqual(
|
|
call(helpers.validateInput, reparsedValue.value, unit)
|
|
);
|
|
});
|
|
|
|
it('should put setValueField', () => {
|
|
expect(gen.next(isValid).value).toEqual(
|
|
put(
|
|
transactionFieldsActions.setValueField({
|
|
raw: reparsedValue.raw,
|
|
value: reparsedValue.value
|
|
} as any)
|
|
)
|
|
);
|
|
});
|
|
|
|
itShouldBeDone(gen);
|
|
});
|
|
});
|
|
|
|
describe('isValueDifferent', () => {
|
|
it('should be truthy when raw differs', () => {
|
|
const curVal: ICurrentValue = { raw: 'a', value: new BN(0) };
|
|
const newVal: ICurrentValue = { raw: 'b', value: new BN(0) };
|
|
expect(sagas.isValueDifferent(curVal, newVal)).toBeTruthy();
|
|
});
|
|
|
|
it('should be falsy when value is the same BN', () => {
|
|
const curVal: ICurrentValue = { raw: '', value: new BN(1) };
|
|
const newVal: ICurrentValue = { raw: '', value: new BN(1) };
|
|
expect(sagas.isValueDifferent(curVal, newVal)).toBeFalsy();
|
|
});
|
|
|
|
it('should be truthy when value is a different BN', () => {
|
|
const curVal: ICurrentValue = { raw: '', value: new BN(1) };
|
|
const newVal: ICurrentValue = { raw: '', value: new BN(2) };
|
|
expect(sagas.isValueDifferent(curVal, newVal)).toBeTruthy();
|
|
});
|
|
|
|
it('should be truthy when value is not the same and not both BNs', () => {
|
|
const curVal: ICurrentValue = { raw: '', value: new BN(1) };
|
|
const newVal: ICurrentValue = { raw: '', value: null };
|
|
expect(sagas.isValueDifferent(curVal, newVal)).toBeTruthy();
|
|
});
|
|
});
|
|
|
|
describe('reparseCurrentValue*', () => {
|
|
const decimal = 5;
|
|
|
|
const sharedLogic = (gen: SagaIterator, isEth: boolean) => {
|
|
it('should select isEtherTransaction', () => {
|
|
expect(gen.next().value).toEqual(select(derivedSelectors.isEtherTransaction));
|
|
});
|
|
|
|
it('should select getDecimal', () => {
|
|
expect(gen.next(isEth).value).toEqual(select(transactionMetaSelectors.getDecimal));
|
|
});
|
|
};
|
|
|
|
describe('when eth tx value is positive number and valid decimal', () => {
|
|
const value: any = {
|
|
raw: '100.0000'
|
|
};
|
|
const gen = sagas.reparseCurrentValue(value);
|
|
|
|
sharedLogic(gen, true);
|
|
|
|
it('should return correctly', () => {
|
|
expect(gen.next(decimal).value).toEqual({
|
|
raw: value.raw,
|
|
value: toTokenBase(value.raw, decimal)
|
|
});
|
|
});
|
|
|
|
itShouldBeDone(gen);
|
|
});
|
|
|
|
describe('when eth tx value is zero and decimal is valid', () => {
|
|
const value: any = {
|
|
raw: '0'
|
|
};
|
|
const gen = sagas.reparseCurrentValue(value);
|
|
|
|
sharedLogic(gen, true);
|
|
|
|
it('should return correctly', () => {
|
|
expect(gen.next(decimal).value).toEqual({
|
|
raw: value.raw,
|
|
value: toTokenBase(value.raw, decimal)
|
|
});
|
|
});
|
|
|
|
itShouldBeDone(gen);
|
|
});
|
|
|
|
describe('when eth tx value is invalid', () => {
|
|
const value: any = {
|
|
raw: 'invalidNumber'
|
|
};
|
|
const gen = sagas.reparseCurrentValue(value);
|
|
|
|
sharedLogic(gen, true);
|
|
|
|
it('should return null', () => {
|
|
expect(gen.next(decimal).value).toEqual(null);
|
|
});
|
|
|
|
itShouldBeDone(gen);
|
|
});
|
|
|
|
describe('when non-eth tx value is zero and valid decimal', () => {
|
|
const value: any = {
|
|
raw: '0'
|
|
};
|
|
const gen = sagas.reparseCurrentValue(value);
|
|
|
|
sharedLogic(gen, false);
|
|
|
|
it('should return null', () => {
|
|
expect(gen.next(decimal).value).toEqual(null);
|
|
});
|
|
|
|
itShouldBeDone(gen);
|
|
});
|
|
});
|
|
});
|
|
describe('Reset', () => {
|
|
describe('resetTransactionState*', () => {
|
|
const gen = sagas.resetTransactionState();
|
|
|
|
it('should check if this is a contract interaction tab', () => {
|
|
expect(gen.next().value).toEqual(select(transactionMetaSelectors.isContractInteraction));
|
|
});
|
|
it('should put resetActionCreator', () => {
|
|
expect(gen.next(false).value).toEqual(
|
|
put(transactionFieldsActions.resetTransactionSuccessful({ isContractInteraction: false }))
|
|
);
|
|
});
|
|
|
|
it('should be done', () => {
|
|
expect(gen.next().done).toEqual(true);
|
|
});
|
|
});
|
|
});
|
|
describe('Send Everything', () => {
|
|
describe('handleSendEverything*', () => {
|
|
let random: () => number;
|
|
beforeAll(() => {
|
|
random = Math.random;
|
|
Math.random = () => 0.001;
|
|
});
|
|
|
|
afterAll(() => {
|
|
Math.random = random;
|
|
});
|
|
|
|
const sharedStart = (gen: SagaIterator, transactionObj: any, currentBalance: BN | null) => {
|
|
it('should select getTransaction', () => {
|
|
expect(gen.next().value).toEqual(select(derivedSelectors.getTransaction));
|
|
});
|
|
|
|
it('should select getCurrentBalance', () => {
|
|
expect(gen.next(transactionObj).value).toEqual(
|
|
select(derivedSelectors.getCurrentBalance)
|
|
);
|
|
});
|
|
|
|
it('should select getEtherBalance', () => {
|
|
expect(gen.next(currentBalance).value).toEqual(select(walletSelectors.getEtherBalance));
|
|
});
|
|
};
|
|
|
|
describe('!etherBalance', () => {
|
|
const transactionObj = {
|
|
transaction: 'transaction'
|
|
};
|
|
const currentBalance = Wei('100');
|
|
const etherBalance = null;
|
|
const gen = sagas.handleSendEverything();
|
|
|
|
sharedStart(gen, transactionObj, currentBalance);
|
|
|
|
it('should put sendEverythingFailed', () => {
|
|
expect(gen.next(etherBalance).value).toEqual(put(actions.sendEverythingFailed()));
|
|
});
|
|
});
|
|
|
|
describe('!currentBalance', () => {
|
|
const transactionObj = {
|
|
transaction: 'transaction'
|
|
};
|
|
const currentBalance = null;
|
|
const etherBalance = Wei('100');
|
|
const gen = sagas.handleSendEverything();
|
|
|
|
sharedStart(gen, transactionObj, currentBalance);
|
|
|
|
it('should put sendEverythingFailed', () => {
|
|
expect(gen.next(etherBalance).value).toEqual(put(actions.sendEverythingFailed()));
|
|
});
|
|
});
|
|
|
|
describe('etherBalance && currentBalance', () => {
|
|
const transaction = {
|
|
getUpfrontCost: jest.fn()
|
|
};
|
|
const transactionObj = { transaction };
|
|
const currentBalance = Wei('100');
|
|
const etherBalance = Wei('100');
|
|
const etherTransaction = true;
|
|
|
|
const gens: any = {};
|
|
gens.gen = cloneableGenerator(sagas.handleSendEverything)();
|
|
gens.clone1 = {};
|
|
gens.clone2 = {};
|
|
|
|
sharedStart(gens.gen, transactionObj, currentBalance);
|
|
|
|
it('should select isEtherTransaction', () => {
|
|
expect(gens.gen.next(etherBalance).value).toEqual(
|
|
select(derivedSelectors.isEtherTransaction)
|
|
);
|
|
});
|
|
|
|
it('should apply transaction.getUpfrontCost', () => {
|
|
gens.clone2 = gens.gen.clone();
|
|
expect(gens.gen.next(etherTransaction).value).toEqual(
|
|
apply(transaction, transaction.getUpfrontCost)
|
|
);
|
|
gens.clone1 = gens.gen.clone();
|
|
});
|
|
|
|
describe('if totalCost > etherBalance', () => {
|
|
const totalCost = Wei('1000');
|
|
|
|
it('should put showNotification', () => {
|
|
expect(gens.clone1.next(totalCost).value).toEqual(
|
|
put(
|
|
notificationsActions.showNotification(
|
|
'warning',
|
|
`The cost of gas is higher than your balance. Total cost: ${totalCost} > Your Ether balance: ${etherBalance}`
|
|
)
|
|
)
|
|
);
|
|
});
|
|
|
|
it('should put sendEverythingFailed', () => {
|
|
expect(gens.clone1.next().value).toEqual(put(actions.sendEverythingFailed()));
|
|
});
|
|
|
|
it('should put setValueField', () => {
|
|
expect(gens.clone1.next().value).toEqual(
|
|
put(transactionFieldsActions.setValueField({ raw: '0', value: null }))
|
|
);
|
|
});
|
|
|
|
it('should be done', () => {
|
|
expect(gens.clone1.next().done).toEqual(true);
|
|
});
|
|
});
|
|
|
|
describe('if etherTransaction', () => {
|
|
const totalCost = Wei('10');
|
|
const remainder = currentBalance.sub(totalCost);
|
|
const rawVersion = fromWei(remainder, 'ether');
|
|
|
|
it('should put setValueField', () => {
|
|
expect(gens.gen.next(totalCost).value).toEqual(
|
|
put(
|
|
transactionFieldsActions.setValueField({
|
|
raw: rawVersion,
|
|
value: remainder
|
|
})
|
|
)
|
|
);
|
|
});
|
|
|
|
it('should put sendEverythingSucceeded', () => {
|
|
expect(gens.gen.next().value).toEqual(put(actions.sendEverythingSucceeded()));
|
|
});
|
|
|
|
it('should be done', () => {
|
|
expect(gens.gen.next().done).toEqual(true);
|
|
});
|
|
});
|
|
|
|
describe('if !etherTransaction (a token tx)', () => {
|
|
const totalCostLocal = Wei('1');
|
|
const etherTransactionLocal = false;
|
|
const decimal = 3;
|
|
const rawVersion = fromTokenBase(currentBalance, decimal);
|
|
|
|
it('should apply transaction.getUpfrontCost', () => {
|
|
expect(gens.clone2.next(etherTransactionLocal).value).toEqual(
|
|
apply(transaction, transaction.getUpfrontCost)
|
|
);
|
|
});
|
|
|
|
it('should select getDecimal', () => {
|
|
expect(gens.clone2.next(totalCostLocal).value).toEqual(
|
|
select(transactionMetaSelectors.getDecimal)
|
|
);
|
|
});
|
|
|
|
it('should put setTokenValue', () => {
|
|
expect(gens.clone2.next(decimal).value).toEqual(
|
|
put(transactionMetaActions.setTokenValue({ raw: rawVersion, value: currentBalance }))
|
|
);
|
|
});
|
|
|
|
it('should put sendEverythingSucceeded', () => {
|
|
expect(gens.clone2.next().value).toEqual(put(actions.sendEverythingSucceeded()));
|
|
});
|
|
|
|
it('should be done', () => {
|
|
expect(gens.clone2.next().done).toEqual(true);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|