Add tests for selectors.
This commit is contained in:
parent
84e9a59ab7
commit
d70debb7b1
|
@ -0,0 +1,75 @@
|
|||
import { getNetworkContracts, getAllTokens } from 'selectors/config';
|
||||
import { getInitialState, testShallowlyEqual } from './helpers';
|
||||
|
||||
describe('selectors/config', () => {
|
||||
describe('getNetworkContracts', () => {
|
||||
const noContractsState = getInitialState();
|
||||
noContractsState.config = {
|
||||
...noContractsState.config,
|
||||
network: {
|
||||
...noContractsState.config.network,
|
||||
contracts: null
|
||||
}
|
||||
};
|
||||
|
||||
it('should return network contracts when there are some', () => {
|
||||
const contractsState = getInitialState();
|
||||
contractsState.config = {
|
||||
...noContractsState.config,
|
||||
network: {
|
||||
...noContractsState.config.network,
|
||||
contracts: [
|
||||
{
|
||||
name: 'test',
|
||||
address: 'test',
|
||||
abi: 'test'
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
const contracts = getNetworkContracts(contractsState);
|
||||
expect(contracts).toEqual(contractsState.config.network.contracts);
|
||||
});
|
||||
|
||||
it('should return an empty array when there’re no network contracts', () => {
|
||||
const noContracts = getNetworkContracts(noContractsState);
|
||||
expect(noContracts).toBeTruthy();
|
||||
expect(noContracts).toHaveLength(0);
|
||||
});
|
||||
|
||||
testShallowlyEqual(
|
||||
getNetworkContracts(noContractsState),
|
||||
getNetworkContracts(noContractsState)
|
||||
);
|
||||
});
|
||||
|
||||
describe('getAllTokens', () => {
|
||||
const allTokensState = getInitialState();
|
||||
allTokensState.config.network = {
|
||||
...allTokensState.config.network,
|
||||
tokens: [
|
||||
{
|
||||
address: 'test',
|
||||
symbol: 'TEST',
|
||||
decimal: 18
|
||||
}
|
||||
]
|
||||
};
|
||||
allTokensState.customTokens = [
|
||||
{
|
||||
address: 'also test',
|
||||
symbol: 'TEST2',
|
||||
decimal: 18
|
||||
}
|
||||
];
|
||||
const allTokens = getAllTokens(allTokensState);
|
||||
|
||||
it('should return network tokens + custom tokens, custom tokens last', () => {
|
||||
expect(allTokens).toEqual(
|
||||
allTokensState.config.network.tokens.concat(allTokensState.customTokens)
|
||||
);
|
||||
});
|
||||
|
||||
testShallowlyEqual(allTokens, getAllTokens(allTokensState));
|
||||
});
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
import { configuredStore } from '../../common/store';
|
||||
|
||||
export function getInitialState() {
|
||||
return { ...configuredStore.getState() };
|
||||
}
|
||||
|
||||
export function testShallowlyEqual(oldValue: any, newValue: any) {
|
||||
it('should be shallowly equal when called again with the same state', () => {
|
||||
expect(oldValue === newValue).toBeTruthy();
|
||||
});
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
import { getTransaction, getGasCost, signaturePending } from 'selectors/transaction';
|
||||
import { getInitialState, testShallowlyEqual } from './helpers';
|
||||
import { Wei } from 'libs/units';
|
||||
|
||||
describe('selectrons/transaction', () => {
|
||||
describe('getTransaction', () => {
|
||||
const state = getInitialState();
|
||||
const transaction = getTransaction(state);
|
||||
|
||||
it('should return the transaction and if it’s a full one', () => {
|
||||
expect(transaction.transaction).toBeTruthy();
|
||||
expect(transaction.isFullTransaction).toBeDefined();
|
||||
});
|
||||
|
||||
testShallowlyEqual(transaction, getTransaction(state));
|
||||
});
|
||||
|
||||
describe('getGasCost', () => {
|
||||
const gasCostState = getInitialState();
|
||||
gasCostState.transaction = {
|
||||
...gasCostState.transaction,
|
||||
fields: {
|
||||
...gasCostState.transaction.fields,
|
||||
gasPrice: {
|
||||
raw: 'Doesn’t matter',
|
||||
value: Wei('2')
|
||||
},
|
||||
gasLimit: {
|
||||
raw: 'Doesn’t matter',
|
||||
value: Wei('2')
|
||||
}
|
||||
}
|
||||
};
|
||||
const gasCost = getGasCost(gasCostState);
|
||||
|
||||
it('should return the gas cost', () => {
|
||||
expect(gasCost.toString()).toBe('4');
|
||||
});
|
||||
|
||||
it('should return zero wei if gas limit value is falsy', () => {
|
||||
const zeroGasCostState = getInitialState();
|
||||
zeroGasCostState.transaction = {
|
||||
...zeroGasCostState.transaction,
|
||||
fields: {
|
||||
...zeroGasCostState.transaction.fields,
|
||||
gasPrice: {
|
||||
raw: 'Doesn’t matter',
|
||||
value: Wei('1000000000000')
|
||||
},
|
||||
gasLimit: {
|
||||
raw: 'Doesn’t matter',
|
||||
value: null
|
||||
}
|
||||
}
|
||||
};
|
||||
const zeroGasCost = getGasCost(zeroGasCostState);
|
||||
expect(zeroGasCost.toString()).toBe('0');
|
||||
});
|
||||
|
||||
testShallowlyEqual(gasCost, getGasCost(gasCostState));
|
||||
});
|
||||
|
||||
describe('signaturePending', () => {
|
||||
const state = getInitialState();
|
||||
const sigPending = signaturePending(state);
|
||||
|
||||
it('should return if the signature is pending, and if it’s a hardware wallet', () => {
|
||||
expect(sigPending.isSignaturePending).toBeDefined();
|
||||
expect(sigPending.isHardwareWallet).toBeDefined();
|
||||
});
|
||||
|
||||
testShallowlyEqual(sigPending, signaturePending(state));
|
||||
});
|
||||
});
|
|
@ -0,0 +1,158 @@
|
|||
import {
|
||||
getTokens,
|
||||
getWalletConfigTokens,
|
||||
getTokenBalances,
|
||||
getTokenBalance,
|
||||
getShownTokenBalances,
|
||||
getWalletType
|
||||
} from 'selectors/wallet';
|
||||
import { TokenValue } from 'libs/units';
|
||||
import { getInitialState, testShallowlyEqual } from './helpers';
|
||||
|
||||
describe('selectors/wallet', () => {
|
||||
const tokenBalanceState = getInitialState();
|
||||
tokenBalanceState.config = {
|
||||
...tokenBalanceState.config,
|
||||
network: {
|
||||
...tokenBalanceState.config.network,
|
||||
tokens: [
|
||||
{
|
||||
address: 'test',
|
||||
symbol: 'TEST',
|
||||
decimal: 18
|
||||
},
|
||||
{
|
||||
address: 'zero',
|
||||
symbol: 'ZERO',
|
||||
decimal: 18
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
tokenBalanceState.wallet = {
|
||||
...tokenBalanceState.wallet,
|
||||
tokens: {
|
||||
TEST: {
|
||||
balance: TokenValue('1'),
|
||||
error: null
|
||||
},
|
||||
ZERO: {
|
||||
balance: TokenValue('0'),
|
||||
error: null
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
describe('getTokens', () => {
|
||||
const tokensState = { ...tokenBalanceState };
|
||||
tokensState.customTokens = [
|
||||
{
|
||||
address: 'also test',
|
||||
symbol: 'TEST2',
|
||||
decimal: 18
|
||||
}
|
||||
];
|
||||
const tokens = getTokens(tokensState);
|
||||
|
||||
it('should return network tokens + custom tokens, custom tokens last', () => {
|
||||
expect(tokens).toHaveLength(
|
||||
tokenBalanceState.config.network.tokens.length + tokensState.customTokens.length
|
||||
);
|
||||
});
|
||||
|
||||
it('should mark custom tokens as such', () => {
|
||||
expect(tokens[tokenBalanceState.config.network.tokens.length].custom).toBeTruthy();
|
||||
});
|
||||
|
||||
testShallowlyEqual(tokens, getTokens(tokensState));
|
||||
});
|
||||
|
||||
describe('getWalletConfigTokens', () => {
|
||||
const walletConfigState = { ...tokenBalanceState };
|
||||
walletConfigState.wallet = {
|
||||
...walletConfigState.wallet,
|
||||
config: { tokens: ['TEST'] }
|
||||
};
|
||||
const walletConfigTokens = getWalletConfigTokens(walletConfigState);
|
||||
|
||||
const noWalletConfigState = getInitialState();
|
||||
walletConfigState.wallet = {
|
||||
...walletConfigState.wallet,
|
||||
config: null
|
||||
};
|
||||
const noWalletConfigTokens = getWalletConfigTokens(noWalletConfigState);
|
||||
|
||||
it('should return wallet config tokens, if there are any', () => {
|
||||
expect(walletConfigTokens).toBeTruthy();
|
||||
expect(walletConfigTokens).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should return an empty array, if no wallet config', () => {
|
||||
expect(noWalletConfigTokens).toBeTruthy();
|
||||
expect(noWalletConfigTokens).toHaveLength(0);
|
||||
});
|
||||
|
||||
// Test both config and no config to be shallowly equal
|
||||
testShallowlyEqual(walletConfigTokens, getWalletConfigTokens(walletConfigState));
|
||||
testShallowlyEqual(noWalletConfigTokens, getWalletConfigTokens(noWalletConfigState));
|
||||
});
|
||||
|
||||
describe('getTokenBalances', () => {
|
||||
const state = getInitialState();
|
||||
testShallowlyEqual(getTokenBalances(state), getTokenBalances(state));
|
||||
});
|
||||
|
||||
describe('getTokenBalance', () => {
|
||||
const tokenBalance = getTokenBalance(tokenBalanceState, 'TEST');
|
||||
|
||||
it('should retrieve the requested token’s balance', () => {
|
||||
expect(tokenBalance).toBeTruthy();
|
||||
expect(tokenBalance!.toString()).toBe('1');
|
||||
});
|
||||
|
||||
it('should return null if no token matches the symbol', () => {
|
||||
expect(getTokenBalance(tokenBalanceState, 'NOT REAL')).toBeFalsy();
|
||||
});
|
||||
|
||||
testShallowlyEqual(tokenBalance, getTokenBalance(tokenBalanceState, 'TEST'));
|
||||
});
|
||||
|
||||
describe('getShownTokenBalances', () => {
|
||||
const shownTkState = { ...tokenBalanceState };
|
||||
shownTkState.wallet.config = {
|
||||
tokens: ['TEST', 'ZERO']
|
||||
};
|
||||
const shownTkBalances = getShownTokenBalances(shownTkState);
|
||||
|
||||
it('should retrieve balances matching wallet config tokens', () => {
|
||||
expect(shownTkBalances[0].symbol).toBe('TEST');
|
||||
expect(shownTkBalances[1].symbol).toBe('ZERO');
|
||||
expect(shownTkBalances).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('should exclude zero balances, if passed that arg', () => {
|
||||
const nonZeroTkBalances = getShownTokenBalances(shownTkState, true);
|
||||
expect(nonZeroTkBalances[0].symbol).toBe('TEST');
|
||||
expect(nonZeroTkBalances).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should exclude tokens that are in config, but not in balances', () => {
|
||||
const extraTkState = { ...shownTkState };
|
||||
extraTkState.wallet.config = {
|
||||
...extraTkState.wallet.config,
|
||||
tokens: extraTkState.wallet.config!.tokens!.concat(['FAKE'])
|
||||
};
|
||||
const extraTkBalances = getShownTokenBalances(extraTkState);
|
||||
expect(shownTkBalances[0].symbol).toBe('TEST');
|
||||
expect(shownTkBalances[1].symbol).toBe('ZERO');
|
||||
expect(extraTkBalances).toHaveLength(2);
|
||||
});
|
||||
|
||||
testShallowlyEqual(shownTkBalances, getShownTokenBalances(shownTkState));
|
||||
});
|
||||
|
||||
describe('getWalletType', () => {
|
||||
const state = getInitialState();
|
||||
testShallowlyEqual(getWalletType(state), getWalletType(state));
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue