2021-05-07 21:38:24 +02:00
|
|
|
import { strict as assert } from 'assert';
|
2021-02-04 19:15:23 +01:00
|
|
|
import sinon from 'sinon';
|
2021-09-10 19:37:19 +02:00
|
|
|
import { TokensController } from '@metamask/controllers';
|
2021-03-16 22:00:08 +01:00
|
|
|
import TokenRatesController from './token-rates';
|
2021-09-10 19:37:19 +02:00
|
|
|
import NetworkController from './network';
|
|
|
|
import PreferencesController from './preferences';
|
|
|
|
|
|
|
|
const networkControllerProviderConfig = {
|
|
|
|
getAccounts: () => undefined,
|
|
|
|
};
|
2018-04-16 17:21:06 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('TokenRatesController', function () {
|
2021-09-10 19:37:19 +02:00
|
|
|
let nativeCurrency,
|
|
|
|
getNativeCurrency,
|
|
|
|
network,
|
|
|
|
provider,
|
|
|
|
preferences,
|
|
|
|
tokensController;
|
2021-02-12 18:41:53 +01:00
|
|
|
beforeEach(function () {
|
|
|
|
nativeCurrency = 'ETH';
|
|
|
|
getNativeCurrency = () => nativeCurrency;
|
2021-09-10 19:37:19 +02:00
|
|
|
network = new NetworkController();
|
|
|
|
network.setInfuraProjectId('foo');
|
|
|
|
network.initializeProvider(networkControllerProviderConfig);
|
|
|
|
provider = network.getProviderAndBlockTracker().provider;
|
|
|
|
preferences = new PreferencesController({ network, provider });
|
|
|
|
tokensController = new TokensController({
|
|
|
|
onPreferencesStateChange: preferences.store.subscribe.bind(
|
|
|
|
preferences.store,
|
|
|
|
),
|
|
|
|
onNetworkStateChange: network.store.subscribe.bind(network.store),
|
|
|
|
});
|
|
|
|
sinon.stub(network, 'getLatestBlock').callsFake(() => Promise.resolve({}));
|
|
|
|
sinon.stub(tokensController, '_instantiateNewEthersProvider').returns(null);
|
|
|
|
sinon
|
|
|
|
.stub(tokensController, '_detectIsERC721')
|
|
|
|
.returns(Promise.resolve(false));
|
2021-02-12 18:41:53 +01:00
|
|
|
});
|
2021-09-10 19:37:19 +02:00
|
|
|
it('should listen for tokenControllers state updates', async function () {
|
2021-02-12 18:41:53 +01:00
|
|
|
const controller = new TokenRatesController({
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2021-02-12 18:41:53 +01:00
|
|
|
getNativeCurrency,
|
|
|
|
});
|
2021-09-10 19:37:19 +02:00
|
|
|
await tokensController.addToken('0x1', 'TEST', 1);
|
|
|
|
assert.deepEqual(controller._tokens, [
|
|
|
|
{
|
|
|
|
address: '0x1',
|
|
|
|
decimals: 1,
|
|
|
|
symbol: 'TEST',
|
|
|
|
image: undefined,
|
|
|
|
isERC721: false,
|
|
|
|
},
|
|
|
|
]);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-16 17:21:06 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should poll on correct interval', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const stub = sinon.stub(global, 'setInterval');
|
2021-02-12 18:41:53 +01:00
|
|
|
const controller = new TokenRatesController({
|
2021-09-10 19:37:19 +02:00
|
|
|
tokensController,
|
2021-02-12 18:41:53 +01:00
|
|
|
getNativeCurrency,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
controller.start(1337);
|
2020-06-05 20:36:55 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.strictEqual(stub.getCall(0).args[1], 1337);
|
|
|
|
stub.restore();
|
|
|
|
controller.stop();
|
|
|
|
});
|
|
|
|
});
|