1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/test/unit/app/controllers/detect-tokens-test.js

87 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-06-28 04:18:06 +02:00
const assert = require('assert')
const sinon = require('sinon')
const DetectTokensController = require('../../../../app/scripts/controllers/detect-tokens')
const PreferencesController = require('../../../../app/scripts/controllers/preferences')
const ObservableStore = require('obs-store')
describe('DetectTokensController', () => {
2018-07-11 21:59:05 +02:00
const sandbox = sinon.createSandbox()
let clock
before(async () => {
})
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()
const network = new ObservableStore({provider: {type: 'mainnet'}})
const preferences = new PreferencesController()
const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true
var stub = sandbox.stub(controller, 'exploreNewTokens')
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-03 21:24:23 +02:00
var network = new ObservableStore({provider: {type: 'rinkeby'}})
2018-06-28 04:18:06 +02:00
const preferences = new PreferencesController()
const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true
2018-07-11 21:59:05 +02:00
var stub = sandbox.stub(controller, 'detectTokenBalance')
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
2018-06-28 04:18:06 +02:00
await controller.exploreNewTokens()
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-03 21:24:23 +02:00
const network = new ObservableStore({provider: {type: 'mainnet'}})
2018-06-28 04:18:06 +02:00
const preferences = new PreferencesController()
const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true
2018-07-11 21:59:05 +02:00
sandbox.stub(controller, 'detectTokenBalance')
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
2018-06-28 04:18:06 +02:00
await controller.exploreNewTokens()
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-03 21:24:23 +02:00
const network = new ObservableStore({provider: {type: 'mainnet'}})
2018-06-28 04:18:06 +02:00
const preferences = new PreferencesController()
2018-07-11 21:59:05 +02:00
preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8)
2018-06-28 04:18:06 +02:00
const controller = new DetectTokensController({preferences: preferences, network: network})
controller.isActive = true
2018-07-11 21:59:05 +02:00
sandbox.stub(controller, 'detectTokenBalance')
.withArgs('0x0D262e5dC4A06a0F1c90cE79C7a60C09DfC884E4').returns(true)
.withArgs('0xBC86727E770de68B1060C91f6BB6945c73e10388').returns(true)
2018-06-28 04:18:06 +02:00
await controller.exploreNewTokens()
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
})
})