From c4ca4e52b88ce2ebec5c7ae20b47f5498635970c Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 10 Jun 2018 12:11:34 -0400 Subject: [PATCH] support embarkjs in the tests --- js/embark.js | 20 +++++++++++++------ lib/tests/test.js | 13 ++++++++---- .../test_app/test/another_storage_spec.js | 5 +++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/js/embark.js b/js/embark.js index 19fe6d88..4412a5d5 100644 --- a/js/embark.js +++ b/js/embark.js @@ -1,9 +1,14 @@ var EmbarkJS = { - onReady: __embarkContext.execWhenReady + onReady: function(cb) { + if (typeof (__embarkContext) === 'undefined') { + return cb(); + } + return __embarkContext.execWhenReady(cb); + } }; -EmbarkJS.isNewWeb3 = function() { - var _web3 = new Web3(); +EmbarkJS.isNewWeb3 = function(web3Obj) { + var _web3 = web3Obj || (new Web3()); if (typeof(_web3.version) === "string") { return true; } @@ -17,22 +22,24 @@ EmbarkJS.Contract = function(options) { this.abi = options.abi; this.address = options.address; + this.gas = options.gas; this.code = '0x' + options.code; //this.web3 = options.web3 || web3; this.web3 = options.web3; if (!this.web3 && typeof ('web3') !== 'undefined') { this.web3 = web3; - } else { + } else if (!this.web3) { this.web3 = window.web3; } - if (EmbarkJS.isNewWeb3()) { + if (EmbarkJS.isNewWeb3(this.web3)) { ContractClass = new this.web3.eth.Contract(this.abi, this.address); ContractClass.setProvider(this.web3.currentProvider); ContractClass.options.data = this.code; ContractClass.options.from = this.from; ContractClass.abi = ContractClass.options.abi; ContractClass.address = this.address; + ContractClass.gas = this.gas; let originalMethods = Object.keys(ContractClass); @@ -386,4 +393,5 @@ EmbarkJS.Utils = { } }; -export default EmbarkJS; +//export default EmbarkJS; +module.exports = EmbarkJS; diff --git a/lib/tests/test.js b/lib/tests/test.js index a6789da9..4ebcfddd 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -8,6 +8,8 @@ const cloneDeep = require('clone-deep'); const AccountParser = require('../contracts/accountParser'); const Provider = require('../contracts/provider'); +const EmbarkJS = require('../../js/embark'); + function getSimulator() { try { return require('ganache-cli'); @@ -241,8 +243,10 @@ class Test { } else { data = self.contracts[contractName].options.data; } - Object.assign(self.contracts[contractName], new self.web3.eth.Contract(contract.abiDefinition, contract.deployedAddress, - {from: self.web3.eth.defaultAccount, gas: 6000000})); + //Object.assign(self.contracts[contractName], new self.web3.eth.Contract(contract.abiDefinition, contract.deployedAddress, + // {from: self.web3.eth.defaultAccount, gas: 6000000})); + Object.assign(self.contracts[contractName], new EmbarkJS.Contract({abi: contract.abiDefinition, address: contract.deployedAddress, from: self.web3.eth.defaultAccount, gas: 6000000, web3: self.web3})); + self.contracts[contractName].address = contract.deployedAddress; if (self.contracts[contractName].options) { self.contracts[contractName].options.from = self.contracts[contractName].options.from || self.web3.eth.defaultAccount; @@ -288,8 +292,9 @@ class Test { contract = this.engine.contractsManager.contracts[contractNames[0]]; } } - this.contracts[contractName] = new this.web3.eth.Contract(contract.abiDefinition, contract.address, - {from: this.web3.eth.defaultAccount, gas: 6000000}); + //this.contracts[contractName] = new this.web3.eth.Contract(contract.abiDefinition, contract.address, + // {from: this.web3.eth.defaultAccount, gas: 6000000}); + this.contracts[contractName] = new EmbarkJS.Contract({abi: contract.abiDefinition, address: contract.address, from: this.web3.eth.defaultAccount, gas: 6000000, web3: this.web3}); this.contracts[contractName].address = contract.address; this.contracts[contractName].options.data = contract.code; this.web3.eth.getAccounts().then((accounts) => { diff --git a/test_apps/test_app/test/another_storage_spec.js b/test_apps/test_app/test/another_storage_spec.js index 8ebc9e3f..5bdc738c 100644 --- a/test_apps/test_app/test/another_storage_spec.js +++ b/test_apps/test_app/test/another_storage_spec.js @@ -40,6 +40,11 @@ contract("AnotherStorage", function() { assert.equal(result.toString(), SimpleStorage.options.address); }); + it("set SimpleStorage address with alternative syntax", async function() { + let result = await AnotherStorage.simpleStorageAddress(); + assert.equal(result.toString(), SimpleStorage.options.address); + }); + it('should set the balance correctly', async function () { const balance = await web3.eth.getBalance(accounts[0]); assert.ok(balance < 5000000000000000000);