2018-06-28 04:18:06 +02:00
|
|
|
const assert = require('assert')
|
|
|
|
const sinon = require('sinon')
|
2018-07-20 01:46:46 +02:00
|
|
|
const ObservableStore = require('obs-store')
|
2018-06-28 04:18:06 +02:00
|
|
|
const DetectTokensController = require('../../../../app/scripts/controllers/detect-tokens')
|
2018-07-13 02:43:43 +02:00
|
|
|
const NetworkController = require('../../../../app/scripts/controllers/network/network')
|
2018-06-28 04:18:06 +02:00
|
|
|
const PreferencesController = require('../../../../app/scripts/controllers/preferences')
|
|
|
|
|
|
|
|
describe('DetectTokensController', () => {
|
2018-07-20 01:46:46 +02:00
|
|
|
const sandbox = sinon.createSandbox()
|
2018-08-08 20:30:49 +02:00
|
|
|
let clock, keyringMemStore, network, preferences
|
|
|
|
beforeEach(async () => {
|
2018-07-20 01:46:46 +02:00
|
|
|
keyringMemStore = new ObservableStore({ isUnlocked: false})
|
2018-08-08 20:30:49 +02:00
|
|
|
network = new NetworkController({ provider: { type: 'mainnet' }})
|
|
|
|
preferences = new PreferencesController({ network })
|
2018-07-20 01:46:46 +02:00
|
|
|
})
|
2018-07-11 21:59:05 +02:00
|
|
|
after(() => {
|
|
|
|
sandbox.restore()
|
|
|
|
})
|
|
|
|
|
2018-06-28 04:18:06 +02:00
|
|
|
it('should poll on correct interval', async () => {
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
|
2018-07-11 21:59:05 +02:00
|
|
|
it('should be called on every polling period', async () => {
|
|
|
|
clock = sandbox.useFakeTimers()
|
2018-07-13 02:43:43 +02:00
|
|
|
network.setProviderType('mainnet')
|
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
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
var 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)
|
|
|
|
})
|
|
|
|
|
2018-06-28 04:18:06 +02:00
|
|
|
it('should not check tokens while in test network', async () => {
|
2018-07-13 02:43:43 +02:00
|
|
|
network.setProviderType('rinkeby')
|
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
|
|
|
|
|
|
|
var stub = sandbox.stub(controller, 'detectTokenBalance')
|
|
|
|
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
|
|
|
|
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
|
|
|
|
|
2018-07-19 21:56:38 +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 () => {
|
2018-07-13 02:43:43 +02:00
|
|
|
network.setProviderType('mainnet')
|
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
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
await controller.detectNewTokens()
|
2018-07-11 21:59:05 +02: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 () => {
|
2018-07-13 02:43:43 +02:00
|
|
|
network.setProviderType('mainnet')
|
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
|
|
|
|
2018-07-19 21:56:38 +02:00
|
|
|
await controller.detectNewTokens()
|
2018-07-11 21:59:05 +02: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 () => {
|
|
|
|
network.setProviderType('mainnet')
|
|
|
|
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-20 01:46:46 +02:00
|
|
|
var stub = sandbox.stub(controller, 'detectNewTokens')
|
|
|
|
await preferences.setSelectedAddress('0xbc86727e770de68b1060c91f6bb6945c73e10388')
|
|
|
|
sandbox.assert.called(stub)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should trigger detect new tokens when submit password', async () => {
|
|
|
|
network.setProviderType('mainnet')
|
|
|
|
const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore })
|
2018-07-21 01:58:03 +02:00
|
|
|
controller.isOpen = true
|
2018-07-20 01:46:46 +02:00
|
|
|
controller.selectedAddress = '0x0'
|
|
|
|
var stub = sandbox.stub(controller, 'detectNewTokens')
|
|
|
|
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 () => {
|
|
|
|
network.setProviderType('mainnet')
|
|
|
|
const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore })
|
|
|
|
controller.isOpen = true
|
|
|
|
controller.isUnlocked = false
|
|
|
|
var stub = sandbox.stub(controller, 'detectTokenBalance')
|
|
|
|
clock.tick(180000)
|
|
|
|
sandbox.assert.notCalled(stub)
|
|
|
|
controller.isOpen = false
|
|
|
|
controller.isUnlocked = true
|
|
|
|
clock.tick(180000)
|
|
|
|
sandbox.assert.notCalled(stub)
|
|
|
|
})
|
2018-06-28 04:18:06 +02:00
|
|
|
})
|