aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/app/controllers/preferences-controller-test.js
blob: e055500b152f69eeec7b294f73fb2a964714181d (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
const assert = require('assert')
const PreferencesController = require('../../../../app/scripts/controllers/preferences')

describe('preferences controller', function () {
  let preferencesController

  beforeEach(() => {
    preferencesController = new PreferencesController()
  })

  describe('setAddresses', function () {
    it('should keep a map of addresses to names and addresses in the store', function () {
      preferencesController.setAddresses([
        '0xda22le',
        '0x7e57e2',
      ])

      const {identities} = preferencesController.store.getState()
      assert.deepEqual(identities, {
        '0xda22le': {
          name: 'Account 1',
          address: '0xda22le',
        },
        '0x7e57e2': {
          name: 'Account 2',
          address: '0x7e57e2',
        },
      })
    })

    it('should replace its list of addresses', function () {
      preferencesController.setAddresses([
        '0xda22le',
        '0x7e57e2',
      ])
      preferencesController.setAddresses([
        '0xda22le77',
        '0x7e57e277',
      ])

      const {identities} = preferencesController.store.getState()
      assert.deepEqual(identities, {
        '0xda22le77': {
          name: 'Account 1',
          address: '0xda22le77',
        },
        '0x7e57e277': {
          name: 'Account 2',
          address: '0x7e57e277',
        },
      })
    })
  })

  describe('removeAddress', function () {
    it('should remove an address from state', function () {
      preferencesController.setAddresses([
        '0xda22le',
        '0x7e57e2',
      ])

      preferencesController.removeAddress('0xda22le')

      assert.equal(preferencesController.store.getState().identities['0xda22le'], undefined)
    })

    it('should switch accounts if the selected address is removed', function () {
      preferencesController.setAddresses([
        '0xda22le',
        '0x7e57e2',
      ])

      preferencesController.setSelectedAddress('0x7e57e2')
      preferencesController.removeAddress('0x7e57e2')

      assert.equal(preferencesController.getSelectedAddress(), '0xda22le')
    })
  })

  describe('setAccountLabel', function () {
    it('should update a label for the given account', function () {
      preferencesController.setAddresses([
        '0xda22le',
        '0x7e57e2',
      ])

      assert.deepEqual(preferencesController.store.getState().identities['0xda22le'], {
        name: 'Account 1',
        address: '0xda22le',
      })


      preferencesController.setAccountLabel('0xda22le', 'Dazzle')
      assert.deepEqual(preferencesController.store.getState().identities['0xda22le'], {
        name: 'Dazzle',
        address: '0xda22le',
      })
    })
  })

  describe('getTokens', function () {
    it('should return an empty list initially', async function () {
      await preferencesController.setSelectedAddress('0x7e57e2')

      const tokens = preferencesController.getTokens()
      assert.equal(tokens.length, 0, 'empty list of tokens')
    })
  })

  describe('addToken', function () {
    it('should add that token to its state', async function () {
      const address = '0xabcdef1234567'
      const symbol = 'ABBR'
      const decimals = 5

      await preferencesController.setSelectedAddress('0x7e57e2')
      await preferencesController.addToken(address, symbol, decimals)

      const tokens = preferencesController.getTokens()
      assert.equal(tokens.length, 1, 'one token added')

      const added = tokens[0]
      assert.equal(added.address, address, 'set address correctly')
      assert.equal(added.symbol, symbol, 'set symbol correctly')
      assert.equal(added.decimals, decimals, 'set decimals correctly')
    })

    it('should allow updating a token value', async function () {
      const address = '0xabcdef1234567'
      const symbol = 'ABBR'
      const decimals = 5

      await preferencesController.setSelectedAddress('0x7e57e2')
      await preferencesController.addToken(address, symbol, decimals)

      const newDecimals = 6
      await preferencesController.addToken(address, symbol, newDecimals)

      const tokens = preferencesController.getTokens()
      assert.equal(tokens.length, 1, 'one token added')

      const added = tokens[0]
      assert.equal(added.address, address, 'set address correctly')
      assert.equal(added.symbol, symbol, 'set symbol correctly')
      assert.equal(added.decimals, newDecimals, 'updated decimals correctly')
    })

    it('should allow adding tokens to two separate addresses', async function () {
      const address = '0xabcdef1234567'
      const symbol = 'ABBR'
      const decimals = 5

      await preferencesController.setSelectedAddress('0x7e57e2')
      await preferencesController.addToken(address, symbol, decimals)
      assert.equal(preferencesController.getTokens().length, 1, 'one token added for 1st address')

      await preferencesController.setSelectedAddress('0xda22le')
      await preferencesController.addToken(address, symbol, decimals)
      assert.equal(preferencesController.getTokens().length, 1, 'one token added for 2nd address')
    })
  })

  describe('removeToken', function () {
    it('should remove the only token from its state', async function () {
      await preferencesController.setSelectedAddress('0x7e57e2')
      await preferencesController.addToken('0xa', 'A', 5)
      await preferencesController.removeToken('0xa')

      const tokens = preferencesController.getTokens()
      assert.equal(tokens.length, 0, 'one token removed')
    })

    it('should remove a token from its state', async function () {
      await preferencesController.setSelectedAddress('0x7e57e2')
      await preferencesController.addToken('0xa', 'A', 4)
      await preferencesController.addToken('0xb', 'B', 5)
      await preferencesController.removeToken('0xa')

      const tokens = preferencesController.getTokens()
      assert.equal(tokens.length, 1, 'one token removed')

      const [token1] = tokens
      assert.deepEqual(token1, {address: '0xb', symbol: 'B', decimals: 5})
    })
  })
})