aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/send_/send-footer/tests/send-footer-component.test.js
blob: e071fe54f1ad5c692756d7b3c23ad910f8d20645 (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
import React from 'react'
import assert from 'assert'
import { shallow } from 'enzyme'
import sinon from 'sinon'
import { CONFIRM_TRANSACTION_ROUTE, DEFAULT_ROUTE } from '../../../../routes'
import SendFooter from '../send-footer.component.js'

import PageContainerFooter from '../../../page-container/page-container-footer'

const propsMethodSpies = {
  addToAddressBookIfNew: sinon.spy(),
  clearSend: sinon.spy(),
  sign: sinon.spy(),
  update: sinon.spy(),
}
const historySpies = {
  push: sinon.spy(),
}
const MOCK_EVENT = { preventDefault: () => {} }

sinon.spy(SendFooter.prototype, 'onCancel')
sinon.spy(SendFooter.prototype, 'onSubmit')

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

  beforeEach(() => {
    wrapper = shallow(<SendFooter
      addToAddressBookIfNew={propsMethodSpies.addToAddressBookIfNew}
      amount={'mockAmount'}
      clearSend={propsMethodSpies.clearSend}
      disabled={true}
      editingTransactionId={'mockEditingTransactionId'}
      errors={{}}
      from={ { address: 'mockAddress', balance: 'mockBalance' } }
      gasLimit={'mockGasLimit'}
      gasPrice={'mockGasPrice'}
      gasTotal={'mockGasTotal'}
      history={historySpies}
      inError={false}
      selectedToken={{ mockProp: 'mockSelectedTokenProp' }}
      sign={propsMethodSpies.sign}
      to={'mockTo'}
      toAccounts={['mockAccount']}
      tokenBalance={'mockTokenBalance'}
      unapprovedTxs={['mockTx']}
      update={propsMethodSpies.update}
    />, { context: { t: str => str } })
  })

  afterEach(() => {
    propsMethodSpies.clearSend.resetHistory()
    propsMethodSpies.addToAddressBookIfNew.resetHistory()
    propsMethodSpies.clearSend.resetHistory()
    propsMethodSpies.sign.resetHistory()
    propsMethodSpies.update.resetHistory()
    historySpies.push.resetHistory()
    SendFooter.prototype.onCancel.resetHistory()
    SendFooter.prototype.onSubmit.resetHistory()
  })

  describe('onCancel', () => {
    it('should call clearSend', () => {
      assert.equal(propsMethodSpies.clearSend.callCount, 0)
      wrapper.instance().onCancel()
      assert.equal(propsMethodSpies.clearSend.callCount, 1)
    })

    it('should call history.push', () => {
      assert.equal(historySpies.push.callCount, 0)
      wrapper.instance().onCancel()
      assert.equal(historySpies.push.callCount, 1)
      assert.equal(historySpies.push.getCall(0).args[0], DEFAULT_ROUTE)
    })
  })


  describe('formShouldBeDisabled()', () => {
    const config = {
      'should return true if inError is truthy': {
        inError: true,
        expectedResult: true,
      },
      'should return true if gasTotal is falsy': {
        inError: false,
        gasTotal: false,
        expectedResult: true,
      },
      'should return true if to is truthy': {
        to: '0xsomevalidAddress',
        inError: false,
        gasTotal: false,
        expectedResult: true,
      },
      'should return true if selectedToken is truthy and tokenBalance is falsy': {
        selectedToken: true,
        tokenBalance: null,
        expectedResult: true,
      },
      'should return false if inError is false and all other params are truthy': {
        inError: false,
        gasTotal: '0x123',
        selectedToken: true,
        tokenBalance: 123,
        expectedResult: false,
      },
    }
    Object.entries(config).map(([description, obj]) => {
      it(description, () => {
        wrapper.setProps(obj)
        assert.equal(wrapper.instance().formShouldBeDisabled(), obj.expectedResult)
      })
    })
  })

  describe('onSubmit', () => {
    it('should call addToAddressBookIfNew with the correct params', () => {
      wrapper.instance().onSubmit(MOCK_EVENT)
      assert(propsMethodSpies.addToAddressBookIfNew.calledOnce)
      assert.deepEqual(
        propsMethodSpies.addToAddressBookIfNew.getCall(0).args,
        ['mockTo', ['mockAccount']]
      )
    })

    it('should call props.update if editingTransactionId is truthy', () => {
      wrapper.instance().onSubmit(MOCK_EVENT)
      assert(propsMethodSpies.update.calledOnce)
      assert.deepEqual(
        propsMethodSpies.update.getCall(0).args[0],
        {
          amount: 'mockAmount',
          editingTransactionId: 'mockEditingTransactionId',
          from: 'mockAddress',
          gas: 'mockGasLimit',
          gasPrice: 'mockGasPrice',
          selectedToken: { mockProp: 'mockSelectedTokenProp' },
          to: 'mockTo',
          unapprovedTxs: ['mockTx'],
        }
      )
    })

    it('should not call props.sign if editingTransactionId is truthy', () => {
      assert.equal(propsMethodSpies.sign.callCount, 0)
    })

    it('should call props.sign if editingTransactionId is falsy', () => {
      wrapper.setProps({ editingTransactionId: null })
      wrapper.instance().onSubmit(MOCK_EVENT)
      assert(propsMethodSpies.sign.calledOnce)
      assert.deepEqual(
        propsMethodSpies.sign.getCall(0).args[0],
        {
          amount: 'mockAmount',
          from: 'mockAddress',
          gas: 'mockGasLimit',
          gasPrice: 'mockGasPrice',
          selectedToken: { mockProp: 'mockSelectedTokenProp' },
          to: 'mockTo',
        }
      )
    })

    it('should not call props.update if editingTransactionId is falsy', () => {
      assert.equal(propsMethodSpies.update.callCount, 0)
    })

    it('should call history.push', () => {
      wrapper.instance().onSubmit(MOCK_EVENT)
      assert.equal(historySpies.push.callCount, 1)
      assert.equal(historySpies.push.getCall(0).args[0], CONFIRM_TRANSACTION_ROUTE)
    })
  })

  describe('render', () => {
    beforeEach(() => {
      sinon.stub(SendFooter.prototype, 'formShouldBeDisabled').returns('formShouldBeDisabledReturn')
      wrapper = shallow(<SendFooter
        addToAddressBookIfNew={propsMethodSpies.addToAddressBookIfNew}
        amount={'mockAmount'}
        clearSend={propsMethodSpies.clearSend}
        disabled={true}
        editingTransactionId={'mockEditingTransactionId'}
        errors={{}}
        from={ { address: 'mockAddress', balance: 'mockBalance' } }
        gasLimit={'mockGasLimit'}
        gasPrice={'mockGasPrice'}
        gasTotal={'mockGasTotal'}
        history={historySpies}
        inError={false}
        selectedToken={{ mockProp: 'mockSelectedTokenProp' }}
        sign={propsMethodSpies.sign}
        to={'mockTo'}
        toAccounts={['mockAccount']}
        tokenBalance={'mockTokenBalance'}
        unapprovedTxs={['mockTx']}
        update={propsMethodSpies.update}
      />, { context: { t: str => str } })
    })

    afterEach(() => {
      SendFooter.prototype.formShouldBeDisabled.restore()
    })

    it('should render a PageContainerFooter component', () => {
      assert.equal(wrapper.find(PageContainerFooter).length, 1)
    })

    it('should pass the correct props to PageContainerFooter', () => {
      const {
        onCancel,
        onSubmit,
        disabled,
      } = wrapper.find(PageContainerFooter).props()
      assert.equal(disabled, 'formShouldBeDisabledReturn')

      assert.equal(SendFooter.prototype.onSubmit.callCount, 0)
      onSubmit(MOCK_EVENT)
      assert.equal(SendFooter.prototype.onSubmit.callCount, 1)

      assert.equal(SendFooter.prototype.onCancel.callCount, 0)
      onCancel()
      assert.equal(SendFooter.prototype.onCancel.callCount, 1)
    })
  })
})