aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/gas-customization/gas-price-chart/tests/gas-price-chart.component.test.js
blob: 74eddae421d8fbbf406330f84d8152864ff3ba3b (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
import React from 'react'
import assert from 'assert'
import proxyquire from 'proxyquire'
import sinon from 'sinon'
import shallow from '../../../../../lib/shallow-with-context'
import * as d3 from 'd3'

function timeout (time) {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, time)
  })
}

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

const selectReturnSpies = {
  empty: sinon.spy(),
  remove: sinon.spy(),
  style: sinon.spy(),
  select: d3.select,
  attr: sinon.spy(),
  on: sinon.spy(),
  datum: sinon.stub().returns({ x: 'mockX' }),
}

const mockSelectReturn = {
  ...d3.select('div'),
  node: () => ({
    getBoundingClientRect: () => ({ x: 123, y: 321, width: 400 }),
  }),
  ...selectReturnSpies,
}

const gasPriceChartUtilsSpies = {
  appendOrUpdateCircle: sinon.spy(),
  generateChart: sinon.stub().returns({ mockChart: true }),
  generateDataUIObj: sinon.spy(),
  getAdjacentGasPrices: sinon.spy(),
  getCoordinateData: sinon.stub().returns({ x: 'mockCoordinateX', width: 'mockWidth' }),
  getNewXandTimeEstimate: sinon.spy(),
  handleChartUpdate: sinon.spy(),
  hideDataUI: sinon.spy(),
  setSelectedCircle: sinon.spy(),
  setTickPosition: sinon.spy(),
  handleMouseMove: sinon.spy(),
}

const testProps = {
  gasPrices: [1.5, 2.5, 4, 8],
  estimatedTimes: [100, 80, 40, 10],
  gasPricesMax: 9,
  estimatedTimesMax: '100',
  currentPrice: 6,
  updateCustomGasPrice: propsMethodSpies.updateCustomGasPrice,
}

const GasPriceChart = proxyquire('../gas-price-chart.component.js', {
  './gas-price-chart.utils.js': gasPriceChartUtilsSpies,
  'd3': {
    ...d3,
    select: function (...args) {
      const result = d3.select(...args)
      return result.empty()
        ? mockSelectReturn
        : result
    },
    event: {
      clientX: 'mockClientX',
    },
  },
}).default

sinon.spy(GasPriceChart.prototype, 'renderChart')

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

  beforeEach(() => {
    wrapper = shallow(<GasPriceChart {...testProps} />)
  })

  describe('render()', () => {
    it('should render', () => {
      assert(wrapper.hasClass('gas-price-chart'))
    })

    it('should render the chart div', () => {
      assert(wrapper.childAt(0).hasClass('gas-price-chart__root'))
      assert.equal(wrapper.childAt(0).props().id, 'chart')
    })
  })

  describe('componentDidMount', () => {
    it('should call this.renderChart with the components props', () => {
      assert(GasPriceChart.prototype.renderChart.callCount, 1)
      wrapper.instance().componentDidMount()
      assert(GasPriceChart.prototype.renderChart.callCount, 2)
      assert.deepEqual(GasPriceChart.prototype.renderChart.getCall(1).args, [{...testProps}])
    })
  })

  describe('componentDidUpdate', () => {
    it('should call handleChartUpdate if props.currentPrice has changed', () => {
      gasPriceChartUtilsSpies.handleChartUpdate.resetHistory()
      wrapper.instance().componentDidUpdate({ currentPrice: 7 })
      assert.equal(gasPriceChartUtilsSpies.handleChartUpdate.callCount, 1)
    })

    it('should call handleChartUpdate with the correct props', () => {
      gasPriceChartUtilsSpies.handleChartUpdate.resetHistory()
      wrapper.instance().componentDidUpdate({ currentPrice: 7 })
      assert.deepEqual(gasPriceChartUtilsSpies.handleChartUpdate.getCall(0).args, [{
        chart: { mockChart: true },
        gasPrices: [1.5, 2.5, 4, 8],
        newPrice: 6,
        cssId: '#set-circle',
      }])
    })

    it('should not call handleChartUpdate if props.currentPrice has not changed', () => {
      gasPriceChartUtilsSpies.handleChartUpdate.resetHistory()
      wrapper.instance().componentDidUpdate({ currentPrice: 6 })
      assert.equal(gasPriceChartUtilsSpies.handleChartUpdate.callCount, 0)
    })
  })

  describe('renderChart', () => {
    it('should call setTickPosition 4 times, with the expected props', async () => {
      await timeout(0)
      gasPriceChartUtilsSpies.setTickPosition.resetHistory()
      assert.equal(gasPriceChartUtilsSpies.setTickPosition.callCount, 0)
      wrapper.instance().renderChart(testProps)
      await timeout(0)
      assert.equal(gasPriceChartUtilsSpies.setTickPosition.callCount, 4)
      assert.deepEqual(gasPriceChartUtilsSpies.setTickPosition.getCall(0).args, ['y', 0, -5, 8])
      assert.deepEqual(gasPriceChartUtilsSpies.setTickPosition.getCall(1).args, ['y', 1, -3, -5])
      assert.deepEqual(gasPriceChartUtilsSpies.setTickPosition.getCall(2).args, ['x', 0, 3])
      assert.deepEqual(gasPriceChartUtilsSpies.setTickPosition.getCall(3).args, ['x', 1, 3, -8])
    })

    it('should call handleChartUpdate with the correct props', async () => {
      await timeout(0)
      gasPriceChartUtilsSpies.handleChartUpdate.resetHistory()
      wrapper.instance().renderChart(testProps)
      await timeout(0)
      assert.deepEqual(gasPriceChartUtilsSpies.handleChartUpdate.getCall(0).args, [{
        chart: { mockChart: true },
        gasPrices: [1.5, 2.5, 4, 8],
        newPrice: 6,
        cssId: '#set-circle',
      }])
    })

    it('should add three events to the chart', async () => {
      await timeout(0)
      selectReturnSpies.on.resetHistory()
      assert.equal(selectReturnSpies.on.callCount, 0)
      wrapper.instance().renderChart(testProps)
      await timeout(0)
      assert.equal(selectReturnSpies.on.callCount, 3)

      const firstOnEventArgs = selectReturnSpies.on.getCall(0).args
      assert.equal(firstOnEventArgs[0], 'mouseout')
      const secondOnEventArgs = selectReturnSpies.on.getCall(1).args
      assert.equal(secondOnEventArgs[0], 'click')
      const thirdOnEventArgs = selectReturnSpies.on.getCall(2).args
      assert.equal(thirdOnEventArgs[0], 'mousemove')
    })

    it('should hide the data UI on mouseout', async () => {
      await timeout(0)
      selectReturnSpies.on.resetHistory()
      wrapper.instance().renderChart(testProps)
      gasPriceChartUtilsSpies.hideDataUI.resetHistory()
      await timeout(0)
      const mouseoutEventArgs = selectReturnSpies.on.getCall(0).args
      assert.equal(gasPriceChartUtilsSpies.hideDataUI.callCount, 0)
      mouseoutEventArgs[1]()
      assert.equal(gasPriceChartUtilsSpies.hideDataUI.callCount, 1)
      assert.deepEqual(gasPriceChartUtilsSpies.hideDataUI.getCall(0).args, [{ mockChart: true }, '#overlayed-circle'])
    })

    it('should updateCustomGasPrice on click', async () => {
      await timeout(0)
      selectReturnSpies.on.resetHistory()
      wrapper.instance().renderChart(testProps)
      propsMethodSpies.updateCustomGasPrice.resetHistory()
      await timeout(0)
      const mouseoutEventArgs = selectReturnSpies.on.getCall(1).args
      assert.equal(propsMethodSpies.updateCustomGasPrice.callCount, 0)
      mouseoutEventArgs[1]()
      assert.equal(propsMethodSpies.updateCustomGasPrice.callCount, 1)
      assert.equal(propsMethodSpies.updateCustomGasPrice.getCall(0).args[0], 'mockX')
    })

    it('should handle mousemove', async () => {
      await timeout(0)
      selectReturnSpies.on.resetHistory()
      wrapper.instance().renderChart(testProps)
      gasPriceChartUtilsSpies.handleMouseMove.resetHistory()
      await timeout(0)
      const mouseoutEventArgs = selectReturnSpies.on.getCall(2).args
      assert.equal(gasPriceChartUtilsSpies.handleMouseMove.callCount, 0)
      mouseoutEventArgs[1]()
      assert.equal(gasPriceChartUtilsSpies.handleMouseMove.callCount, 1)
      assert.deepEqual(gasPriceChartUtilsSpies.handleMouseMove.getCall(0).args, [{
        xMousePos: 'mockClientX',
        chartXStart: 'mockCoordinateX',
        chartWidth: 'mockWidth',
        gasPrices: testProps.gasPrices,
        estimatedTimes: testProps.estimatedTimes,
        chart: { mockChart: true },
      }])
    })
  })
})