aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/send_/send-footer/tests/send-footer-utils.test.js
blob: 2d3135995bf4966379ab9d53ec509f3d06e16064 (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
import assert from 'assert'
import proxyquire from 'proxyquire'
import sinon from 'sinon'
const { TOKEN_TRANSFER_FUNCTION_SIGNATURE } = require('../../send.constants')

const stubs = {
  rawEncode: sinon.stub().callsFake((arr1, arr2) => {
    return [ ...arr1, ...arr2 ]
  }),
}

const sendUtils = proxyquire('../send-footer.utils.js', {
  'ethereumjs-abi': {
    rawEncode: stubs.rawEncode,
  },
})
const {
  addressIsNew,
  constructTxParams,
  constructUpdatedTx,
  addHexPrefixToObjectValues,
} = sendUtils

describe('send-footer utils', () => {

  describe('addHexPrefixToObjectValues()', () => {
    it('should return a new object with the same properties with a 0x prefix', () => {
      assert.deepEqual(
        addHexPrefixToObjectValues({
          prop1: '0x123',
          prop2: '456',
          prop3: 'x',
        }),
        {
          prop1: '0x123',
          prop2: '0x456',
          prop3: '0xx',
        }
      )
    })
  })

  describe('addressIsNew()', () => {
    it('should return false if the address exists in toAccounts', () => {
      assert.equal(
        addressIsNew([
          { address: '0xabc' },
          { address: '0xdef' },
          { address: '0xghi' },
        ], '0xdef'),
        false
      )
    })

    it('should return true if the address does not exists in toAccounts', () => {
      assert.equal(
        addressIsNew([
          { address: '0xabc' },
          { address: '0xdef' },
          { address: '0xghi' },
        ], '0xxyz'),
        true
      )
    })
  })

  describe('constructTxParams()', () => {
    it('should return a new txParams object with value and to properties if there is no selectedToken', () => {
      assert.deepEqual(
        constructTxParams({
          selectedToken: false,
          to: 'mockTo',
          amount: 'mockAmount',
          from: 'mockFrom',
          gas: 'mockGas',
          gasPrice: 'mockGasPrice',
        }),
        {
          to: '0xmockTo',
          value: '0xmockAmount',
          from: '0xmockFrom',
          gas: '0xmockGas',
          gasPrice: '0xmockGasPrice',
        }
      )
    })

    it('should return a new txParams object without a to property and a 0 value if there is a selectedToken', () => {
      assert.deepEqual(
        constructTxParams({
          selectedToken: true,
          to: 'mockTo',
          amount: 'mockAmount',
          from: 'mockFrom',
          gas: 'mockGas',
          gasPrice: 'mockGasPrice',
        }),
        {
          value: '0x0',
          from: '0xmockFrom',
          gas: '0xmockGas',
          gasPrice: '0xmockGasPrice',
        }
      )
    })
  })

  describe('constructUpdatedTx()', () => {
    it('should return a new object with an updated txParams', () => {
      const result = constructUpdatedTx({
        amount: 'mockAmount',
        editingTransactionId: '0x456',
        from: 'mockFrom',
        gas: 'mockGas',
        gasPrice: 'mockGasPrice',
        selectedToken: false,
        to: 'mockTo',
        unapprovedTxs: {
          '0x123': {},
          '0x456': {
            unapprovedTxParam: 'someOtherParam',
            txParams: {
              data: 'someData',
            },
          },
        },
      })

      assert.deepEqual(result, {
        unapprovedTxParam: 'someOtherParam',
        txParams: {
          from: '0xmockFrom',
          gas: '0xmockGas',
          gasPrice: '0xmockGasPrice',
          value: '0xmockAmount',
          to: '0xmockTo',
          data: '0xsomeData',
        },
      })
    })

    it('should not have data property if there is non in the original tx', () => {
      const result = constructUpdatedTx({
        amount: 'mockAmount',
        editingTransactionId: '0x456',
        from: 'mockFrom',
        gas: 'mockGas',
        gasPrice: 'mockGasPrice',
        selectedToken: false,
        to: 'mockTo',
        unapprovedTxs: {
          '0x123': {},
          '0x456': {
            unapprovedTxParam: 'someOtherParam',
            txParams: {
              from: 'oldFrom',
              gas: 'oldGas',
              gasPrice: 'oldGasPrice',
            },
          },
        },
      })

      assert.deepEqual(result, {
        unapprovedTxParam: 'someOtherParam',
        txParams: {
          from: '0xmockFrom',
          gas: '0xmockGas',
          gasPrice: '0xmockGasPrice',
          value: '0xmockAmount',
          to: '0xmockTo',
        },
      })
    })

    it('should have token property values if selectedToken is truthy', () => {
      const result = constructUpdatedTx({
        amount: 'mockAmount',
        editingTransactionId: '0x456',
        from: 'mockFrom',
        gas: 'mockGas',
        gasPrice: 'mockGasPrice',
        selectedToken: {
          address: 'mockTokenAddress',
        },
        to: 'mockTo',
        unapprovedTxs: {
          '0x123': {},
          '0x456': {
            unapprovedTxParam: 'someOtherParam',
            txParams: {},
          },
        },
      })

      assert.deepEqual(result, {
        unapprovedTxParam: 'someOtherParam',
        txParams: {
          from: '0xmockFrom',
          gas: '0xmockGas',
          gasPrice: '0xmockGasPrice',
          value: '0x0',
          to: '0xmockTokenAddress',
          data: `${TOKEN_TRANSFER_FUNCTION_SIGNATURE}ss56Tont`,
        },
      })
    })
  })

})