From 60f4d2f27fe0208c7b89f4662a426ed9cd3ea93d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 12 Dec 2019 15:58:17 -0500 Subject: [PATCH] fix(@embark/embarkjs): use getNetworkId to test connection This fixes the case when dappAutoEnable is false and tehn in Status getAccounts returned an error --- .../embarkjs/embarkjs/src/lib/blockchain.js | 32 ++++++++++--------- .../embarkjs/embarkjs/src/test/blockchain.js | 5 +-- .../embarkjs/src/test/helpers/blockchain.js | 8 ++--- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/embarkjs/embarkjs/src/lib/blockchain.js b/packages/embarkjs/embarkjs/src/lib/blockchain.js index ee1f5768d..a046fa5ac 100644 --- a/packages/embarkjs/embarkjs/src/lib/blockchain.js +++ b/packages/embarkjs/embarkjs/src/lib/blockchain.js @@ -100,18 +100,23 @@ Blockchain.doConnect = function(connectionList, opts, doneCb) { const self = this; const checkConnect = (next) => { - this.blockchainConnector.getAccounts((error, _a) => { - const provider = self.blockchainConnector.getCurrentProvider(); - const connectionString = provider.host; - - if (error) this.blockchainConnector.setProvider(null); - - return next(null, { - connectionString, - error, - connected: !error + const provider = self.blockchainConnector.getCurrentProvider(); + const connectionString = provider.host; + this.blockchainConnector.getNetworkId() + .then(_id => { + next(null, { + connectionString, + error: null, + connected: true + }); + }) + .catch(error => { + next(null, { + connectionString, + error, + connected: false + }); }); - }); }; const connectWeb3 = async (next) => { @@ -171,10 +176,7 @@ Blockchain.doConnect = function(connectionList, opts, doneCb) { return doneCb(new BlockchainConnectionError(connectionErrs)); } - self.blockchainConnector.getAccounts(async (err, accounts) => { - if (err) { - return doneCb(err); - } + self.blockchainConnector.getAccounts(async (_err, accounts) => { const currentProv = self.blockchainConnector.getCurrentProvider(); if (opts.warnAboutMetamask) { // if we are using metamask, ask embark to turn on dev_funds diff --git a/packages/embarkjs/embarkjs/src/test/blockchain.js b/packages/embarkjs/embarkjs/src/test/blockchain.js index dfef1bf9f..bfc864315 100644 --- a/packages/embarkjs/embarkjs/src/test/blockchain.js +++ b/packages/embarkjs/embarkjs/src/test/blockchain.js @@ -1,4 +1,4 @@ -/* global before describe it require */ +/* global before describe it */ const {startRPCMockServer, TestProvider} = require('./helpers/blockchain'); const {assert} = require('chai'); @@ -50,7 +50,8 @@ describe('Blockchain', () => { await new Promise((resolve, reject) => { Blockchain.connect({dappConnection}, err => { 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) => { assert.strictEqual(server.visited, scenario.visited[idx]); }); diff --git a/packages/embarkjs/embarkjs/src/test/helpers/blockchain.js b/packages/embarkjs/embarkjs/src/test/helpers/blockchain.js index 7d4cd4f07..61a6ab6f7 100644 --- a/packages/embarkjs/embarkjs/src/test/helpers/blockchain.js +++ b/packages/embarkjs/embarkjs/src/test/helpers/blockchain.js @@ -27,11 +27,9 @@ const startRPCMockServer = (options = {}, callback) => { req.on('end', () => { const request = JSON.parse(body); const accountsResponse = JSON.stringify({ - "jsonrpc": "2.0", - "id": request.id, - "result": [ - "0x7c67d951b7338a96168f259a16b7ba25e7a30315" - ] + jsonrpc: "2.0", + id: request.id, + result: 1337 }); res.writeHead(200, {