Add reducer tests (#133)
* create config reducer tests * create generateWallet reducer tests * create wallet reducer tests * create swap reducer tests; use empty string as destination amount when origin amount is an empty string instead of 0 * add additional swap reducer tests * refactor swap; additional tests * create separate dir for swap reducer
This commit is contained in:
parent
982d70a56c
commit
a61dc268dc
|
@ -0,0 +1,36 @@
|
|||
import { combineAndUpper } from 'utils/formatters';
|
||||
import without from 'lodash/without';
|
||||
import { ALL_CRYPTO_KIND_OPTIONS } from '.';
|
||||
|
||||
export const buildDestinationAmount = (
|
||||
originAmount,
|
||||
originKind,
|
||||
destinationKind,
|
||||
bityRates
|
||||
) => {
|
||||
let pairName = combineAndUpper(originKind, destinationKind);
|
||||
let bityRate = bityRates[pairName];
|
||||
return originAmount !== null ? originAmount * bityRate : null;
|
||||
};
|
||||
|
||||
export const buildDestinationKind = (
|
||||
originKind: string,
|
||||
destinationKind: string
|
||||
): string => {
|
||||
if (originKind === destinationKind) {
|
||||
return without(ALL_CRYPTO_KIND_OPTIONS, originKind)[0];
|
||||
} else {
|
||||
return destinationKind;
|
||||
}
|
||||
};
|
||||
|
||||
export const buildOriginKind = (
|
||||
originKind: string,
|
||||
destinationKind: string
|
||||
): string => {
|
||||
if (originKind === destinationKind) {
|
||||
return without(ALL_CRYPTO_KIND_OPTIONS, destinationKind)[0];
|
||||
} else {
|
||||
return originKind;
|
||||
}
|
||||
};
|
|
@ -1,7 +1,11 @@
|
|||
// @flow
|
||||
import { combineAndUpper } from 'utils/formatters';
|
||||
import type { SwapAction } from 'actions/swapTypes';
|
||||
import without from 'lodash/without';
|
||||
import {
|
||||
buildDestinationAmount,
|
||||
buildDestinationKind,
|
||||
buildOriginKind
|
||||
} from './helpers';
|
||||
export const ALL_CRYPTO_KIND_OPTIONS = ['BTC', 'ETH', 'REP'];
|
||||
const DEFAULT_ORIGIN_KIND = 'BTC';
|
||||
const DEFAULT_DESTINATION_KIND = 'ETH';
|
||||
|
@ -49,28 +53,6 @@ export const INITIAL_STATE: State = {
|
|||
orderId: null
|
||||
};
|
||||
|
||||
const buildDestinationAmount = (
|
||||
originAmount,
|
||||
originKind,
|
||||
destinationKind,
|
||||
bityRates
|
||||
) => {
|
||||
let pairName = combineAndUpper(originKind, destinationKind);
|
||||
let bityRate = bityRates[pairName];
|
||||
return originAmount ? originAmount * bityRate : 0;
|
||||
};
|
||||
|
||||
const buildDestinationKind = (
|
||||
originKind: string,
|
||||
destinationKind: string
|
||||
): string => {
|
||||
if (originKind === destinationKind) {
|
||||
return without(ALL_CRYPTO_KIND_OPTIONS, originKind)[0];
|
||||
} else {
|
||||
return destinationKind;
|
||||
}
|
||||
};
|
||||
|
||||
export function swap(state: State = INITIAL_STATE, action: SwapAction) {
|
||||
switch (action.type) {
|
||||
case 'SWAP_ORIGIN_KIND': {
|
||||
|
@ -92,8 +74,10 @@ export function swap(state: State = INITIAL_STATE, action: SwapAction) {
|
|||
};
|
||||
}
|
||||
case 'SWAP_DESTINATION_KIND': {
|
||||
const newOriginKind = buildOriginKind(state.originKind, action.value);
|
||||
return {
|
||||
...state,
|
||||
originKind: newOriginKind,
|
||||
destinationKind: action.value,
|
||||
destinationAmount: buildDestinationAmount(
|
||||
state.originAmount,
|
||||
|
@ -148,6 +132,7 @@ export function swap(state: State = INITIAL_STATE, action: SwapAction) {
|
|||
...state,
|
||||
isPostingOrder: false
|
||||
};
|
||||
// TODO - fix bad naming
|
||||
case 'SWAP_BITY_ORDER_CREATE_SUCCEEDED':
|
||||
return {
|
||||
...state,
|
|
@ -0,0 +1,35 @@
|
|||
import { config, INITIAL_STATE } from 'reducers/config';
|
||||
import * as configActions from 'actions/config';
|
||||
import { NODES } from 'config/data';
|
||||
|
||||
describe('config reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(config(undefined, {})).toEqual(INITIAL_STATE);
|
||||
});
|
||||
|
||||
it('should handle CONFIG_LANGUAGE_CHANGE', () => {
|
||||
const language = 'en';
|
||||
expect(config(undefined, configActions.changeLanguage(language))).toEqual({
|
||||
...INITIAL_STATE,
|
||||
languageSelection: language
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle CONFIG_NODE_CHANGE', () => {
|
||||
const node = NODES[0];
|
||||
|
||||
expect(config(undefined, configActions.changeNode(node))).toEqual({
|
||||
...INITIAL_STATE,
|
||||
nodeSelection: node
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle CONFIG_GAS_PRICE', () => {
|
||||
const gasPrice = 20;
|
||||
|
||||
expect(config(undefined, configActions.changeGasPrice(gasPrice))).toEqual({
|
||||
...INITIAL_STATE,
|
||||
gasPriceGwei: gasPrice
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,25 @@
|
|||
import { generateWallet, INITIAL_STATE } from 'reducers/generateWallet';
|
||||
import * as generateWalletActions from 'actions/generateWallet';
|
||||
|
||||
describe('generateWallet reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(generateWallet(undefined, {})).toEqual(INITIAL_STATE);
|
||||
});
|
||||
|
||||
it('should handle GENERATE_WALLET_CONTINUE_TO_PAPER', () => {
|
||||
expect(
|
||||
generateWallet(undefined, generateWalletActions.continueToPaper())
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
activeStep: 'paper'
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle GENERATE_WALLET_RESET', () => {
|
||||
expect(
|
||||
generateWallet(undefined, generateWalletActions.resetGenerateWallet())
|
||||
).toEqual({
|
||||
...INITIAL_STATE
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,182 @@
|
|||
import { swap, INITIAL_STATE, ALL_CRYPTO_KIND_OPTIONS } from 'reducers/swap';
|
||||
import {
|
||||
buildDestinationAmount,
|
||||
buildDestinationKind,
|
||||
buildOriginKind
|
||||
} from 'reducers/swap/helpers';
|
||||
import * as swapActions from 'actions/swap';
|
||||
import without from 'lodash/without';
|
||||
|
||||
describe('swap reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(swap(undefined, {})).toEqual(INITIAL_STATE);
|
||||
});
|
||||
|
||||
it('should handle SWAP_ORIGIN_KIND', () => {
|
||||
const newOriginKind = 'ETH';
|
||||
const newDestinationKind = buildDestinationKind(
|
||||
newOriginKind,
|
||||
INITIAL_STATE.destinationKind
|
||||
);
|
||||
const fakeBityRates = {
|
||||
BTCETH: 10,
|
||||
ETHBTC: 0.01
|
||||
};
|
||||
expect(swap(undefined, swapActions.originKindSwap(newOriginKind))).toEqual({
|
||||
...INITIAL_STATE,
|
||||
originKind: newOriginKind,
|
||||
destinationKind: newDestinationKind,
|
||||
destinationKindOptions: without(ALL_CRYPTO_KIND_OPTIONS, newOriginKind),
|
||||
destinationAmount: buildDestinationAmount(
|
||||
INITIAL_STATE.originAmount,
|
||||
newOriginKind,
|
||||
newDestinationKind,
|
||||
fakeBityRates
|
||||
)
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_DESTINATION_KIND', () => {
|
||||
const newDestinationKind = 'REP';
|
||||
const newOriginKind = buildOriginKind(
|
||||
INITIAL_STATE.originKind,
|
||||
newDestinationKind
|
||||
);
|
||||
const fakeBityRates = {
|
||||
BTCETH: 10,
|
||||
ETHBTC: 0.01
|
||||
};
|
||||
expect(
|
||||
swap(undefined, swapActions.destinationKindSwap(newDestinationKind))
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
destinationKind: newDestinationKind,
|
||||
destinationKindOptions: without(ALL_CRYPTO_KIND_OPTIONS, newOriginKind),
|
||||
destinationAmount: buildDestinationAmount(
|
||||
INITIAL_STATE.originAmount,
|
||||
newOriginKind,
|
||||
newDestinationKind,
|
||||
fakeBityRates
|
||||
)
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_ORIGIN_AMOUNT', () => {
|
||||
const originAmount = 2;
|
||||
expect(
|
||||
swap(undefined, swapActions.originAmountSwap(originAmount))
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
originAmount
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_DESTINATION_AMOUNT', () => {
|
||||
const destinationAmount = 2;
|
||||
expect(
|
||||
swap(undefined, swapActions.destinationAmountSwap(destinationAmount))
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
destinationAmount
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_LOAD_BITY_RATES_SUCCEEDED', () => {
|
||||
const bityRates = {
|
||||
BTCETH: 0.01,
|
||||
ETHREP: 10
|
||||
};
|
||||
expect(
|
||||
swap(undefined, swapActions.loadBityRatesSucceededSwap(bityRates))
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
isFetchingRates: false,
|
||||
bityRates
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_STEP', () => {
|
||||
const step = 2;
|
||||
expect(swap(undefined, swapActions.changeStepSwap(step))).toEqual({
|
||||
...INITIAL_STATE,
|
||||
step
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_DESTINATION_ADDRESS', () => {
|
||||
const destinationAddress = '341a0sdf83';
|
||||
expect(
|
||||
swap(undefined, swapActions.destinationAddressSwap(destinationAddress))
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
destinationAddress
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_RESTART', () => {
|
||||
const bityRates = {
|
||||
BTCETH: 0.01,
|
||||
ETHREP: 10
|
||||
};
|
||||
expect(
|
||||
swap(
|
||||
{
|
||||
...INITIAL_STATE,
|
||||
bityRates,
|
||||
originAmount: 1
|
||||
},
|
||||
swapActions.restartSwap()
|
||||
)
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
bityRates
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_ORDER_CREATE_REQUESTED', () => {
|
||||
expect(swap(undefined, { type: 'SWAP_ORDER_CREATE_REQUESTED' })).toEqual({
|
||||
...INITIAL_STATE,
|
||||
isPostingOrder: true
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_ORDER_CREATE_FAILED', () => {
|
||||
expect(swap(undefined, { type: 'SWAP_ORDER_CREATE_FAILED' })).toEqual({
|
||||
...INITIAL_STATE,
|
||||
isPostingOrder: false
|
||||
});
|
||||
});
|
||||
|
||||
// TODO
|
||||
// it('should handle SWAP_BITY_ORDER_CREATE_SUCCEEDED', () => {
|
||||
// });
|
||||
//
|
||||
// it('should handle SWAP_BITY_ORDER_STATUS_SUCCEEDED', () => {
|
||||
// });
|
||||
|
||||
it('should handle SWAP_ORDER_TIME', () => {
|
||||
const secondsRemaining = 300;
|
||||
expect(
|
||||
swap(undefined, swapActions.orderTimeSwap(secondsRemaining))
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
secondsRemaining
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_LOAD_BITY_RATES_REQUESTED', () => {
|
||||
expect(
|
||||
swap(undefined, { type: 'SWAP_LOAD_BITY_RATES_REQUESTED' })
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
isFetchingRates: true
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SWAP_STOP_LOAD_BITY_RATES', () => {
|
||||
expect(swap(undefined, { type: 'SWAP_STOP_LOAD_BITY_RATES' })).toEqual({
|
||||
...INITIAL_STATE,
|
||||
isFetchingRates: false
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,29 @@
|
|||
import { wallet, INITIAL_STATE } from 'reducers/wallet';
|
||||
import * as walletActions from 'actions/wallet';
|
||||
import Big from 'bignumber.js';
|
||||
|
||||
describe('wallet reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(wallet(undefined, {})).toEqual(INITIAL_STATE);
|
||||
});
|
||||
|
||||
it('should handle WALLET_SET', () => {
|
||||
const walletInstance = { wallet: true };
|
||||
expect(wallet(undefined, walletActions.setWallet(walletInstance))).toEqual({
|
||||
...INITIAL_STATE,
|
||||
inst: walletInstance,
|
||||
balance: new Big(0),
|
||||
tokens: {}
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle WALLET_SET_TOKEN_BALANCES', () => {
|
||||
const tokenBalances = { OMG: new Big(20) };
|
||||
expect(
|
||||
wallet(undefined, walletActions.setTokenBalances(tokenBalances))
|
||||
).toEqual({
|
||||
...INITIAL_STATE,
|
||||
tokens: tokenBalances
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue