1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/app/scripts/controllers/token-rates-controller.test.js
Alex Donesky 490d3b8d40
Integrate TokensController (#11552)
* Integrate controllers/tokensController

* address rebase issues

* small cleanup

* addressing feedback

* more feedback
2021-09-10 12:37:19 -05:00

69 lines
2.1 KiB
JavaScript

import { strict as assert } from 'assert';
import sinon from 'sinon';
import { TokensController } from '@metamask/controllers';
import TokenRatesController from './token-rates';
import NetworkController from './network';
import PreferencesController from './preferences';
const networkControllerProviderConfig = {
getAccounts: () => undefined,
};
describe('TokenRatesController', function () {
let nativeCurrency,
getNativeCurrency,
network,
provider,
preferences,
tokensController;
beforeEach(function () {
nativeCurrency = 'ETH';
getNativeCurrency = () => nativeCurrency;
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));
});
it('should listen for tokenControllers state updates', async function () {
const controller = new TokenRatesController({
tokensController,
getNativeCurrency,
});
await tokensController.addToken('0x1', 'TEST', 1);
assert.deepEqual(controller._tokens, [
{
address: '0x1',
decimals: 1,
symbol: 'TEST',
image: undefined,
isERC721: false,
},
]);
});
it('should poll on correct interval', async function () {
const stub = sinon.stub(global, 'setInterval');
const controller = new TokenRatesController({
tokensController,
getNativeCurrency,
});
controller.start(1337);
assert.strictEqual(stub.getCall(0).args[1], 1337);
stub.restore();
controller.stop();
});
});