MyCrypto/spec/sagas/transaction/meta/unitSwap.spec.ts
Daniel Kmak 985ea0fb89 Ethereum Alarm Clock Integration (#1343)
* [FEATURE] Initial EAC integration.

* Title and explanation

* [FEATURE] Move the Schedule Payment to the main tab.

* [FEATURE] TimeBounty slider.

* [FEATURE] Move to main menu.

* [FEATURE] Redirection to the DApp for details.

* [FEATURE] Timestamp scheduling

* Scheduling: Basic date and time widget

* Linting fixes

* Moved the datetime field to new tab

* Fixed push errors

* Added missing specs

* Undid unintentional UI change

* Fixed some failing tests

* Ignore datetime parameter when checking if a transaction is full

* Added a date selector widget and renamed ScheduleTimestamp to ScheduleDate

* Marked componentDidMount

* Initialized Pikaday

* Revert "Initialized Pikaday"

This reverts commit 4e5bf5b2b882f236f5977400abf9b7092cbd1592.

* Revert "Marked componentDidMount"

This reverts commit 85d52192ac58f4b6ca9219e702f7390cd27e582f.

* Revert "Added a date selector widget and renamed ScheduleTimestamp to ScheduleDate"

This reverts commit aaad0ac9b565a78d1bfc631754160919fd38a59b.

* Converted the date picker into a datetime picker

* Added decent styling to the datetimepicker

* Added validation to the datetime picker

* Fixed prepush errors for scheduling timestamp

* Adjusted validation logic scheduling timestamp

* [FEATURE] Move scheduling to main tab.

* [FEATURE] Timezone selector

* [FEATURE] Scheduling: Timezone selector

* Removed zombie files

* [FEATURE] Reimplement Time Bounty.

* [FEATURE] Time/block selector

* [FEATURE] Add Window Size field.

* [FEATURE] Time/block switch functionality

* Implemented time/block switcher fuctionality

* [FEATURE] Add Schedule Gas Price field.

* [FEATURE] Scheduling toggle

* [FEATURE] Add basic styling and network check.

* [FEATURE] Add Schedule Gas Limit field

* [FEATURE] "Scheduled" button styling

* Reordered, renamed and centered scheduling elements

* Added the toggle button styling

* Class -> ClassName

* [FEATURE] Add Deposit field

*  [FEATURE] Move scheduling code into one directory

* [FIX] Scheduling responsiveness

* [FIX] Datetime picker not working on md screens

* [FEATURE] Timestamp Scheduling basic functionality

* [FIX] Fix data serialization.

* [FEATURE] Timezone inclusion

* [FEATURE] Add ChronoLogic logo.

* [FEATURE] Add link to image.

* [FIX] Update CSS of logo.

* [FEATURE] Default Window Size

* [FEATURE] Modified Help component to enable acting as a tooltip

* [FEATURE] Call contract to validate scheduling params

* [FIX] Change moment import to fix tests

* [FEATURE] Gas estimation for scheduling

* [FEATURE] Additional validation

* [FEATURE] UI changes and descriptions

* [FEATURE] Add tooltip to window and fix fee display.

* [FIX] Fix ethereumjs-abi dependency.

* [FEATURE] Hide scheduling when sending tokens.

* [FIX] Improved styling datetime picker

* [FEATURE] Add Redux state for scheduling

* [FEATURE] Create Toggle component, Share code between components

* [FEATURE] Use Tooltip component for help.

* [FEATURE] Better datetime picker

* [FEATURE] Remove fee

* Trigger mycryptobuild

* [FIX] Timestamp scheduling - Validation match

* [FIX] EAC integration touchups

* [FIX] Code review fixes

* [FIX] Window Size type

* [FIX] Type fixes.

* [FIX] Make tooltips only show on icons + resposiveness fixes

* [FIX] Break tooltips into more lines

* [FIX] Remove unnecessary code.

* [FIX] Remove unnecessary code.

* [FIX] Remove unnecessary types declaration.

* [FIX] Fee class names
2018-04-14 17:21:33 -05:00

282 lines
7.9 KiB
TypeScript

import { select, call, put } from 'redux-saga/effects';
import {
getTokenTo,
getTokenValue,
getTo,
getPreviousUnit,
getValue,
getDecimalFromUnit
} from 'selectors/transaction';
import { getToken } from 'selectors/wallet';
import { Wei, Address } from 'libs/units';
import {
swapTokenToEther,
swapEtherToToken,
swapTokenToToken
} from 'actions/transaction/actionCreators/swap';
import { encodeTransfer } from 'libs/transaction';
import { bufferToHex } from 'ethereumjs-util';
import { rebaseUserInput, validateInput } from 'sagas/transaction/validationHelpers';
import { cloneableGenerator, SagaIteratorClone } from 'redux-saga/utils';
import { handleSetUnitMeta } from 'sagas/transaction/meta/unitSwap';
import { isNetworkUnit } from 'selectors/config';
import { SagaIterator } from 'redux-saga';
import BN from 'bn.js';
import { setSchedulingToggle } from 'actions/schedule';
const itShouldBeDone = (gen: SagaIterator) => {
it('should be done', () => {
expect(gen.next().done).toEqual(true);
});
};
describe('handleSetUnitMeta*', () => {
const expectedStart = (
gen: SagaIterator,
previousUnit: string,
currentUnit: string,
prevUnitIsNetworkUnit: boolean,
currUnitIsNetworkUnit: boolean
) => {
it('should select getPreviousUnit', () => {
expect(gen.next().value).toEqual(select(getPreviousUnit));
});
it('should check if prevUnit is a network unit', () => {
expect(gen.next(previousUnit).value).toEqual(select(isNetworkUnit, previousUnit));
});
it('should check if currUnit is a network unit', () => {
expect(gen.next(prevUnitIsNetworkUnit).value).toEqual(select(isNetworkUnit, currentUnit));
});
it('should select getDeciimalFromUnit with currentUnit', () => {
expect(gen.next(currUnitIsNetworkUnit).value).toEqual(
select(getDecimalFromUnit, currentUnit)
);
});
};
describe('etherToEther', () => {
const currentUnit = 'ETH';
const previousUnit = 'ETH';
const action: any = {
payload: currentUnit
};
const gen = handleSetUnitMeta(action);
expectedStart(gen, previousUnit, currentUnit, true, true);
it('should return correctly', () => {
expect(gen.next().value).toEqual(undefined);
});
itShouldBeDone(gen);
});
describe('tokenToEther', () => {
const previousUnit = 'token';
const currentUnit = 'ETH';
const action: any = {
payload: currentUnit
};
const decimal = 1;
const tokenTo: any = 'tokenTo';
const tokenValue: any = 'tokenValue';
const raw = 'raw';
const value: any = 'value';
const gen = handleSetUnitMeta(action);
expectedStart(gen, previousUnit, currentUnit, false, true);
it('should select getTokenTo', () => {
expect(gen.next(decimal).value).toEqual(select(getTokenTo));
});
it('should select getTokenValue', () => {
expect(gen.next(tokenTo).value).toEqual(select(getTokenValue));
});
it('should call rebaseUserInput with tokenValue', () => {
expect(gen.next(tokenValue).value).toEqual(call(rebaseUserInput, tokenValue));
});
it('should call validateInput with value and currentUnit', () => {
expect(gen.next({ value, raw }).value).toEqual(call(validateInput, value, currentUnit));
});
it('should put swapTokenToEther', () => {
expect(gen.next(true).value).toEqual(
put(
swapTokenToEther({
to: tokenTo,
value: {
raw,
value
},
decimal
} as any)
)
);
});
itShouldBeDone(gen);
});
describe('etherToToken || tokenToToken', () => {
const sharedLogicA = (gen: SagaIteratorClone, decimal: number, currentUnit: string) => {
it('should select getToken with currentUnit', () => {
expect(gen.next(decimal).value).toEqual(select(getToken, currentUnit));
});
it('should throw error if !currentToken', () => {
const clone = gen.clone();
expect(() => clone.next(false)).toThrowError('Could not find token during unit swap');
});
};
const sharedLogicB = (
gen: SagaIterator,
input: string,
raw: string,
value: BN,
currentUnit: string,
isValid: boolean
) => {
it('should call rebaseUserInput with input', () => {
expect(gen.next(input).value).toEqual(call(rebaseUserInput, input as any));
});
it('should call validateInput with value and currentUnit', () => {
expect(gen.next({ raw, value }).value).toEqual(call(validateInput, value, currentUnit));
});
it('should select getTo', () => {
expect(gen.next(isValid).value).toEqual(select(getTo));
});
};
const constructExpectedPayload = (
data: Buffer,
toAddress: string,
raw: string,
value: BN,
decimal: number,
tokenTo?: any
) => {
const base = {
data: { raw: bufferToHex(data), value: data },
to: { raw: '', value: Address(toAddress) },
tokenValue: { raw, value },
decimal
};
if (!tokenTo) {
return base;
}
return {
...base,
tokenTo
};
};
describe('etherToToken', () => {
const previousUnit = 'ETH';
const currentUnit = 'token';
const action: any = {
payload: currentUnit
};
const currentToken = {
address: '0x0'
};
const decimal = 1;
const input = 'input';
const raw = 'raw';
const value = Wei('100');
const isValid = true;
const to = { value: value.toBuffer() };
const gens: any = {};
gens.gen = cloneableGenerator(handleSetUnitMeta)(action);
expectedStart(gens.gen, previousUnit, currentUnit, true, false);
sharedLogicA(gens.gen, decimal, currentUnit);
it('should select getValue', () => {
expect(gens.gen.next(currentToken).value).toEqual(select(getValue));
});
sharedLogicB(gens.gen, input, raw, value, currentUnit, isValid);
it('should put setSchedulingToogle', () => {
expect(gens.gen.next(to).value).toEqual(
put(
setSchedulingToggle({
value: false
})
)
);
});
it('should put swapEtherToToken', () => {
const data = encodeTransfer(to.value, value);
const payload: any = constructExpectedPayload(
data,
currentToken.address,
raw,
value,
decimal,
to
);
expect(gens.gen.next(to).value).toEqual(put(swapEtherToToken(payload)));
});
itShouldBeDone(gens.gen);
});
describe('tokenToToken', () => {
const previousUnit = 'token';
const currentUnit = 'token';
const action: any = {
payload: currentUnit
};
const currentToken = {
address: '0x1'
};
const decimal = 1;
const input = 'input';
const raw = 'raw';
const value = Wei('100');
const isValid = true;
const to = { value: '0xa' };
const tokenTo = { value: '0xb' };
const gens: any = {};
gens.gen = cloneableGenerator(handleSetUnitMeta)(action);
expectedStart(gens.gen, previousUnit, currentUnit, false, false);
sharedLogicA(gens.gen, decimal, currentUnit);
it('should select getTokenValue', () => {
expect(gens.gen.next(currentToken).value).toEqual(select(getTokenValue));
});
sharedLogicB(gens.gen, input, raw, value, currentUnit, isValid);
it('should select getTokenTo', () => {
expect(gens.gen.next(to).value).toEqual(select(getTokenTo));
});
it('should put swapEtherToToken', () => {
const data = encodeTransfer(Address(tokenTo.value), value);
const payload = constructExpectedPayload(data, currentToken.address, raw, value, decimal);
expect(gens.gen.next(tokenTo).value).toEqual(put(swapTokenToToken(payload)));
});
itShouldBeDone(gens.gen);
});
});
});