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

import {
  REQUIRED_ERROR,
  INVALID_RECIPIENT_ADDRESS_ERROR,
  KNOWN_RECIPIENT_ADDRESS_ERROR,
} from '../../../send.constants'

const stubs = {
  isValidAddress: sinon.stub().callsFake(to => Boolean(to.match(/^[0xabcdef123456798]+$/))),
}

const toRowUtils = proxyquire('../send-to-row.utils.js', {
  '../../../../util': {
    isValidAddress: stubs.isValidAddress,
  },
})
const {
  getToErrorObject,
  getToWarningObject,
} = toRowUtils

describe('send-to-row utils', () => {

  describe('getToErrorObject()', () => {
    it('should return a required error if to is falsy', () => {
      assert.deepEqual(getToErrorObject(null), {
        to: REQUIRED_ERROR,
      })
    })

    it('should return null if to is falsy and hexData is truthy', () => {
      assert.deepEqual(getToErrorObject(null, undefined, true), {
        to: null,
      })
    })

    it('should return an invalid recipient error if to is truthy but invalid', () => {
      assert.deepEqual(getToErrorObject('mockInvalidTo'), {
        to: INVALID_RECIPIENT_ADDRESS_ERROR,
      })
    })

    it('should return null if to is truthy and valid', () => {
      assert.deepEqual(getToErrorObject('0xabc123'), {
        to: null,
      })
    })

    it('should return the passed error if to is truthy but invalid if to is truthy and valid', () => {
      assert.deepEqual(getToErrorObject('invalid #$ 345878', 'someExplicitError'), {
        to: 'someExplicitError',
      })
    })

    it('should return a known address recipient if to is truthy but part of state tokens', () => {
      assert.deepEqual(getToErrorObject('0xabc123', undefined, false, [{'address': '0xabc123'}], {'address': '0xabc123'}), {
        to: KNOWN_RECIPIENT_ADDRESS_ERROR,
      })
    })

    it('should null if to is truthy part of tokens but selectedToken falsy', () => {
      assert.deepEqual(getToErrorObject('0xabc123', undefined, false, [{'address': '0xabc123'}]), {
        to: null,
      })
    })

    it('should return a known address recipient if to is truthy but part of contract metadata', () => {
      assert.deepEqual(getToErrorObject('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359', undefined, false, [{'address': '0xabc123'}], {'address': '0xabc123'}), {
        to: KNOWN_RECIPIENT_ADDRESS_ERROR,
      })
    })
    it('should null if to is truthy part of contract metadata but selectedToken falsy', () => {
      assert.deepEqual(getToErrorObject('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359', undefined, false, [{'address': '0xabc123'}], {'address': '0xabc123'}), {
        to: KNOWN_RECIPIENT_ADDRESS_ERROR,
      })
    })
  })

  describe('getToWarningObject()', () => {
    it('should return a known address recipient if to is truthy but part of state tokens', () => {
      assert.deepEqual(getToWarningObject('0xabc123', undefined, [{'address': '0xabc123'}], {'address': '0xabc123'}), {
        to: KNOWN_RECIPIENT_ADDRESS_ERROR,
      })
    })

    it('should null if to is truthy part of tokens but selectedToken falsy', () => {
      assert.deepEqual(getToWarningObject('0xabc123', undefined, [{'address': '0xabc123'}]), {
        to: null,
      })
    })

    it('should return a known address recipient if to is truthy but part of contract metadata', () => {
      assert.deepEqual(getToWarningObject('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359', undefined, [{'address': '0xabc123'}], {'address': '0xabc123'}), {
        to: KNOWN_RECIPIENT_ADDRESS_ERROR,
      })
    })
    it('should null if to is truthy part of contract metadata but selectedToken falsy', () => {
      assert.deepEqual(getToWarningObject('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359', undefined, [{'address': '0xabc123'}], {'address': '0xabc123'}), {
        to: KNOWN_RECIPIENT_ADDRESS_ERROR,
      })
    })
  })

})