1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/ui/ducks/confirm-transaction/confirm-transaction.duck.test.js

392 lines
11 KiB
JavaScript
Raw Normal View History

import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import sinon from 'sinon';
import { TransactionStatus } from '../../../shared/constants/transaction';
import ConfirmTransactionReducer, * as actions from './confirm-transaction.duck';
const initialState = {
txData: {},
tokenData: {},
tokenProps: {},
fiatTransactionAmount: '',
fiatTransactionFee: '',
fiatTransactionTotal: '',
ethTransactionAmount: '',
ethTransactionFee: '',
ethTransactionTotal: '',
hexTransactionAmount: '',
hexTransactionFee: '',
hexTransactionTotal: '',
nonce: '',
};
const UPDATE_TX_DATA = 'metamask/confirm-transaction/UPDATE_TX_DATA';
const UPDATE_TOKEN_DATA = 'metamask/confirm-transaction/UPDATE_TOKEN_DATA';
2020-11-03 00:41:28 +01:00
const UPDATE_TRANSACTION_AMOUNTS =
'metamask/confirm-transaction/UPDATE_TRANSACTION_AMOUNTS';
2020-11-03 00:41:28 +01:00
const UPDATE_TRANSACTION_FEES =
'metamask/confirm-transaction/UPDATE_TRANSACTION_FEES';
2020-11-03 00:41:28 +01:00
const UPDATE_TRANSACTION_TOTALS =
'metamask/confirm-transaction/UPDATE_TRANSACTION_TOTALS';
const UPDATE_NONCE = 'metamask/confirm-transaction/UPDATE_NONCE';
2020-11-03 00:41:28 +01:00
const CLEAR_CONFIRM_TRANSACTION =
'metamask/confirm-transaction/CLEAR_CONFIRM_TRANSACTION';
describe('Confirm Transaction Duck', () => {
describe('State changes', () => {
const mockState = {
txData: {
id: 1,
},
tokenData: {
name: 'abcToken',
},
fiatTransactionAmount: '469.26',
fiatTransactionFee: '0.01',
fiatTransactionTotal: '1.000021',
ethTransactionAmount: '1',
ethTransactionFee: '0.000021',
ethTransactionTotal: '469.27',
hexTransactionAmount: '',
hexTransactionFee: '0x1319718a5000',
hexTransactionTotal: '',
nonce: '0x0',
};
it('should initialize state', () => {
expect(ConfirmTransactionReducer(undefined, {})).toStrictEqual(
initialState,
);
});
it('should return state unchanged if it does not match a dispatched actions type', () => {
expect(
ConfirmTransactionReducer(mockState, {
type: 'someOtherAction',
value: 'someValue',
}),
).toStrictEqual({ ...mockState });
});
it('should set txData when receiving a UPDATE_TX_DATA action', () => {
expect(
ConfirmTransactionReducer(mockState, {
type: UPDATE_TX_DATA,
payload: {
id: 2,
},
}),
).toStrictEqual({
...mockState,
txData: {
...mockState.txData,
id: 2,
},
});
});
it('should set tokenData when receiving a UPDATE_TOKEN_DATA action', () => {
expect(
ConfirmTransactionReducer(mockState, {
type: UPDATE_TOKEN_DATA,
payload: {
name: 'defToken',
},
}),
).toStrictEqual({
...mockState,
tokenData: {
...mockState.tokenData,
name: 'defToken',
},
});
});
it('should update transaction amounts when receiving an UPDATE_TRANSACTION_AMOUNTS action', () => {
expect(
ConfirmTransactionReducer(mockState, {
type: UPDATE_TRANSACTION_AMOUNTS,
payload: {
fiatTransactionAmount: '123.45',
ethTransactionAmount: '.5',
hexTransactionAmount: '0x1',
},
}),
).toStrictEqual({
...mockState,
fiatTransactionAmount: '123.45',
ethTransactionAmount: '.5',
hexTransactionAmount: '0x1',
});
});
it('should update transaction fees when receiving an UPDATE_TRANSACTION_FEES action', () => {
expect(
ConfirmTransactionReducer(mockState, {
type: UPDATE_TRANSACTION_FEES,
payload: {
fiatTransactionFee: '123.45',
ethTransactionFee: '.5',
hexTransactionFee: '0x1',
},
}),
).toStrictEqual({
...mockState,
fiatTransactionFee: '123.45',
ethTransactionFee: '.5',
hexTransactionFee: '0x1',
});
});
it('should update transaction totals when receiving an UPDATE_TRANSACTION_TOTALS action', () => {
expect(
ConfirmTransactionReducer(mockState, {
type: UPDATE_TRANSACTION_TOTALS,
payload: {
fiatTransactionTotal: '123.45',
ethTransactionTotal: '.5',
hexTransactionTotal: '0x1',
},
}),
).toStrictEqual({
...mockState,
fiatTransactionTotal: '123.45',
ethTransactionTotal: '.5',
hexTransactionTotal: '0x1',
});
});
it('should update nonce when receiving an UPDATE_NONCE action', () => {
expect(
ConfirmTransactionReducer(mockState, {
type: UPDATE_NONCE,
payload: '0x1',
}),
).toStrictEqual({
...mockState,
nonce: '0x1',
});
});
it('should clear confirmTransaction when receiving a FETCH_DATA_END action', () => {
expect(
2020-11-03 00:41:28 +01:00
ConfirmTransactionReducer(mockState, {
type: CLEAR_CONFIRM_TRANSACTION,
}),
).toStrictEqual(initialState);
});
});
describe('Single actions', function () {
it('should create an action to update txData', function () {
const txData = { test: 123 };
const expectedAction = {
type: UPDATE_TX_DATA,
payload: txData,
};
expect(actions.updateTxData(txData)).toStrictEqual(expectedAction);
});
it('should create an action to update tokenData', function () {
const tokenData = { test: 123 };
const expectedAction = {
type: UPDATE_TOKEN_DATA,
payload: tokenData,
};
expect(actions.updateTokenData(tokenData)).toStrictEqual(expectedAction);
});
it('should create an action to update transaction amounts', function () {
const transactionAmounts = { test: 123 };
const expectedAction = {
type: UPDATE_TRANSACTION_AMOUNTS,
payload: transactionAmounts,
};
expect(
actions.updateTransactionAmounts(transactionAmounts),
).toStrictEqual(expectedAction);
});
it('should create an action to update transaction fees', function () {
const transactionFees = { test: 123 };
const expectedAction = {
type: UPDATE_TRANSACTION_FEES,
payload: transactionFees,
};
expect(actions.updateTransactionFees(transactionFees)).toStrictEqual(
expectedAction,
);
});
it('should create an action to update transaction totals', function () {
const transactionTotals = { test: 123 };
const expectedAction = {
type: UPDATE_TRANSACTION_TOTALS,
payload: transactionTotals,
};
expect(actions.updateTransactionTotals(transactionTotals)).toStrictEqual(
expectedAction,
);
});
it('should create an action to update nonce', function () {
const nonce = '0x1';
const expectedAction = {
type: UPDATE_NONCE,
payload: nonce,
};
expect(actions.updateNonce(nonce)).toStrictEqual(expectedAction);
});
it('should create an action to clear confirmTransaction', () => {
const expectedAction = {
type: CLEAR_CONFIRM_TRANSACTION,
};
expect(actions.clearConfirmTransaction()).toStrictEqual(expectedAction);
});
});
describe('Thunk actions', () => {
beforeEach(() => {
global.eth = {
2020-11-03 00:41:28 +01:00
getCode: sinon
.stub()
.callsFake((address) =>
2020-11-05 18:09:42 +01:00
Promise.resolve(address?.match(/isContract/u) ? 'not-0x' : '0x'),
2020-11-03 00:41:28 +01:00
),
};
});
afterEach(function () {
global.eth.getCode.resetHistory();
});
it('updates txData and updates gas values in confirmTransaction', () => {
const txData = {
history: [],
id: 2603411941761054,
loadingDefaults: false,
metamaskNetworkId: '5',
origin: 'faucet.metamask.io',
status: TransactionStatus.unapproved,
time: 1530838113716,
txParams: {
from: '0xc5ae6383e126f901dcb06131d97a88745bfa88d6',
gas: '0x33450',
gasPrice: '0x2540be400',
to: '0x81b7e08f65bdf5648606c89998a9cc8164397647',
value: '0xde0b6b3a7640000',
},
};
const mockState = {
metamask: {
conversionRate: 468.58,
currentCurrency: 'usd',
providerConfig: {
ticker: 'ETH',
},
},
confirmTransaction: {
ethTransactionAmount: '1',
ethTransactionFee: '0.000021',
ethTransactionTotal: '1.000021',
fetchingData: false,
fiatTransactionAmount: '469.26',
fiatTransactionFee: '0.01',
fiatTransactionTotal: '469.27',
hexGasTotal: '0x1319718a5000',
methodData: {},
nonce: '',
tokenData: {},
tokenProps: {
decimals: '',
symbol: '',
},
txData: {
...txData,
txParams: {
...txData.txParams,
},
},
},
};
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const store = mockStore(mockState);
const expectedActions = [
'metamask/confirm-transaction/UPDATE_TX_DATA',
'metamask/confirm-transaction/UPDATE_TRANSACTION_AMOUNTS',
'metamask/confirm-transaction/UPDATE_TRANSACTION_FEES',
'metamask/confirm-transaction/UPDATE_TRANSACTION_TOTALS',
];
store.dispatch(actions.updateTxDataAndCalculate(txData));
const storeActions = store.getActions();
expect(storeActions).toHaveLength(expectedActions.length);
2020-11-03 00:41:28 +01:00
storeActions.forEach((action, index) =>
expect(action.type).toStrictEqual(expectedActions[index]),
);
});
it('updates confirmTransaction transaction', () => {
const mockState = {
metamask: {
conversionRate: 468.58,
currentCurrency: 'usd',
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556) The `network` store of the network controller crams two types of data into one place. It roughly tracks whether we have enough information to make requests to the network and whether the network is capable of receiving requests, but it also stores the ID of the network (as obtained via `net_version`). Generally we shouldn't be using the network ID for anything, as it has been completely replaced by chain ID, which all custom RPC endpoints have been required to support for over a year now. However, as the network ID is used in various places within the extension codebase, removing it entirely would be a non-trivial effort. So, minimally, this commit splits `network` into two stores: `networkId` and `networkStatus`. But it also expands the concept of network status. Previously, the network was in one of two states: "loading" and "not-loading". But now it can be in one of four states: - `available`: The network is able to receive and respond to requests. - `unavailable`: The network is not able to receive and respond to requests for unknown reasons. - `blocked`: The network is actively blocking requests based on the user's geolocation. (This is specific to Infura.) - `unknown`: We don't know whether the network can receive and respond to requests, either because we haven't checked or we tried to check and were unsuccessful. This commit also changes how the network status is determined — specifically, how many requests are used to determine that status, when they occur, and whether they are awaited. Previously, the network controller would make 2 to 3 requests during the course of running `lookupNetwork`. * First, if it was an Infura network, it would make a request for `eth_blockNumber` to determine whether Infura was blocking requests or not, then emit an appropriate event. This operation was not awaited. * Then, regardless of the network, it would fetch the network ID via `net_version`. This operation was awaited. * Finally, regardless of the network, it would fetch the latest block via `eth_getBlockByNumber`, then use the result to determine whether the network supported EIP-1559. This operation was awaited. Now: * One fewer request is made, specifically `eth_blockNumber`, as we don't need to make an extra request to determine whether Infura is blocking requests; we can reuse `eth_getBlockByNumber`; * All requests are awaited, which makes `lookupNetwork` run fully in-band instead of partially out-of-band; and * Both requests for `net_version` and `eth_getBlockByNumber` are performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
networkId: '5',
networkStatus: 'available',
providerConfig: {
chainId: '0x5',
},
unapprovedTxs: {
2603411941761054: {
history: [],
id: 2603411941761054,
loadingDefaults: false,
metamaskNetworkId: '5',
origin: 'faucet.metamask.io',
status: TransactionStatus.unapproved,
time: 1530838113716,
txParams: {
from: '0xc5ae6383e126f901dcb06131d97a88745bfa88d6',
gas: '0x33450',
gasPrice: '0x2540be400',
to: '0x81b7e08f65bdf5648606c89998a9cc8164397647',
value: '0xde0b6b3a7640000',
},
},
},
},
confirmTransaction: {},
};
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
const store = mockStore(mockState);
const expectedActions = [
'metamask/confirm-transaction/UPDATE_TX_DATA',
'metamask/confirm-transaction/UPDATE_TRANSACTION_AMOUNTS',
'metamask/confirm-transaction/UPDATE_TRANSACTION_FEES',
'metamask/confirm-transaction/UPDATE_TRANSACTION_TOTALS',
];
store.dispatch(actions.setTransactionToConfirm(2603411941761054));
const storeActions = store.getActions();
expect(storeActions).toHaveLength(expectedActions.length);
2020-11-03 00:41:28 +01:00
storeActions.forEach((action, index) =>
expect(action.type).toStrictEqual(expectedActions[index]),
);
});
});
});