1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/ui/pages/send/send-footer/send-footer.container.test.js
2021-04-28 14:53:59 -05:00

188 lines
5.4 KiB
JavaScript

import sinon from 'sinon';
import {
clearSend,
signTx,
signTokenTx,
addToAddressBook,
} from '../../../store/actions';
import {
addressIsNew,
constructTxParams,
constructUpdatedTx,
} from './send-footer.utils';
let mapDispatchToProps;
jest.mock('react-redux', () => ({
connect: (_, md) => {
mapDispatchToProps = md;
return () => ({});
},
}));
jest.mock('../../../store/actions.js', () => ({
addToAddressBook: jest.fn(),
clearSend: jest.fn(),
signTokenTx: jest.fn(),
signTx: jest.fn(),
updateTransaction: jest.fn(),
}));
jest.mock('../../../selectors/send.js', () => ({
getGasLimit: (s) => `mockGasLimit:${s}`,
getGasPrice: (s) => `mockGasPrice:${s}`,
getGasTotal: (s) => `mockGasTotal:${s}`,
getSendToken: (s) => `mockSendToken:${s}`,
getSendAmount: (s) => `mockAmount:${s}`,
getSendEditingTransactionId: (s) => `mockEditingTransactionId:${s}`,
getSendFromObject: (s) => `mockFromObject:${s}`,
getSendTo: (s) => `mockTo:${s}`,
getSendToNickname: (s) => `mockToNickname:${s}`,
getSendToAccounts: (s) => `mockToAccounts:${s}`,
getTokenBalance: (s) => `mockTokenBalance:${s}`,
getSendHexData: (s) => `mockHexData:${s}`,
getUnapprovedTxs: (s) => `mockUnapprovedTxs:${s}`,
getSendErrors: (s) => `mockSendErrors:${s}`,
isSendFormInError: (s) => `mockInError:${s}`,
getDefaultActiveButtonIndex: () => 0,
}));
jest.mock('../../../selectors/custom-gas.js', () => ({
getRenderableEstimateDataForSmallButtonsFromGWEI: (s) => [
{ gasEstimateType: `mockGasEstimateType:${s}` },
],
}));
jest.mock('./send-footer.utils', () => ({
addressIsNew: jest.fn().mockReturnValue(true),
constructTxParams: jest.fn().mockReturnValue({ value: 'mockAmount' }),
constructUpdatedTx: jest
.fn()
.mockReturnValue('mockConstructedUpdatedTxParams'),
}));
require('./send-footer.container.js');
describe('send-footer container', () => {
describe('mapDispatchToProps()', () => {
let dispatchSpy;
let mapDispatchToPropsObject;
beforeEach(() => {
dispatchSpy = sinon.spy();
mapDispatchToPropsObject = mapDispatchToProps(dispatchSpy);
});
describe('clearSend()', () => {
it('should dispatch an action', () => {
mapDispatchToPropsObject.clearSend();
expect(dispatchSpy.calledOnce).toStrictEqual(true);
expect(clearSend).toHaveBeenCalled();
});
});
describe('sign()', () => {
it('should dispatch a signTokenTx action if sendToken is defined', () => {
mapDispatchToPropsObject.sign({
sendToken: {
address: '0xabc',
},
to: 'mockTo',
amount: 'mockAmount',
from: 'mockFrom',
gas: 'mockGas',
gasPrice: 'mockGasPrice',
});
expect(dispatchSpy.calledOnce).toStrictEqual(true);
expect(constructTxParams).toHaveBeenCalledWith({
data: undefined,
sendToken: {
address: '0xabc',
},
to: 'mockTo',
amount: 'mockAmount',
from: 'mockFrom',
gas: 'mockGas',
gasPrice: 'mockGasPrice',
});
expect(signTokenTx).toHaveBeenCalledWith(
'0xabc',
'mockTo',
'mockAmount',
{ value: 'mockAmount' },
);
});
it('should dispatch a sign action if sendToken is not defined', () => {
mapDispatchToPropsObject.sign({
to: 'mockTo',
amount: 'mockAmount',
from: 'mockFrom',
gas: 'mockGas',
gasPrice: 'mockGasPrice',
});
expect(dispatchSpy.calledOnce).toStrictEqual(true);
expect(constructTxParams).toHaveBeenCalledWith({
data: undefined,
sendToken: undefined,
to: 'mockTo',
amount: 'mockAmount',
from: 'mockFrom',
gas: 'mockGas',
gasPrice: 'mockGasPrice',
});
expect(signTx).toHaveBeenCalledWith({
value: 'mockAmount',
});
});
});
describe('update()', () => {
it('should dispatch an updateTransaction action', () => {
mapDispatchToPropsObject.update({
to: 'mockTo',
amount: 'mockAmount',
from: 'mockFrom',
gas: 'mockGas',
gasPrice: 'mockGasPrice',
editingTransactionId: 'mockEditingTransactionId',
sendToken: { address: 'mockAddress' },
unapprovedTxs: 'mockUnapprovedTxs',
});
expect(dispatchSpy.calledOnce).toStrictEqual(true);
expect(constructUpdatedTx).toHaveBeenCalledWith({
data: undefined,
to: 'mockTo',
amount: 'mockAmount',
from: 'mockFrom',
gas: 'mockGas',
gasPrice: 'mockGasPrice',
editingTransactionId: 'mockEditingTransactionId',
sendToken: { address: 'mockAddress' },
unapprovedTxs: 'mockUnapprovedTxs',
});
});
});
describe('addToAddressBookIfNew()', () => {
it('should dispatch an action', () => {
mapDispatchToPropsObject.addToAddressBookIfNew(
'mockNewAddress',
'mockToAccounts',
'mockNickname',
);
expect(dispatchSpy.calledOnce).toStrictEqual(true);
expect(addressIsNew).toHaveBeenCalledWith(
'mockToAccounts',
'0xmockNewAddress',
);
expect(addToAddressBook).toHaveBeenCalledWith(
'0xmockNewAddress',
'mockNickname',
);
});
});
});
});