aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/send/tests/send-container.test.js
blob: 7a9120d2452d9bdb3f1502c04e0e3051843d496c (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
import assert from 'assert'
import proxyquire from 'proxyquire'
import sinon from 'sinon'

let mapStateToProps
let mapDispatchToProps

const actionSpies = {
  updateSendTokenBalance: sinon.spy(),
  updateGasData: sinon.spy(),
  setGasTotal: sinon.spy(),
}
const duckActionSpies = {
  updateSendErrors: sinon.spy(),
  resetSendState: sinon.spy(),
}

proxyquire('../send.container.js', {
  'react-redux': {
    connect: (ms, md) => {
      mapStateToProps = ms
      mapDispatchToProps = md
      return () => ({})
    },
  },
  'react-router-dom': { withRouter: () => {} },
  'recompose': { compose: (arg1, arg2) => () => arg2() },
  './send.selectors': {
    getAmountConversionRate: (s) => `mockAmountConversionRate:${s}`,
    getBlockGasLimit: (s) => `mockBlockGasLimit:${s}`,
    getConversionRate: (s) => `mockConversionRate:${s}`,
    getCurrentNetwork: (s) => `mockNetwork:${s}`,
    getGasLimit: (s) => `mockGasLimit:${s}`,
    getGasPrice: (s) => `mockGasPrice:${s}`,
    getGasTotal: (s) => `mockGasTotal:${s}`,
    getPrimaryCurrency: (s) => `mockPrimaryCurrency:${s}`,
    getRecentBlocks: (s) => `mockRecentBlocks:${s}`,
    getSelectedAddress: (s) => `mockSelectedAddress:${s}`,
    getSelectedToken: (s) => `mockSelectedToken:${s}`,
    getSelectedTokenContract: (s) => `mockTokenContract:${s}`,
    getSelectedTokenToFiatRate: (s) => `mockTokenToFiatRate:${s}`,
    getSendAmount: (s) => `mockAmount:${s}`,
    getSendTo: (s) => `mockTo:${s}`,
    getSendEditingTransactionId: (s) => `mockEditingTransactionId:${s}`,
    getSendFromObject: (s) => `mockFrom:${s}`,
    getTokenBalance: (s) => `mockTokenBalance:${s}`,
  },
  '../../actions': actionSpies,
  '../../ducks/send.duck': duckActionSpies,
  './send.utils.js': {
    calcGasTotal: (gasLimit, gasPrice) => gasLimit + gasPrice,
  },
})

describe('send container', () => {

  describe('mapStateToProps()', () => {

    it('should map the correct properties to props', () => {
      assert.deepEqual(mapStateToProps('mockState'), {
        amount: 'mockAmount:mockState',
        amountConversionRate: 'mockAmountConversionRate:mockState',
        blockGasLimit: 'mockBlockGasLimit:mockState',
        conversionRate: 'mockConversionRate:mockState',
        editingTransactionId: 'mockEditingTransactionId:mockState',
        from: 'mockFrom:mockState',
        gasLimit: 'mockGasLimit:mockState',
        gasPrice: 'mockGasPrice:mockState',
        gasTotal: 'mockGasTotal:mockState',
        network: 'mockNetwork:mockState',
        primaryCurrency: 'mockPrimaryCurrency:mockState',
        recentBlocks: 'mockRecentBlocks:mockState',
        selectedAddress: 'mockSelectedAddress:mockState',
        selectedToken: 'mockSelectedToken:mockState',
        to: 'mockTo:mockState',
        tokenBalance: 'mockTokenBalance:mockState',
        tokenContract: 'mockTokenContract:mockState',
        tokenToFiatRate: 'mockTokenToFiatRate:mockState',
      })
    })

  })

  describe('mapDispatchToProps()', () => {
    let dispatchSpy
    let mapDispatchToPropsObject

    beforeEach(() => {
      dispatchSpy = sinon.spy()
      mapDispatchToPropsObject = mapDispatchToProps(dispatchSpy)
    })

    describe('updateAndSetGasTotal()', () => {
      const mockProps = {
        blockGasLimit: 'mockBlockGasLimit',
        editingTransactionId: '0x2',
        gasLimit: '0x3',
        gasPrice: '0x4',
        recentBlocks: ['mockBlock'],
        selectedAddress: '0x4',
        selectedToken: { address: '0x1' },
        to: 'mockTo',
        value: 'mockValue',
      }

      it('should dispatch a setGasTotal action when editingTransactionId is truthy', () => {
        mapDispatchToPropsObject.updateAndSetGasTotal(mockProps)
        assert(dispatchSpy.calledOnce)
        assert.equal(
          actionSpies.setGasTotal.getCall(0).args[0],
          '0x30x4'
        )
      })

      it('should dispatch an updateGasData action when editingTransactionId is falsy', () => {
        const { selectedAddress, selectedToken, recentBlocks, blockGasLimit, to, value } = mockProps
        mapDispatchToPropsObject.updateAndSetGasTotal(
          Object.assign({}, mockProps, {editingTransactionId: false})
        )
        assert(dispatchSpy.calledOnce)
        assert.deepEqual(
          actionSpies.updateGasData.getCall(0).args[0],
          { selectedAddress, selectedToken, recentBlocks, blockGasLimit, to, value }
        )
      })
    })

    describe('updateSendTokenBalance()', () => {
      const mockProps = {
        address: '0x10',
        tokenContract: '0x00a',
        selectedToken: {address: '0x1'},
      }

      it('should dispatch an action', () => {
        mapDispatchToPropsObject.updateSendTokenBalance(Object.assign({}, mockProps))
        assert(dispatchSpy.calledOnce)
        assert.deepEqual(
          actionSpies.updateSendTokenBalance.getCall(0).args[0],
          mockProps
        )
      })
    })

    describe('updateSendErrors()', () => {
      it('should dispatch an action', () => {
        mapDispatchToPropsObject.updateSendErrors('mockError')
        assert(dispatchSpy.calledOnce)
        assert.equal(
          duckActionSpies.updateSendErrors.getCall(0).args[0],
          'mockError'
        )
      })
    })

    describe('resetSendState()', () => {
      it('should dispatch an action', () => {
        mapDispatchToPropsObject.resetSendState()
        assert(dispatchSpy.calledOnce)
        assert.equal(
          duckActionSpies.resetSendState.getCall(0).args.length,
          0
        )
      })
    })

  })

})