1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/test/unit/app/controllers/detect-tokens-test.js

142 lines
5.5 KiB
JavaScript
Raw Normal View History

import assert from 'assert'
import nock from 'nock'
import sinon from 'sinon'
import ObservableStore from 'obs-store'
import DetectTokensController from '../../../../app/scripts/controllers/detect-tokens'
import NetworkController from '../../../../app/scripts/controllers/network/network'
import PreferencesController from '../../../../app/scripts/controllers/preferences'
2018-06-28 04:18:06 +02:00
describe('DetectTokensController', function () {
2018-07-30 15:26:34 +02:00
const sandbox = sinon.createSandbox()
let clock, keyringMemStore, network, preferences, controller
2018-07-30 15:26:34 +02:00
const noop = () => {}
const networkControllerProviderConfig = {
getAccounts: noop,
}
beforeEach(async function () {
2018-07-30 15:26:34 +02:00
2018-07-30 15:26:34 +02:00
nock('https://api.infura.io')
.get(/.*/)
.reply(200)
2019-12-03 21:50:55 +01:00
keyringMemStore = new ObservableStore({ isUnlocked: false })
2018-07-30 15:26:34 +02:00
network = new NetworkController()
preferences = new PreferencesController({ network })
2018-07-30 15:26:34 +02:00
controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore })
network.initializeProvider(networkControllerProviderConfig)
})
after(function () {
sandbox.restore()
nock.cleanAll()
2018-07-11 21:59:05 +02:00
})
it('should poll on correct interval', async function () {
2018-06-28 04:18:06 +02:00
const stub = sinon.stub(global, 'setInterval')
new DetectTokensController({ interval: 1337 }) // eslint-disable-line no-new
assert.strictEqual(stub.getCall(0).args[1], 1337)
stub.restore()
})
it('should be called on every polling period', async function () {
2018-07-11 21:59:05 +02:00
clock = sandbox.useFakeTimers()
2018-07-13 02:43:43 +02:00
const network = new NetworkController()
2018-07-30 15:26:34 +02:00
network.initializeProvider(networkControllerProviderConfig)
2018-07-13 02:43:43 +02:00
network.setProviderType('mainnet')
const preferences = new PreferencesController({ network })
2018-07-20 01:46:46 +02:00
const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore })
2018-07-21 01:58:03 +02:00
controller.isOpen = true
controller.isUnlocked = true
2018-07-11 21:59:05 +02:00
const stub = sandbox.stub(controller, 'detectNewTokens')
2018-07-11 21:59:05 +02:00
clock.tick(1)
sandbox.assert.notCalled(stub)
clock.tick(180000)
sandbox.assert.called(stub)
clock.tick(180000)
sandbox.assert.calledTwice(stub)
clock.tick(180000)
sandbox.assert.calledThrice(stub)
})
it('should not check tokens while in test network', async function () {
2018-07-21 01:58:03 +02:00
controller.isOpen = true
controller.isUnlocked = true
2018-07-11 21:59:05 +02:00
const stub = sandbox.stub(controller, 'detectTokenBalance')
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
2018-07-11 21:59:05 +02:00
await controller.detectNewTokens()
2018-07-11 21:59:05 +02:00
sandbox.assert.notCalled(stub)
})
2018-06-28 04:18:06 +02:00
it('should only check and add tokens while in main network', async function () {
2018-07-20 01:46:46 +02:00
const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore })
2018-07-21 01:58:03 +02:00
controller.isOpen = true
controller.isUnlocked = true
2018-07-11 21:59:05 +02:00
sandbox.stub(controller, 'detectTokenBalance')
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4')
.returns(preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8))
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388')
.returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18))
2018-07-11 21:59:05 +02:00
await controller.detectNewTokens()
2019-12-03 21:50:55 +01:00
assert.deepEqual(preferences.store.getState().tokens, [{ address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T' },
{ address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK' }])
2018-06-28 04:18:06 +02:00
})
it('should not detect same token while in main network', async function () {
2018-07-11 21:59:05 +02:00
preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8)
2018-07-20 01:46:46 +02:00
const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore })
2018-07-21 01:58:03 +02:00
controller.isOpen = true
controller.isUnlocked = true
2018-07-11 21:59:05 +02:00
sandbox.stub(controller, 'detectTokenBalance')
2018-07-13 02:43:43 +02:00
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4')
.returns(preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8))
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388')
.returns(preferences.addToken('0xbc86727e770de68b1060c91f6bb6945c73e10388', 'XNK', 18))
2018-07-11 21:59:05 +02:00
await controller.detectNewTokens()
2019-12-03 21:50:55 +01:00
assert.deepEqual(preferences.store.getState().tokens, [{ address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', decimals: 8, symbol: 'J8T' },
{ address: '0xbc86727e770de68b1060c91f6bb6945c73e10388', decimals: 18, symbol: 'XNK' }])
2018-06-28 04:18:06 +02:00
})
2018-07-20 01:46:46 +02:00
it('should trigger detect new tokens when change address', async function () {
2018-07-21 01:58:03 +02:00
controller.isOpen = true
controller.isUnlocked = true
const stub = sandbox.stub(controller, 'detectNewTokens')
2018-07-20 01:46:46 +02:00
await preferences.setSelectedAddress('0xbc86727e770de68b1060c91f6bb6945c73e10388')
sandbox.assert.called(stub)
})
it('should trigger detect new tokens when submit password', async function () {
2018-07-21 01:58:03 +02:00
controller.isOpen = true
2018-07-20 01:46:46 +02:00
controller.selectedAddress = '0x0'
const stub = sandbox.stub(controller, 'detectNewTokens')
2018-07-20 01:46:46 +02:00
await controller._keyringMemStore.updateState({ isUnlocked: true })
sandbox.assert.called(stub)
})
2018-07-21 01:58:03 +02:00
it('should not trigger detect new tokens when not open or not unlocked', async function () {
2018-07-21 01:58:03 +02:00
controller.isOpen = true
controller.isUnlocked = false
const stub = sandbox.stub(controller, 'detectTokenBalance')
2018-07-21 01:58:03 +02:00
clock.tick(180000)
sandbox.assert.notCalled(stub)
controller.isOpen = false
controller.isUnlocked = true
clock.tick(180000)
sandbox.assert.notCalled(stub)
})
2018-09-10 23:11:57 +02:00
})