2018-05-07 14:03:20 +02:00
|
|
|
import React from 'react'
|
|
|
|
import assert from 'assert'
|
|
|
|
import proxyquire from 'proxyquire'
|
|
|
|
import { shallow } from 'enzyme'
|
|
|
|
import sinon from 'sinon'
|
|
|
|
|
|
|
|
import SendHeader from '../send-header/send-header.container'
|
|
|
|
import SendContent from '../send-content/send-content.component'
|
|
|
|
import SendFooter from '../send-footer/send-footer.container'
|
|
|
|
|
|
|
|
const propsMethodSpies = {
|
|
|
|
updateAndSetGasTotal: sinon.spy(),
|
|
|
|
updateSendErrors: sinon.spy(),
|
|
|
|
updateSendTokenBalance: sinon.spy(),
|
2018-06-29 19:19:40 +02:00
|
|
|
resetSendState: sinon.spy(),
|
2018-05-07 14:03:20 +02:00
|
|
|
}
|
|
|
|
const utilsMethodStubs = {
|
|
|
|
getAmountErrorObject: sinon.stub().returns({ amount: 'mockAmountError' }),
|
2018-06-29 19:19:40 +02:00
|
|
|
getGasFeeErrorObject: sinon.stub().returns({ gasFee: 'mockGasFeeError' }),
|
2018-05-07 14:03:20 +02:00
|
|
|
doesAmountErrorRequireUpdate: sinon.stub().callsFake(obj => obj.balance !== obj.prevBalance),
|
|
|
|
}
|
|
|
|
|
|
|
|
const SendTransactionScreen = proxyquire('../send.component.js', {
|
|
|
|
'./send.utils': utilsMethodStubs,
|
|
|
|
}).default
|
|
|
|
|
|
|
|
sinon.spy(SendTransactionScreen.prototype, 'componentDidMount')
|
|
|
|
sinon.spy(SendTransactionScreen.prototype, 'updateGas')
|
|
|
|
|
2018-05-31 20:01:50 +02:00
|
|
|
describe('Send Component', function () {
|
2018-05-07 14:03:20 +02:00
|
|
|
let wrapper
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = shallow(<SendTransactionScreen
|
|
|
|
amount={'mockAmount'}
|
|
|
|
amountConversionRate={'mockAmountConversionRate'}
|
2018-05-22 17:16:53 +02:00
|
|
|
blockGasLimit={'mockBlockGasLimit'}
|
2018-05-07 14:03:20 +02:00
|
|
|
conversionRate={10}
|
|
|
|
editingTransactionId={'mockEditingTransactionId'}
|
|
|
|
from={ { address: 'mockAddress', balance: 'mockBalance' } }
|
|
|
|
gasLimit={'mockGasLimit'}
|
|
|
|
gasPrice={'mockGasPrice'}
|
|
|
|
gasTotal={'mockGasTotal'}
|
|
|
|
history={{ mockProp: 'history-abc'}}
|
|
|
|
network={'3'}
|
|
|
|
primaryCurrency={'mockPrimaryCurrency'}
|
2018-05-22 10:10:06 +02:00
|
|
|
recentBlocks={['mockBlock']}
|
2018-05-07 14:03:20 +02:00
|
|
|
selectedAddress={'mockSelectedAddress'}
|
|
|
|
selectedToken={'mockSelectedToken'}
|
2018-08-16 09:43:13 +02:00
|
|
|
showHexData={true}
|
2018-05-07 14:03:20 +02:00
|
|
|
tokenBalance={'mockTokenBalance'}
|
|
|
|
tokenContract={'mockTokenContract'}
|
|
|
|
updateAndSetGasTotal={propsMethodSpies.updateAndSetGasTotal}
|
|
|
|
updateSendErrors={propsMethodSpies.updateSendErrors}
|
|
|
|
updateSendTokenBalance={propsMethodSpies.updateSendTokenBalance}
|
2018-06-29 19:19:40 +02:00
|
|
|
resetSendState={propsMethodSpies.resetSendState}
|
2018-05-07 14:03:20 +02:00
|
|
|
/>)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
SendTransactionScreen.prototype.componentDidMount.resetHistory()
|
|
|
|
SendTransactionScreen.prototype.updateGas.resetHistory()
|
|
|
|
utilsMethodStubs.doesAmountErrorRequireUpdate.resetHistory()
|
|
|
|
utilsMethodStubs.getAmountErrorObject.resetHistory()
|
2018-06-29 19:19:40 +02:00
|
|
|
utilsMethodStubs.getGasFeeErrorObject.resetHistory()
|
2018-05-07 14:03:20 +02:00
|
|
|
propsMethodSpies.updateAndSetGasTotal.resetHistory()
|
|
|
|
propsMethodSpies.updateSendErrors.resetHistory()
|
|
|
|
propsMethodSpies.updateSendTokenBalance.resetHistory()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call componentDidMount', () => {
|
|
|
|
assert(SendTransactionScreen.prototype.componentDidMount.calledOnce)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('componentWillMount', () => {
|
|
|
|
it('should call this.updateGas', () => {
|
2018-05-30 18:40:30 +02:00
|
|
|
SendTransactionScreen.prototype.updateGas.resetHistory()
|
|
|
|
propsMethodSpies.updateSendErrors.resetHistory()
|
|
|
|
assert.equal(SendTransactionScreen.prototype.updateGas.callCount, 0)
|
2018-05-07 14:03:20 +02:00
|
|
|
wrapper.instance().componentWillMount()
|
2018-05-30 18:40:30 +02:00
|
|
|
assert.equal(SendTransactionScreen.prototype.updateGas.callCount, 1)
|
2018-05-07 14:03:20 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-06-29 19:19:40 +02:00
|
|
|
describe('componentWillUnmount', () => {
|
|
|
|
it('should call this.props.resetSendState', () => {
|
|
|
|
propsMethodSpies.resetSendState.resetHistory()
|
|
|
|
assert.equal(propsMethodSpies.resetSendState.callCount, 0)
|
|
|
|
wrapper.instance().componentWillUnmount()
|
|
|
|
assert.equal(propsMethodSpies.resetSendState.callCount, 1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-05-07 14:03:20 +02:00
|
|
|
describe('componentDidUpdate', () => {
|
|
|
|
it('should call doesAmountErrorRequireUpdate with the expected params', () => {
|
2018-05-30 18:40:30 +02:00
|
|
|
utilsMethodStubs.getAmountErrorObject.resetHistory()
|
2018-05-07 14:03:20 +02:00
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: '',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert(utilsMethodStubs.doesAmountErrorRequireUpdate.calledOnce)
|
|
|
|
assert.deepEqual(
|
|
|
|
utilsMethodStubs.doesAmountErrorRequireUpdate.getCall(0).args[0],
|
|
|
|
{
|
|
|
|
balance: 'mockBalance',
|
|
|
|
gasTotal: 'mockGasTotal',
|
|
|
|
prevBalance: '',
|
|
|
|
prevGasTotal: undefined,
|
|
|
|
prevTokenBalance: undefined,
|
|
|
|
selectedToken: 'mockSelectedToken',
|
|
|
|
tokenBalance: 'mockTokenBalance',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call getAmountErrorObject if doesAmountErrorRequireUpdate returns false', () => {
|
2018-05-30 18:40:30 +02:00
|
|
|
utilsMethodStubs.getAmountErrorObject.resetHistory()
|
2018-05-07 14:03:20 +02:00
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: 'mockBalance',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.equal(utilsMethodStubs.getAmountErrorObject.callCount, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call getAmountErrorObject if doesAmountErrorRequireUpdate returns true', () => {
|
2018-05-30 18:40:30 +02:00
|
|
|
utilsMethodStubs.getAmountErrorObject.resetHistory()
|
2018-05-07 14:03:20 +02:00
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: 'balanceChanged',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.equal(utilsMethodStubs.getAmountErrorObject.callCount, 1)
|
|
|
|
assert.deepEqual(
|
|
|
|
utilsMethodStubs.getAmountErrorObject.getCall(0).args[0],
|
|
|
|
{
|
|
|
|
amount: 'mockAmount',
|
|
|
|
amountConversionRate: 'mockAmountConversionRate',
|
|
|
|
balance: 'mockBalance',
|
|
|
|
conversionRate: 10,
|
|
|
|
gasTotal: 'mockGasTotal',
|
|
|
|
primaryCurrency: 'mockPrimaryCurrency',
|
|
|
|
selectedToken: 'mockSelectedToken',
|
|
|
|
tokenBalance: 'mockTokenBalance',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2018-06-29 19:19:40 +02:00
|
|
|
it('should call getGasFeeErrorObject if doesAmountErrorRequireUpdate returns true and selectedToken is truthy', () => {
|
|
|
|
utilsMethodStubs.getGasFeeErrorObject.resetHistory()
|
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: 'balanceChanged',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.equal(utilsMethodStubs.getGasFeeErrorObject.callCount, 1)
|
|
|
|
assert.deepEqual(
|
|
|
|
utilsMethodStubs.getGasFeeErrorObject.getCall(0).args[0],
|
|
|
|
{
|
|
|
|
amount: 'mockAmount',
|
|
|
|
amountConversionRate: 'mockAmountConversionRate',
|
|
|
|
balance: 'mockBalance',
|
|
|
|
conversionRate: 10,
|
|
|
|
gasTotal: 'mockGasTotal',
|
|
|
|
primaryCurrency: 'mockPrimaryCurrency',
|
|
|
|
selectedToken: 'mockSelectedToken',
|
|
|
|
tokenBalance: 'mockTokenBalance',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call getGasFeeErrorObject if doesAmountErrorRequireUpdate returns false', () => {
|
|
|
|
utilsMethodStubs.getGasFeeErrorObject.resetHistory()
|
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: { address: 'mockAddress', balance: 'mockBalance' },
|
|
|
|
})
|
|
|
|
assert.equal(utilsMethodStubs.getGasFeeErrorObject.callCount, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call getGasFeeErrorObject if doesAmountErrorRequireUpdate returns true but selectedToken is falsy', () => {
|
|
|
|
utilsMethodStubs.getGasFeeErrorObject.resetHistory()
|
|
|
|
wrapper.setProps({ selectedToken: null })
|
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: 'balanceChanged',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.equal(utilsMethodStubs.getGasFeeErrorObject.callCount, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call updateSendErrors with the expected params if selectedToken is falsy', () => {
|
|
|
|
propsMethodSpies.updateSendErrors.resetHistory()
|
|
|
|
wrapper.setProps({ selectedToken: null })
|
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: 'balanceChanged',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.equal(propsMethodSpies.updateSendErrors.callCount, 1)
|
|
|
|
assert.deepEqual(
|
|
|
|
propsMethodSpies.updateSendErrors.getCall(0).args[0],
|
|
|
|
{ amount: 'mockAmountError', gasFee: null }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call updateSendErrors with the expected params if selectedToken is truthy', () => {
|
2018-05-30 18:40:30 +02:00
|
|
|
propsMethodSpies.updateSendErrors.resetHistory()
|
2018-06-29 19:19:40 +02:00
|
|
|
wrapper.setProps({ selectedToken: 'someToken' })
|
2018-05-07 14:03:20 +02:00
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: 'balanceChanged',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.equal(propsMethodSpies.updateSendErrors.callCount, 1)
|
|
|
|
assert.deepEqual(
|
|
|
|
propsMethodSpies.updateSendErrors.getCall(0).args[0],
|
2018-06-29 19:19:40 +02:00
|
|
|
{ amount: 'mockAmountError', gasFee: 'mockGasFeeError' }
|
2018-05-07 14:03:20 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call updateSendTokenBalance or this.updateGas if network === prevNetwork', () => {
|
|
|
|
SendTransactionScreen.prototype.updateGas.resetHistory()
|
2018-05-25 03:00:07 +02:00
|
|
|
propsMethodSpies.updateSendTokenBalance.resetHistory()
|
2018-05-07 14:03:20 +02:00
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: 'balanceChanged',
|
|
|
|
},
|
|
|
|
network: '3',
|
|
|
|
})
|
|
|
|
assert.equal(propsMethodSpies.updateSendTokenBalance.callCount, 0)
|
|
|
|
assert.equal(SendTransactionScreen.prototype.updateGas.callCount, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call updateSendTokenBalance or this.updateGas if network === loading', () => {
|
|
|
|
wrapper.setProps({ network: 'loading' })
|
|
|
|
SendTransactionScreen.prototype.updateGas.resetHistory()
|
|
|
|
propsMethodSpies.updateSendTokenBalance.resetHistory()
|
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: 'balanceChanged',
|
|
|
|
},
|
|
|
|
network: '3',
|
|
|
|
})
|
|
|
|
assert.equal(propsMethodSpies.updateSendTokenBalance.callCount, 0)
|
|
|
|
assert.equal(SendTransactionScreen.prototype.updateGas.callCount, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call updateSendTokenBalance and this.updateGas with the correct params', () => {
|
|
|
|
SendTransactionScreen.prototype.updateGas.resetHistory()
|
2018-05-25 03:00:07 +02:00
|
|
|
propsMethodSpies.updateSendTokenBalance.resetHistory()
|
2018-05-07 14:03:20 +02:00
|
|
|
wrapper.instance().componentDidUpdate({
|
|
|
|
from: {
|
|
|
|
balance: 'balanceChanged',
|
|
|
|
},
|
|
|
|
network: '2',
|
|
|
|
})
|
|
|
|
assert.equal(propsMethodSpies.updateSendTokenBalance.callCount, 1)
|
|
|
|
assert.deepEqual(
|
|
|
|
propsMethodSpies.updateSendTokenBalance.getCall(0).args[0],
|
|
|
|
{
|
|
|
|
selectedToken: 'mockSelectedToken',
|
|
|
|
tokenContract: 'mockTokenContract',
|
|
|
|
address: 'mockAddress',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert.equal(SendTransactionScreen.prototype.updateGas.callCount, 1)
|
|
|
|
assert.deepEqual(
|
|
|
|
SendTransactionScreen.prototype.updateGas.getCall(0).args,
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('updateGas', () => {
|
2018-06-18 18:37:01 +02:00
|
|
|
it('should call updateAndSetGasTotal with the correct params if no to prop is passed', () => {
|
2018-05-07 14:03:20 +02:00
|
|
|
propsMethodSpies.updateAndSetGasTotal.resetHistory()
|
|
|
|
wrapper.instance().updateGas()
|
|
|
|
assert.equal(propsMethodSpies.updateAndSetGasTotal.callCount, 1)
|
|
|
|
assert.deepEqual(
|
|
|
|
propsMethodSpies.updateAndSetGasTotal.getCall(0).args[0],
|
|
|
|
{
|
2018-05-22 17:16:53 +02:00
|
|
|
blockGasLimit: 'mockBlockGasLimit',
|
2018-05-07 14:03:20 +02:00
|
|
|
editingTransactionId: 'mockEditingTransactionId',
|
|
|
|
gasLimit: 'mockGasLimit',
|
|
|
|
gasPrice: 'mockGasPrice',
|
2018-05-22 10:10:06 +02:00
|
|
|
recentBlocks: ['mockBlock'],
|
2018-05-07 14:03:20 +02:00
|
|
|
selectedAddress: 'mockSelectedAddress',
|
|
|
|
selectedToken: 'mockSelectedToken',
|
2018-06-18 18:37:01 +02:00
|
|
|
to: '',
|
2018-05-23 18:43:25 +02:00
|
|
|
value: 'mockAmount',
|
2018-09-27 15:46:24 +02:00
|
|
|
data: undefined,
|
2018-05-07 14:03:20 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2018-05-23 18:43:25 +02:00
|
|
|
|
2018-06-18 18:37:01 +02:00
|
|
|
it('should call updateAndSetGasTotal with the correct params if a to prop is passed', () => {
|
|
|
|
propsMethodSpies.updateAndSetGasTotal.resetHistory()
|
|
|
|
wrapper.setProps({ to: 'someAddress' })
|
|
|
|
wrapper.instance().updateGas()
|
|
|
|
assert.equal(
|
|
|
|
propsMethodSpies.updateAndSetGasTotal.getCall(0).args[0].to,
|
|
|
|
'someaddress',
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2018-05-23 18:43:25 +02:00
|
|
|
it('should call updateAndSetGasTotal with to set to lowercase if passed', () => {
|
|
|
|
propsMethodSpies.updateAndSetGasTotal.resetHistory()
|
|
|
|
wrapper.instance().updateGas({ to: '0xABC' })
|
|
|
|
assert.equal(propsMethodSpies.updateAndSetGasTotal.getCall(0).args[0].to, '0xabc')
|
|
|
|
})
|
2018-05-07 14:03:20 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('render', () => {
|
|
|
|
it('should render a page-container class', () => {
|
|
|
|
assert.equal(wrapper.find('.page-container').length, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should render SendHeader, SendContent and SendFooter', () => {
|
|
|
|
assert.equal(wrapper.find(SendHeader).length, 1)
|
|
|
|
assert.equal(wrapper.find(SendContent).length, 1)
|
|
|
|
assert.equal(wrapper.find(SendFooter).length, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should pass the history prop to SendHeader and SendFooter', () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
wrapper.find(SendFooter).props(),
|
|
|
|
{
|
|
|
|
history: { mockProp: 'history-abc' },
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2018-08-16 09:43:13 +02:00
|
|
|
|
|
|
|
it('should pass showHexData to SendContent', () => {
|
|
|
|
assert.equal(wrapper.find(SendContent).props().showHexData, true)
|
|
|
|
})
|
2018-05-07 14:03:20 +02:00
|
|
|
})
|
|
|
|
})
|