aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/send/tests/send-component.test.js
blob: f4943e7076f3d99379bfaea0b825a438bb3a5df7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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(),
  resetSendState: sinon.spy(),
}
const utilsMethodStubs = {
  getAmountErrorObject: sinon.stub().returns({ amount: 'mockAmountError' }),
  getGasFeeErrorObject: sinon.stub().returns({ gasFee: 'mockGasFeeError' }),
  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')

describe('Send Component', function () {
  let wrapper

  beforeEach(() => {
    wrapper = shallow(<SendTransactionScreen
      amount={'mockAmount'}
      amountConversionRate={'mockAmountConversionRate'}
      blockGasLimit={'mockBlockGasLimit'}
      conversionRate={10}
      editingTransactionId={'mockEditingTransactionId'}
      from={ { address: 'mockAddress', balance: 'mockBalance' } }
      gasLimit={'mockGasLimit'}
      gasPrice={'mockGasPrice'}
      gasTotal={'mockGasTotal'}
      history={{ mockProp: 'history-abc'}}
      network={'3'}
      primaryCurrency={'mockPrimaryCurrency'}
      recentBlocks={['mockBlock']}
      selectedAddress={'mockSelectedAddress'}
      selectedToken={'mockSelectedToken'}
      showHexData={true}
      tokenBalance={'mockTokenBalance'}
      tokenContract={'mockTokenContract'}
      updateAndSetGasTotal={propsMethodSpies.updateAndSetGasTotal}
      updateSendErrors={propsMethodSpies.updateSendErrors}
      updateSendTokenBalance={propsMethodSpies.updateSendTokenBalance}
      resetSendState={propsMethodSpies.resetSendState}
    />)
  })

  afterEach(() => {
    SendTransactionScreen.prototype.componentDidMount.resetHistory()
    SendTransactionScreen.prototype.updateGas.resetHistory()
    utilsMethodStubs.doesAmountErrorRequireUpdate.resetHistory()
    utilsMethodStubs.getAmountErrorObject.resetHistory()
    utilsMethodStubs.getGasFeeErrorObject.resetHistory()
    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', () => {
      SendTransactionScreen.prototype.updateGas.resetHistory()
      propsMethodSpies.updateSendErrors.resetHistory()
      assert.equal(SendTransactionScreen.prototype.updateGas.callCount, 0)
      wrapper.instance().componentWillMount()
      assert.equal(SendTransactionScreen.prototype.updateGas.callCount, 1)
    })
  })

  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)
    })
  })

  describe('componentDidUpdate', () => {
    it('should call doesAmountErrorRequireUpdate with the expected params', () => {
      utilsMethodStubs.getAmountErrorObject.resetHistory()
      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', () => {
      utilsMethodStubs.getAmountErrorObject.resetHistory()
      wrapper.instance().componentDidUpdate({
        from: {
          balance: 'mockBalance',
        },
      })
      assert.equal(utilsMethodStubs.getAmountErrorObject.callCount, 0)
    })

    it('should call getAmountErrorObject if doesAmountErrorRequireUpdate returns true', () => {
      utilsMethodStubs.getAmountErrorObject.resetHistory()
      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',
        }
      )
    })

    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', () => {
      propsMethodSpies.updateSendErrors.resetHistory()
      wrapper.setProps({ selectedToken: 'someToken' })
      wrapper.instance().componentDidUpdate({
        from: {
          balance: 'balanceChanged',
        },
      })
      assert.equal(propsMethodSpies.updateSendErrors.callCount, 1)
      assert.deepEqual(
        propsMethodSpies.updateSendErrors.getCall(0).args[0],
        { amount: 'mockAmountError', gasFee: 'mockGasFeeError' }
      )
    })

    it('should not call updateSendTokenBalance or this.updateGas if network === prevNetwork', () => {
      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 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()
      propsMethodSpies.updateSendTokenBalance.resetHistory()
      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', () => {
    it('should call updateAndSetGasTotal with the correct params if no to prop is passed', () => {
      propsMethodSpies.updateAndSetGasTotal.resetHistory()
      wrapper.instance().updateGas()
      assert.equal(propsMethodSpies.updateAndSetGasTotal.callCount, 1)
      assert.deepEqual(
        propsMethodSpies.updateAndSetGasTotal.getCall(0).args[0],
        {
          blockGasLimit: 'mockBlockGasLimit',
          editingTransactionId: 'mockEditingTransactionId',
          gasLimit: 'mockGasLimit',
          gasPrice: 'mockGasPrice',
          recentBlocks: ['mockBlock'],
          selectedAddress: 'mockSelectedAddress',
          selectedToken: 'mockSelectedToken',
          to: '',
          value: 'mockAmount',
          data: undefined,
        }
      )
    })

    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',
      )
    })

    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')
    })
  })

  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' },
        }
      )
    })

    it('should pass showHexData to SendContent', () => {
      assert.equal(wrapper.find(SendContent).props().showHexData, true)
    })
  })
})