2019-02-06 23:38:12 +00:00
|
|
|
/* global before describe it require */
|
|
|
|
|
2019-04-28 21:33:41 +00:00
|
|
|
const {startRPCMockServer, TestProvider} = require('./helpers/blockchain');
|
2019-02-06 23:38:12 +00:00
|
|
|
const {assert} = require('chai');
|
2019-04-28 21:33:41 +00:00
|
|
|
const {Blockchain} = require('..');
|
2019-02-11 06:08:01 +00:00
|
|
|
const {promisify} = require('util');
|
2019-02-06 23:38:12 +00:00
|
|
|
|
|
|
|
describe('Blockchain', () => {
|
|
|
|
describe('#connect', () => {
|
|
|
|
before(() => {
|
2019-04-28 21:33:41 +00:00
|
|
|
Blockchain.registerProvider('web3', TestProvider);
|
|
|
|
Blockchain.setProvider('web3', {});
|
2019-02-06 23:38:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const scenarios = [
|
|
|
|
{
|
|
|
|
description: 'should not keep trying other connections if connected',
|
|
|
|
servers: [true, true],
|
|
|
|
visited: [true, false],
|
|
|
|
error: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should keep trying other connections if not connected',
|
|
|
|
servers: [false, true],
|
|
|
|
visited: [true, true],
|
|
|
|
error: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: 'should return error if no connections succeed',
|
|
|
|
servers: [false, false],
|
|
|
|
visited: [true, true],
|
|
|
|
error: true
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
scenarios.forEach(({description, ...scenario}) => {
|
|
|
|
it(description, async () => {
|
|
|
|
const makeServers = async () => {
|
|
|
|
const servers = await Promise.all(
|
2019-02-11 06:08:01 +00:00
|
|
|
scenario.servers.map(server => (
|
|
|
|
promisify(startRPCMockServer)({successful: server})
|
|
|
|
))
|
2019-02-06 23:38:12 +00:00
|
|
|
);
|
|
|
|
const dappConnection = servers.map(server => server.connectionString);
|
|
|
|
return {servers, dappConnection};
|
|
|
|
};
|
|
|
|
|
|
|
|
// test Blockchain.connect() using callback
|
|
|
|
let {servers, dappConnection} = await makeServers();
|
|
|
|
await new Promise((resolve, reject) => {
|
2019-04-28 21:33:41 +00:00
|
|
|
Blockchain.connect({dappConnection}, err => {
|
2019-02-06 23:38:12 +00:00
|
|
|
try {
|
2019-02-11 06:08:01 +00:00
|
|
|
assert(scenario.error ? err : !err);
|
2019-02-06 23:38:12 +00:00
|
|
|
servers.forEach((server, idx) => {
|
|
|
|
assert.strictEqual(server.visited, scenario.visited[idx]);
|
|
|
|
});
|
|
|
|
resolve();
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// test Blockchain.connect() without callback
|
|
|
|
({servers, dappConnection} = await makeServers());
|
|
|
|
try {
|
2019-04-28 21:33:41 +00:00
|
|
|
await Blockchain.connect({dappConnection});
|
2019-02-06 23:38:12 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (!scenario.error) throw e;
|
|
|
|
}
|
|
|
|
servers.forEach((server, idx) => {
|
|
|
|
assert.strictEqual(server.visited, scenario.visited[idx]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|