Skip to content

Commit

Permalink
ModuleManager unit tests 🔧
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby authored and Ben Clabby committed Jul 9, 2021
1 parent 58b58e8 commit d3f747f
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions packages/bitcore-node/test/unit/modules.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { Modules } from '../../src/modules';
import { ChainStateProvider } from '../../src/providers/chain-state';
import { Libs } from '../../src/providers/libs';
import { Config } from '../../src/services/config';
Expand All @@ -10,6 +12,27 @@ describe('Modules', function() {
before(unitBeforeHelper);
after(unitAfterHelper);

it('should load configured modules correctly', () => {
const sandbox = sinon.createSandbox();
sandbox.stub(Config, 'get').returns(mockConfig);

validateModules();
sandbox.restore();
});

it('should prevent double module registration', () => {
const sandbox = sinon.createSandbox();
sandbox.stub(Config, 'get').returns({
// Double module registration test case. ModuleManager will attempt to register BTC Module from
// "config.modules" as well as "config.chains". Should only register once.
modules: ['./bitcoin'],
...mockConfig
});

validateModules();
sandbox.restore();
});

it('should have a test which runs', function() {
expect(true).to.equal(true);
});
Expand Down Expand Up @@ -46,3 +69,51 @@ describe('Modules', function() {
}
});
});

const mockConfig = {
chains: {
BTC: {
testnet: {
chainSource: 'p2p',
trustedPeers: [
{
host: '127.0.0.1',
port: 18333
}
],
rpc: {
host: '127.0.0.1',
port: 18332,
username: 'bitpaytest',
password: 'local321'
}
},
},
ETH: {
dev: {
trustedPeers: [
{
host: '127.0.0.1',
port: 8545
}
],
chainSource: 'p2p',
provider: {
protocol: 'http',
host: '127.0.0.1',
port: 8545,
chain: 'ETH'
}
}
}
}
};

const validateModules = () => {
Modules.internalServices = []; // Remove all loaded modules from internalServices array for a fresh load
Modules.loadConfigured(); // Re-load modules with stubbed Config.get()

expect(Modules.internalServices.length).to.equal(2);
expect(Modules.internalServices[0].constructor.name).to.equal('BitcoinModule');
expect(Modules.internalServices[1].constructor.name).to.equal('ETHModule');
};

0 comments on commit d3f747f

Please sign in to comment.