fix(@embark/embarkjs): use getNetworkId to test connection

This fixes the case when dappAutoEnable is false and tehn in Status
getAccounts returned an error
This commit is contained in:
Jonathan Rainville 2019-12-12 15:58:17 -05:00 committed by Iuri Matias
parent 7e707616be
commit 60f4d2f27f
3 changed files with 23 additions and 22 deletions

View File

@ -100,18 +100,23 @@ Blockchain.doConnect = function(connectionList, opts, doneCb) {
const self = this; const self = this;
const checkConnect = (next) => { const checkConnect = (next) => {
this.blockchainConnector.getAccounts((error, _a) => { const provider = self.blockchainConnector.getCurrentProvider();
const provider = self.blockchainConnector.getCurrentProvider(); const connectionString = provider.host;
const connectionString = provider.host; this.blockchainConnector.getNetworkId()
.then(_id => {
if (error) this.blockchainConnector.setProvider(null); next(null, {
connectionString,
return next(null, { error: null,
connectionString, connected: true
error, });
connected: !error })
.catch(error => {
next(null, {
connectionString,
error,
connected: false
});
}); });
});
}; };
const connectWeb3 = async (next) => { const connectWeb3 = async (next) => {
@ -171,10 +176,7 @@ Blockchain.doConnect = function(connectionList, opts, doneCb) {
return doneCb(new BlockchainConnectionError(connectionErrs)); return doneCb(new BlockchainConnectionError(connectionErrs));
} }
self.blockchainConnector.getAccounts(async (err, accounts) => { self.blockchainConnector.getAccounts(async (_err, accounts) => {
if (err) {
return doneCb(err);
}
const currentProv = self.blockchainConnector.getCurrentProvider(); const currentProv = self.blockchainConnector.getCurrentProvider();
if (opts.warnAboutMetamask) { if (opts.warnAboutMetamask) {
// if we are using metamask, ask embark to turn on dev_funds // if we are using metamask, ask embark to turn on dev_funds

View File

@ -1,4 +1,4 @@
/* global before describe it require */ /* global before describe it */
const {startRPCMockServer, TestProvider} = require('./helpers/blockchain'); const {startRPCMockServer, TestProvider} = require('./helpers/blockchain');
const {assert} = require('chai'); const {assert} = require('chai');
@ -50,7 +50,8 @@ describe('Blockchain', () => {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
Blockchain.connect({dappConnection}, err => { Blockchain.connect({dappConnection}, err => {
try { try {
assert(scenario.error ? err : !err); assert(scenario.error ? err : !err,
scenario.error ? 'There should have been an error, but there was none' : 'There should not have been an error, but there was one');
servers.forEach((server, idx) => { servers.forEach((server, idx) => {
assert.strictEqual(server.visited, scenario.visited[idx]); assert.strictEqual(server.visited, scenario.visited[idx]);
}); });

View File

@ -27,11 +27,9 @@ const startRPCMockServer = (options = {}, callback) => {
req.on('end', () => { req.on('end', () => {
const request = JSON.parse(body); const request = JSON.parse(body);
const accountsResponse = JSON.stringify({ const accountsResponse = JSON.stringify({
"jsonrpc": "2.0", jsonrpc: "2.0",
"id": request.id, id: request.id,
"result": [ result: 1337
"0x7c67d951b7338a96168f259a16b7ba25e7a30315"
]
}); });
res.writeHead(200, { res.writeHead(200, {