aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/gas-customization/gas-modal-page-container/advanced-tab-content/tests/advanced-tab-content-component.test.js
blob: 2500ee26733b73676b7fea6dd978b42ce43e1605 (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import React from 'react'
import assert from 'assert'
import shallow from '../../../../../../lib/shallow-with-context'
import sinon from 'sinon'
import AdvancedTabContent from '../advanced-tab-content.component.js'

import GasPriceChart from '../../../gas-price-chart'
import Loading from '../../../../loading-screen'

const propsMethodSpies = {
  updateCustomGasPrice: sinon.spy(),
  updateCustomGasLimit: sinon.spy(),
}

sinon.spy(AdvancedTabContent.prototype, 'renderGasEditRow')
sinon.spy(AdvancedTabContent.prototype, 'gasInput')
sinon.spy(AdvancedTabContent.prototype, 'renderGasEditRows')
sinon.spy(AdvancedTabContent.prototype, 'renderDataSummary')
sinon.spy(AdvancedTabContent.prototype, 'gasInputError')

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

  beforeEach(() => {
    wrapper = shallow(<AdvancedTabContent
      updateCustomGasPrice={propsMethodSpies.updateCustomGasPrice}
      updateCustomGasLimit={propsMethodSpies.updateCustomGasLimit}
      customGasPrice={11}
      customGasLimit={23456}
      timeRemaining={21500}
      transactionFee={'$0.25'}
      insufficientBalance={false}
      customPriceIsSafe={true}
      isSpeedUp={false}
    />, { context: { t: (str1, str2) => str2 ? str1 + str2 : str1 } })
  })

  afterEach(() => {
    propsMethodSpies.updateCustomGasPrice.resetHistory()
    propsMethodSpies.updateCustomGasLimit.resetHistory()
    AdvancedTabContent.prototype.renderGasEditRow.resetHistory()
    AdvancedTabContent.prototype.gasInput.resetHistory()
    AdvancedTabContent.prototype.renderGasEditRows.resetHistory()
    AdvancedTabContent.prototype.renderDataSummary.resetHistory()
  })

  describe('render()', () => {
    it('should render the advanced-tab root node', () => {
      assert(wrapper.hasClass('advanced-tab'))
    })

    it('should render the expected four children of the advanced-tab div', () => {
      const advancedTabChildren = wrapper.children()
      assert.equal(advancedTabChildren.length, 2)

      assert(advancedTabChildren.at(0).hasClass('advanced-tab__transaction-data-summary'))
      assert(advancedTabChildren.at(1).hasClass('advanced-tab__fee-chart'))

      const feeChartDiv = advancedTabChildren.at(1)

      assert(feeChartDiv.childAt(0).hasClass('advanced-tab__gas-edit-rows'))
      assert(feeChartDiv.childAt(1).hasClass('advanced-tab__fee-chart__title'))
      assert(feeChartDiv.childAt(2).is(GasPriceChart))
      assert(feeChartDiv.childAt(3).hasClass('advanced-tab__fee-chart__speed-buttons'))
    })

    it('should render a loading component instead of the chart if gasEstimatesLoading is true', () => {
      wrapper.setProps({ gasEstimatesLoading: true })
      const advancedTabChildren = wrapper.children()
      assert.equal(advancedTabChildren.length, 2)

      assert(advancedTabChildren.at(0).hasClass('advanced-tab__transaction-data-summary'))
      assert(advancedTabChildren.at(1).hasClass('advanced-tab__fee-chart'))

      const feeChartDiv = advancedTabChildren.at(1)

      assert(feeChartDiv.childAt(0).hasClass('advanced-tab__gas-edit-rows'))
      assert(feeChartDiv.childAt(1).hasClass('advanced-tab__fee-chart__title'))
      assert(feeChartDiv.childAt(2).is(Loading))
      assert(feeChartDiv.childAt(3).hasClass('advanced-tab__fee-chart__speed-buttons'))
    })

    it('should call renderDataSummary with the expected params', () => {
      assert.equal(AdvancedTabContent.prototype.renderGasEditRows.callCount, 1)
      const renderDataSummaryArgs = AdvancedTabContent.prototype.renderDataSummary.getCall(0).args
      assert.deepEqual(renderDataSummaryArgs, ['$0.25', 21500])
    })

    it('should call renderGasEditRows with the expected params', () => {
      assert.equal(AdvancedTabContent.prototype.renderGasEditRows.callCount, 1)
      const renderGasEditRowArgs = AdvancedTabContent.prototype.renderGasEditRows.getCall(0).args
      assert.deepEqual(renderGasEditRowArgs, [{
        customGasPrice: 11,
        customGasLimit: 23456,
        insufficientBalance: false,
        customPriceIsSafe: true,
        updateCustomGasPrice: propsMethodSpies.updateCustomGasPrice,
        updateCustomGasLimit: propsMethodSpies.updateCustomGasLimit,
        isSpeedUp: false,
      }])
    })
  })

  describe('renderDataSummary()', () => {
    let dataSummary

    beforeEach(() => {
      dataSummary = shallow(wrapper.instance().renderDataSummary('mockTotalFee', 'mockMsRemaining'))
    })

    it('should render the transaction-data-summary root node', () => {
      assert(dataSummary.hasClass('advanced-tab__transaction-data-summary'))
    })

    it('should render titles of the data', () => {
      const titlesNode = dataSummary.children().at(0)
      assert(titlesNode.hasClass('advanced-tab__transaction-data-summary__titles'))
      assert.equal(titlesNode.children().at(0).text(), 'newTransactionFee')
      assert.equal(titlesNode.children().at(1).text(), '~transactionTime')
    })

    it('should render the data', () => {
      const dataNode = dataSummary.children().at(1)
      assert(dataNode.hasClass('advanced-tab__transaction-data-summary__container'))
      assert.equal(dataNode.children().at(0).text(), 'mockTotalFee')
      assert(dataNode.children().at(1).hasClass('time-remaining'))
      assert.equal(dataNode.children().at(1).text(), 'mockMsRemaining')
    })
  })

  describe('renderGasEditRow()', () => {
    let gasEditRow

    beforeEach(() => {
      AdvancedTabContent.prototype.gasInput.resetHistory()
      gasEditRow = shallow(wrapper.instance().renderGasEditRow({
        labelKey: 'mockLabelKey',
        someArg: 'argA',
      }))
    })

    it('should render the gas-edit-row root node', () => {
      assert(gasEditRow.hasClass('advanced-tab__gas-edit-row'))
    })

    it('should render a label and an input', () => {
      const gasEditRowChildren = gasEditRow.children()
      assert.equal(gasEditRowChildren.length, 2)
      assert(gasEditRowChildren.at(0).hasClass('advanced-tab__gas-edit-row__label'))
      assert(gasEditRowChildren.at(1).hasClass('advanced-tab__gas-edit-row__input-wrapper'))
    })

    it('should render the label key and info button', () => {
      const gasRowLabelChildren = gasEditRow.children().at(0).children()
      assert.equal(gasRowLabelChildren.length, 2)
      assert(gasRowLabelChildren.at(0), 'mockLabelKey')
      assert(gasRowLabelChildren.at(1).hasClass('fa-info-circle'))
    })

    it('should call this.gasInput with the correct args', () => {
      const gasInputSpyArgs = AdvancedTabContent.prototype.gasInput.args
      assert.deepEqual(gasInputSpyArgs[0], [ { labelKey: 'mockLabelKey', someArg: 'argA' } ])
    })
  })

  describe('renderGasEditRows()', () => {
    let gasEditRows
    let tempOnChangeGasLimit

    beforeEach(() => {
      tempOnChangeGasLimit = wrapper.instance().onChangeGasLimit
      wrapper.instance().onChangeGasLimit = () => 'mockOnChangeGasLimit'
      AdvancedTabContent.prototype.renderGasEditRow.resetHistory()
      gasEditRows = shallow(wrapper.instance().renderGasEditRows(
        'mockGasPrice',
        () => 'mockUpdateCustomGasPriceReturn',
        'mockGasLimit',
        () => 'mockUpdateCustomGasLimitReturn',
        false
      ))
    })

    afterEach(() => {
      wrapper.instance().onChangeGasLimit = tempOnChangeGasLimit
    })

    it('should render the gas-edit-rows root node', () => {
      assert(gasEditRows.hasClass('advanced-tab__gas-edit-rows'))
    })

    it('should render two rows', () => {
      const gasEditRowsChildren = gasEditRows.children()
      assert.equal(gasEditRowsChildren.length, 2)
      assert(gasEditRowsChildren.at(0).hasClass('advanced-tab__gas-edit-row'))
      assert(gasEditRowsChildren.at(1).hasClass('advanced-tab__gas-edit-row'))
    })

    it('should call this.renderGasEditRow twice, with the expected args', () => {
      const renderGasEditRowSpyArgs = AdvancedTabContent.prototype.renderGasEditRow.args
      assert.equal(renderGasEditRowSpyArgs.length, 2)
      assert.deepEqual(renderGasEditRowSpyArgs[0].map(String), [{
        labelKey: 'gasPrice',
        value: 'mockGasLimit',
        onChange: () => 'mockOnChangeGasLimit',
        insufficientBalance: false,
        customPriceIsSafe: true,
        showGWEI: true,
      }].map(String))
      assert.deepEqual(renderGasEditRowSpyArgs[1].map(String), [{
        labelKey: 'gasPrice',
        value: 'mockGasPrice',
        onChange: () => 'mockUpdateCustomGasPriceReturn',
        insufficientBalance: false,
        customPriceIsSafe: true,
        showGWEI: true,
      }].map(String))
    })
  })

  describe('infoButton()', () => {
    let infoButton

    beforeEach(() => {
      AdvancedTabContent.prototype.renderGasEditRow.resetHistory()
      infoButton = shallow(wrapper.instance().infoButton(() => 'mockOnClickReturn'))
    })

    it('should render the i element', () => {
      assert(infoButton.hasClass('fa-info-circle'))
    })

    it('should pass the onClick argument to the i tag onClick prop', () => {
      assert(infoButton.props().onClick(), 'mockOnClickReturn')
    })
  })

  describe('gasInput()', () => {
    let gasInput

    beforeEach(() => {
      AdvancedTabContent.prototype.renderGasEditRow.resetHistory()
      AdvancedTabContent.prototype.gasInputError.resetHistory()
      gasInput = shallow(wrapper.instance().gasInput({
        labelKey: 'gasPrice',
        value: 321,
        onChange: value => value + 7,
        insufficientBalance: false,
        showGWEI: true,
        customPriceIsSafe: true,
        isSpeedUp: false,
      }))
    })

    it('should render the input-wrapper root node', () => {
      assert(gasInput.hasClass('advanced-tab__gas-edit-row__input-wrapper'))
    })

    it('should render two children, including an input', () => {
      assert.equal(gasInput.children().length, 2)
      assert(gasInput.children().at(0).hasClass('advanced-tab__gas-edit-row__input'))
    })

    it('should call the passed onChange method with the value of the input onChange event', () => {
      const inputOnChange = gasInput.find('input').props().onChange
      assert.equal(inputOnChange({ target: { value: 8} }), 15)
    })

    it('should have two input arrows', () => {
      const upArrow = gasInput.find('.fa-angle-up')
      assert.equal(upArrow.length, 1)
      const downArrow = gasInput.find('.fa-angle-down')
      assert.equal(downArrow.length, 1)
    })

    it('should call onChange with the value incremented decremented when its onchange method is called', () => {
      const upArrow = gasInput.find('.advanced-tab__gas-edit-row__input-arrows__i-wrap').at(0)
      assert.equal(upArrow.props().onClick(), 329)
      const downArrow = gasInput.find('.advanced-tab__gas-edit-row__input-arrows__i-wrap').at(1)
      assert.equal(downArrow.props().onClick(), 327)
    })

    it('should call gasInputError with the expected params', () => {
      assert.equal(AdvancedTabContent.prototype.gasInputError.callCount, 1)
      const gasInputErrorArgs = AdvancedTabContent.prototype.gasInputError.getCall(0).args
      assert.deepEqual(gasInputErrorArgs, [{
        labelKey: 'gasPrice',
        insufficientBalance: false,
        customPriceIsSafe: true,
        value: 321,
        isSpeedUp: false,
      }])
    })
  })

  describe('gasInputError()', () => {
    let gasInputError

    beforeEach(() => {
      AdvancedTabContent.prototype.renderGasEditRow.resetHistory()
      gasInputError = wrapper.instance().gasInputError({
        labelKey: '',
        insufficientBalance: false,
        customPriceIsSafe: true,
        isSpeedUp: false,
      })
    })

    it('should return an insufficientBalance error', () => {
      const gasInputError = wrapper.instance().gasInputError({
        labelKey: 'gasPrice',
        insufficientBalance: true,
        customPriceIsSafe: true,
        isSpeedUp: false,
        value: 1,
      })
      assert.deepEqual(gasInputError, {
        isInError: true,
        errorText: 'insufficientBalance',
        errorType: 'error',
      })
    })

    it('should return a zero gas on retry error', () => {
      const gasInputError = wrapper.instance().gasInputError({
        labelKey: 'gasPrice',
        insufficientBalance: false,
        customPriceIsSafe: false,
        isSpeedUp: true,
        value: 0,
      })
      assert.deepEqual(gasInputError, {
        isInError: true,
        errorText: 'zeroGasPriceOnSpeedUpError',
        errorType: 'error',
      })
    })

    it('should return a low gas warning', () => {
      const gasInputError = wrapper.instance().gasInputError({
        labelKey: 'gasPrice',
        insufficientBalance: false,
        customPriceIsSafe: false,
        isSpeedUp: false,
        value: 1,
      })
      assert.deepEqual(gasInputError, {
        isInError: true,
        errorText: 'gasPriceExtremelyLow',
        errorType: 'warning',
      })
    })

    it('should return isInError false if there is no error', () => {
      gasInputError = wrapper.instance().gasInputError({
        labelKey: 'gasPrice',
        insufficientBalance: false,
        customPriceIsSafe: true,
        value: 1,
      })
      assert.equal(gasInputError.isInError, false)
    })
  })

})