You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
540 lines
15 KiB
540 lines
15 KiB
4 years ago
|
import assert from 'assert';
|
||
|
import sinon from 'sinon';
|
||
4 years ago
|
import { TRANSACTION_STATUSES } from '../../../shared/constants/transaction';
|
||
5 years ago
|
import {
|
||
7 years ago
|
getBlockGasLimit,
|
||
7 years ago
|
getConversionRate,
|
||
6 years ago
|
getNativeCurrency,
|
||
7 years ago
|
getGasLimit,
|
||
|
getGasPrice,
|
||
|
getGasTotal,
|
||
|
getPrimaryCurrency,
|
||
5 years ago
|
getSendToken,
|
||
|
getSendTokenContract,
|
||
7 years ago
|
getSendAmount,
|
||
5 years ago
|
sendAmountIsInError,
|
||
7 years ago
|
getSendEditingTransactionId,
|
||
|
getSendErrors,
|
||
|
getSendFrom,
|
||
|
getSendFromBalance,
|
||
|
getSendFromObject,
|
||
6 years ago
|
getSendHexDataFeatureFlagState,
|
||
7 years ago
|
getSendMaxModeState,
|
||
|
getSendTo,
|
||
|
getSendToAccounts,
|
||
|
getTokenBalance,
|
||
|
getUnapprovedTxs,
|
||
5 years ago
|
gasFeeIsInError,
|
||
|
getGasLoadingError,
|
||
|
getGasButtonGroupShown,
|
||
|
getTitleKey,
|
||
|
isSendFormInError,
|
||
4 years ago
|
} from './send';
|
||
4 years ago
|
import mockState from './send-selectors-test-data';
|
||
4 years ago
|
import {
|
||
|
accountsWithSendEtherInfoSelector,
|
||
|
getCurrentAccountWithSendEtherInfo,
|
||
|
} from '.';
|
||
7 years ago
|
|
||
5 years ago
|
describe('send selectors', function () {
|
||
4 years ago
|
const tempGlobalEth = { ...global.eth };
|
||
5 years ago
|
beforeEach(function () {
|
||
7 years ago
|
global.eth = {
|
||
|
contract: sinon.stub().returns({
|
||
4 years ago
|
at: (address) => `mockAt:${address}`,
|
||
7 years ago
|
}),
|
||
4 years ago
|
};
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
afterEach(function () {
|
||
4 years ago
|
global.eth = tempGlobalEth;
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('accountsWithSendEtherInfoSelector()', function () {
|
||
|
it('should return an array of account objects with name info from identities', function () {
|
||
4 years ago
|
assert.deepStrictEqual(accountsWithSendEtherInfoSelector(mockState), [
|
||
4 years ago
|
{
|
||
|
code: '0x',
|
||
|
balance: '0x47c9d71831c76efe',
|
||
|
nonce: '0x1b',
|
||
|
address: '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825',
|
||
|
name: 'Send Account 1',
|
||
|
},
|
||
|
{
|
||
|
code: '0x',
|
||
|
balance: '0x37452b1315889f80',
|
||
|
nonce: '0xa',
|
||
|
address: '0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb',
|
||
|
name: 'Send Account 2',
|
||
|
},
|
||
|
{
|
||
|
code: '0x',
|
||
|
balance: '0x30c9d71831c76efe',
|
||
|
nonce: '0x1c',
|
||
|
address: '0x2f8d4a878cfa04a6e60d46362f5644deab66572d',
|
||
|
name: 'Send Account 3',
|
||
|
},
|
||
|
{
|
||
|
code: '0x',
|
||
|
balance: '0x0',
|
||
|
nonce: '0x0',
|
||
|
address: '0xd85a4b6a394794842887b8284293d69163007bbb',
|
||
|
name: 'Send Account 4',
|
||
|
},
|
||
4 years ago
|
]);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getBlockGasLimit', function () {
|
||
|
it('should return the current block gas limit', function () {
|
||
4 years ago
|
assert.deepStrictEqual(getBlockGasLimit(mockState), '0x4c1878');
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getConversionRate()', function () {
|
||
|
it('should return the eth conversion rate', function () {
|
||
4 years ago
|
assert.deepStrictEqual(getConversionRate(mockState), 1200.88200327);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getCurrentAccountWithSendEtherInfo()', function () {
|
||
|
it('should return the currently selected account with identity info', function () {
|
||
4 years ago
|
assert.deepStrictEqual(getCurrentAccountWithSendEtherInfo(mockState), {
|
||
4 years ago
|
code: '0x',
|
||
|
balance: '0x0',
|
||
|
nonce: '0x0',
|
||
|
address: '0xd85a4b6a394794842887b8284293d69163007bbb',
|
||
|
name: 'Send Account 4',
|
||
4 years ago
|
});
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getNativeCurrency()', function () {
|
||
|
it('should return the ticker symbol of the selected network', function () {
|
||
4 years ago
|
assert.strictEqual(getNativeCurrency(mockState), 'ETH');
|
||
|
});
|
||
|
});
|
||
6 years ago
|
|
||
5 years ago
|
describe('getGasLimit()', function () {
|
||
|
it('should return the send.gasLimit', function () {
|
||
4 years ago
|
assert.strictEqual(getGasLimit(mockState), '0xFFFF');
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getGasPrice()', function () {
|
||
|
it('should return the send.gasPrice', function () {
|
||
4 years ago
|
assert.strictEqual(getGasPrice(mockState), '0xaa');
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getGasTotal()', function () {
|
||
|
it('should return the send.gasTotal', function () {
|
||
4 years ago
|
assert.strictEqual(getGasTotal(mockState), 'a9ff56');
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getPrimaryCurrency()', function () {
|
||
5 years ago
|
it('should return the symbol of the send token', function () {
|
||
4 years ago
|
assert.strictEqual(
|
||
4 years ago
|
getPrimaryCurrency({
|
||
|
metamask: { send: { token: { symbol: 'DEF' } } },
|
||
|
}),
|
||
4 years ago
|
'DEF',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendToken()', function () {
|
||
|
it('should return the current send token if set', function () {
|
||
4 years ago
|
assert.deepStrictEqual(
|
||
5 years ago
|
getSendToken({
|
||
|
metamask: {
|
||
|
send: {
|
||
|
token: {
|
||
|
address: '0x8d6b81208414189a58339873ab429b6c47ab92d3',
|
||
|
decimals: 4,
|
||
|
symbol: 'DEF',
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
7 years ago
|
{
|
||
7 years ago
|
address: '0x8d6b81208414189a58339873ab429b6c47ab92d3',
|
||
|
decimals: 4,
|
||
|
symbol: 'DEF',
|
||
4 years ago
|
},
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendTokenContract()', function () {
|
||
|
it('should return the contract at the send token address', function () {
|
||
4 years ago
|
assert.strictEqual(
|
||
5 years ago
|
getSendTokenContract({
|
||
|
metamask: {
|
||
|
send: {
|
||
|
token: {
|
||
|
address: '0x8d6b81208414189a58339873ab429b6c47ab92d3',
|
||
|
decimals: 4,
|
||
|
symbol: 'DEF',
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
4 years ago
|
'mockAt:0x8d6b81208414189a58339873ab429b6c47ab92d3',
|
||
4 years ago
|
);
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
it('should return null if send token is not set', function () {
|
||
4 years ago
|
const modifiedMetamaskState = { ...mockState.metamask, send: {} };
|
||
4 years ago
|
assert.strictEqual(
|
||
4 years ago
|
getSendTokenContract({ ...mockState, metamask: modifiedMetamaskState }),
|
||
4 years ago
|
null,
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendAmount()', function () {
|
||
|
it('should return the send.amount', function () {
|
||
4 years ago
|
assert.strictEqual(getSendAmount(mockState), '0x080');
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendEditingTransactionId()', function () {
|
||
|
it('should return the send.editingTransactionId', function () {
|
||
4 years ago
|
assert.strictEqual(getSendEditingTransactionId(mockState), 97531);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendErrors()', function () {
|
||
|
it('should return the send.errors', function () {
|
||
4 years ago
|
assert.deepStrictEqual(getSendErrors(mockState), { someError: null });
|
||
|
});
|
||
|
});
|
||
6 years ago
|
|
||
5 years ago
|
describe('getSendHexDataFeatureFlagState()', function () {
|
||
|
it('should return the sendHexData feature flag state', function () {
|
||
4 years ago
|
assert.deepStrictEqual(getSendHexDataFeatureFlagState(mockState), true);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendFrom()', function () {
|
||
|
it('should return the send.from', function () {
|
||
4 years ago
|
assert.deepStrictEqual(
|
||
7 years ago
|
getSendFrom(mockState),
|
||
5 years ago
|
'0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendFromBalance()', function () {
|
||
|
it('should get the send.from balance if it exists', function () {
|
||
4 years ago
|
assert.strictEqual(getSendFromBalance(mockState), '0x37452b1315889f80');
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
it('should get the selected account balance if the send.from does not exist', function () {
|
||
7 years ago
|
const editedMockState = {
|
||
4 years ago
|
metamask: {
|
||
|
...mockState.metamask,
|
||
7 years ago
|
send: {
|
||
|
from: null,
|
||
|
},
|
||
4 years ago
|
},
|
||
4 years ago
|
};
|
||
|
assert.strictEqual(getSendFromBalance(editedMockState), '0x0');
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendFromObject()', function () {
|
||
|
it('should return send.from if it exists', function () {
|
||
4 years ago
|
assert.deepStrictEqual(getSendFromObject(mockState), {
|
||
4 years ago
|
address: '0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb',
|
||
|
balance: '0x37452b1315889f80',
|
||
|
code: '0x',
|
||
|
nonce: '0xa',
|
||
4 years ago
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
it('should return the current account if send.from does not exist', function () {
|
||
7 years ago
|
const editedMockState = {
|
||
4 years ago
|
metamask: {
|
||
|
...mockState.metamask,
|
||
7 years ago
|
send: {
|
||
|
from: null,
|
||
|
},
|
||
4 years ago
|
},
|
||
4 years ago
|
};
|
||
4 years ago
|
assert.deepStrictEqual(getSendFromObject(editedMockState), {
|
||
4 years ago
|
code: '0x',
|
||
|
balance: '0x0',
|
||
|
nonce: '0x0',
|
||
|
address: '0xd85a4b6a394794842887b8284293d69163007bbb',
|
||
4 years ago
|
});
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendMaxModeState()', function () {
|
||
|
it('should return send.maxModeOn', function () {
|
||
4 years ago
|
assert.strictEqual(getSendMaxModeState(mockState), false);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendTo()', function () {
|
||
|
it('should return send.to', function () {
|
||
4 years ago
|
assert.strictEqual(getSendTo(mockState), '0x987fedabc');
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getSendToAccounts()', function () {
|
||
|
it('should return an array including all the users accounts and the address book', function () {
|
||
4 years ago
|
assert.deepStrictEqual(getSendToAccounts(mockState), [
|
||
4 years ago
|
{
|
||
|
code: '0x',
|
||
|
balance: '0x47c9d71831c76efe',
|
||
|
nonce: '0x1b',
|
||
|
address: '0xfdea65c8e26263f6d9a1b5de9555d2931a33b825',
|
||
|
name: 'Send Account 1',
|
||
|
},
|
||
|
{
|
||
|
code: '0x',
|
||
|
balance: '0x37452b1315889f80',
|
||
|
nonce: '0xa',
|
||
|
address: '0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb',
|
||
|
name: 'Send Account 2',
|
||
|
},
|
||
|
{
|
||
|
code: '0x',
|
||
|
balance: '0x30c9d71831c76efe',
|
||
|
nonce: '0x1c',
|
||
|
address: '0x2f8d4a878cfa04a6e60d46362f5644deab66572d',
|
||
|
name: 'Send Account 3',
|
||
|
},
|
||
|
{
|
||
|
code: '0x',
|
||
|
balance: '0x0',
|
||
|
nonce: '0x0',
|
||
|
address: '0xd85a4b6a394794842887b8284293d69163007bbb',
|
||
|
name: 'Send Account 4',
|
||
|
},
|
||
|
{
|
||
|
address: '0x06195827297c7a80a443b6894d3bdb8824b43896',
|
||
|
name: 'Address Book Account 1',
|
||
|
chainId: '0x3',
|
||
|
},
|
||
4 years ago
|
]);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getTokenBalance()', function () {
|
||
|
it('should', function () {
|
||
4 years ago
|
assert.strictEqual(getTokenBalance(mockState), 3434);
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
5 years ago
|
describe('getUnapprovedTxs()', function () {
|
||
|
it('should return the unapproved txs', function () {
|
||
4 years ago
|
assert.deepStrictEqual(getUnapprovedTxs(mockState), {
|
||
4 years ago
|
4768706228115573: {
|
||
|
id: 4768706228115573,
|
||
|
time: 1487363153561,
|
||
4 years ago
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
||
4 years ago
|
gasMultiplier: 1,
|
||
|
metamaskNetworkId: '3',
|
||
|
txParams: {
|
||
|
from: '0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb',
|
||
|
to: '0x18a3462427bcc9133bb46e88bcbe39cd7ef0e761',
|
||
|
value: '0xde0b6b3a7640000',
|
||
|
metamaskId: 4768706228115573,
|
||
7 years ago
|
metamaskNetworkId: '3',
|
||
4 years ago
|
gas: '0x5209',
|
||
7 years ago
|
},
|
||
4 years ago
|
txFee: '17e0186e60800',
|
||
|
txValue: 'de0b6b3a7640000',
|
||
|
maxCost: 'de234b52e4a0800',
|
||
|
gasPrice: '4a817c800',
|
||
4 years ago
|
},
|
||
4 years ago
|
});
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('send-amount-row selectors', function () {
|
||
|
describe('sendAmountIsInError()', function () {
|
||
|
it('should return true if send.errors.amount is truthy', function () {
|
||
|
const state = {
|
||
|
send: {
|
||
|
errors: {
|
||
|
amount: 'abc',
|
||
|
},
|
||
|
},
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
assert.strictEqual(sendAmountIsInError(state), true);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should return false if send.errors.amount is falsy', function () {
|
||
|
const state = {
|
||
|
send: {
|
||
|
errors: {
|
||
|
amount: null,
|
||
|
},
|
||
|
},
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
assert.strictEqual(sendAmountIsInError(state), false);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('send-gas-row selectors', function () {
|
||
|
describe('getGasLoadingError()', function () {
|
||
|
it('should return send.errors.gasLoading', function () {
|
||
|
const state = {
|
||
|
send: {
|
||
|
errors: {
|
||
|
gasLoading: 'abc',
|
||
|
},
|
||
|
},
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
assert.strictEqual(getGasLoadingError(state), 'abc');
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('gasFeeIsInError()', function () {
|
||
|
it('should return true if send.errors.gasFee is truthy', function () {
|
||
|
const state = {
|
||
|
send: {
|
||
|
errors: {
|
||
|
gasFee: 'def',
|
||
|
},
|
||
|
},
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
assert.strictEqual(gasFeeIsInError(state), true);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should return false send.errors.gasFee is falsely', function () {
|
||
|
const state = {
|
||
|
send: {
|
||
|
errors: {
|
||
|
gasFee: null,
|
||
|
},
|
||
|
},
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
assert.strictEqual(gasFeeIsInError(state), false);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('getGasButtonGroupShown()', function () {
|
||
|
it('should return send.gasButtonGroupShown', function () {
|
||
|
const state = {
|
||
|
send: {
|
||
|
gasButtonGroupShown: 'foobar',
|
||
|
},
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
assert.strictEqual(getGasButtonGroupShown(state), 'foobar');
|
||
|
});
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('send-header selectors', function () {
|
||
|
const getMetamaskSendMockState = (send) => {
|
||
|
return {
|
||
|
metamask: {
|
||
|
send: { ...send },
|
||
|
},
|
||
4 years ago
|
};
|
||
|
};
|
||
5 years ago
|
|
||
|
describe('getTitleKey()', function () {
|
||
|
it('should return the correct key when "to" is empty', function () {
|
||
4 years ago
|
assert.strictEqual(
|
||
|
getTitleKey(getMetamaskSendMockState({})),
|
||
|
'addRecipient',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should return the correct key when getSendEditingTransactionId is truthy', function () {
|
||
4 years ago
|
assert.strictEqual(
|
||
5 years ago
|
getTitleKey(
|
||
|
getMetamaskSendMockState({
|
||
|
to: true,
|
||
|
editingTransactionId: true,
|
||
5 years ago
|
token: {},
|
||
4 years ago
|
}),
|
||
4 years ago
|
),
|
||
|
'edit',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
5 years ago
|
it('should return the correct key when getSendEditingTransactionId is falsy and getSendToken is truthy', function () {
|
||
4 years ago
|
assert.strictEqual(
|
||
5 years ago
|
getTitleKey(
|
||
|
getMetamaskSendMockState({
|
||
|
to: true,
|
||
|
editingTransactionId: false,
|
||
5 years ago
|
token: {},
|
||
4 years ago
|
}),
|
||
4 years ago
|
),
|
||
|
'sendTokens',
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
5 years ago
|
it('should return the correct key when getSendEditingTransactionId is falsy and getSendToken is falsy', function () {
|
||
4 years ago
|
assert.strictEqual(
|
||
5 years ago
|
getTitleKey(
|
||
|
getMetamaskSendMockState({
|
||
|
to: true,
|
||
|
editingTransactionId: false,
|
||
5 years ago
|
token: null,
|
||
4 years ago
|
}),
|
||
4 years ago
|
),
|
||
|
'sendETH',
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('send-footer selectors', function () {
|
||
|
const getSendMockState = (send) => {
|
||
|
return {
|
||
|
send: { ...send },
|
||
4 years ago
|
};
|
||
|
};
|
||
5 years ago
|
|
||
|
describe('isSendFormInError()', function () {
|
||
|
it('should return true if any of the values of the object returned by getSendErrors are truthy', function () {
|
||
4 years ago
|
assert.strictEqual(
|
||
4 years ago
|
isSendFormInError(
|
||
|
getSendMockState({
|
||
|
errors: [true],
|
||
|
}),
|
||
|
),
|
||
|
true,
|
||
4 years ago
|
);
|
||
|
});
|
||
5 years ago
|
|
||
|
it('should return false if all of the values of the object returned by getSendErrors are falsy', function () {
|
||
4 years ago
|
assert.strictEqual(
|
||
4 years ago
|
isSendFormInError(
|
||
|
getSendMockState({
|
||
|
errors: [],
|
||
|
}),
|
||
|
),
|
||
|
false,
|
||
4 years ago
|
);
|
||
4 years ago
|
assert.strictEqual(
|
||
4 years ago
|
isSendFormInError(
|
||
|
getSendMockState({
|
||
|
errors: [false],
|
||
|
}),
|
||
|
),
|
||
|
false,
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|