aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/app/controllers/cached-balances-test.js
blob: 27aeabba29833051535ced3b1ca532266d82a81b (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
const assert = require('assert')
const sinon = require('sinon')
const CachedBalancesController = require('../../../../app/scripts/controllers/cached-balances')

describe('CachedBalancesController', () => {
  describe('updateCachedBalances', () => {
    it('should update the cached balances', async () => {
      const controller = new CachedBalancesController({
        getNetwork: () => Promise.resolve(17),
        accountTracker: {
          store: {
            subscribe: () => {},
          },
        },
        initState: {
          cachedBalances: 'mockCachedBalances',
        },
      })

      controller._generateBalancesToCache = sinon.stub().callsFake(() => Promise.resolve('mockNewCachedBalances'))

      await controller.updateCachedBalances({ accounts: 'mockAccounts' })

      assert.equal(controller._generateBalancesToCache.callCount, 1)
      assert.deepEqual(controller._generateBalancesToCache.args[0], ['mockAccounts', 17])
      assert.equal(controller.store.getState().cachedBalances, 'mockNewCachedBalances')
    })
  })

  describe('_generateBalancesToCache', () => {
    it('should generate updated account balances where the current network was updated', () => {
      const controller = new CachedBalancesController({
        accountTracker: {
          store: {
            subscribe: () => {},
          },
        },
        initState: {
          cachedBalances: {
            17: {
              a: '0x1',
              b: '0x2',
              c: '0x3',
            },
            16: {
              a: '0xa',
              b: '0xb',
              c: '0xc',
            },
          },
        },
      })

      const result = controller._generateBalancesToCache({
        a: { balance: '0x4' },
        b: { balance: null },
        c: { balance: '0x5' },
      }, 17)

      assert.deepEqual(result, {
        17: {
          a: '0x4',
          b: '0x2',
          c: '0x5',
        },
        16: {
          a: '0xa',
          b: '0xb',
          c: '0xc',
        },
      })
    })

    it('should generate updated account balances where the a new network was selected', () => {
      const controller = new CachedBalancesController({
        accountTracker: {
          store: {
            subscribe: () => {},
          },
        },
        initState: {
          cachedBalances: {
            17: {
              a: '0x1',
              b: '0x2',
              c: '0x3',
            },
          },
        },
      })

      const result = controller._generateBalancesToCache({
        a: { balance: '0x4' },
        b: { balance: null },
        c: { balance: '0x5' },
      }, 16)

      assert.deepEqual(result, {
        17: {
          a: '0x1',
          b: '0x2',
          c: '0x3',
        },
        16: {
          a: '0x4',
          c: '0x5',
        },
      })
    })
  })

  describe('_registerUpdates', () => {
    it('should subscribe to the account tracker with the updateCachedBalances method', async () => {
      const subscribeSpy = sinon.spy()
      const controller = new CachedBalancesController({
        getNetwork: () => Promise.resolve(17),
        accountTracker: {
          store: {
            subscribe: subscribeSpy,
          },
        },
      })
      subscribeSpy.resetHistory()

      const updateCachedBalancesSpy = sinon.spy()
      controller.updateCachedBalances = updateCachedBalancesSpy
      controller._registerUpdates({ accounts: 'mockAccounts' })

      assert.equal(subscribeSpy.callCount, 1)

      subscribeSpy.args[0][0]()

      assert.equal(updateCachedBalancesSpy.callCount, 1)
    })
  })

})