Skip to content

Commit 471a33a

Browse files
emizzle0x-r4bbit
authored andcommitted
chore(@embark/blockchain): Add unit tests to blockchain stack component
fix(@embark/blockchain): Add callback to `blockchain:node:register` and `blockchain:client:register` Add unit tests for the stack/blockchain and update supporting API documentation in the Wiki. Add injectables `Web3` and `warnIfPackageNotDefinedLocally` to stack/blockchain so that those functions can be tested properly. Update stack/blockchain dependencies in `package.json`.
1 parent 56d5b45 commit 471a33a

File tree

7 files changed

+557
-22
lines changed

7 files changed

+557
-22
lines changed

packages/core/console/test/console.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('core/console', () => {
1616

1717
beforeEach(() => {
1818
console = new Console(embark, {
19-
ipc: embark.config.ipc,
19+
ipc: embark.ipc,
2020
events: embark.events,
2121
plugins: embark.plugins,
2222
logger: embark.logger,

packages/stack/blockchain/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
"../../../.eslintrc.json",
5050
"plugin:jest/recommended",
5151
"plugin:jest/style"
52-
]
52+
],
53+
"rules": {
54+
"jest/expect-expect": "off"
55+
}
5356
},
5457
"dependencies": {
5558
"@babel/runtime-corejs3": "7.8.4",
@@ -59,6 +62,7 @@
5962
"embark-core": "^5.3.0-nightly.11",
6063
"embark-i18n": "^5.3.0-nightly.5",
6164
"embark-logger": "^5.3.0-nightly.6",
65+
"embark-testing": "^5.3.0-nightly.6",
6266
"embark-utils": "^5.3.0-nightly.7",
6367
"web3": "1.2.6"
6468
},
@@ -72,6 +76,7 @@
7276
"jest": "25.1.0",
7377
"npm-run-all": "4.1.5",
7478
"rimraf": "3.0.0",
79+
"sinon": "7.4.2",
7580
"tslint": "5.20.1",
7681
"typescript": "3.7.2"
7782
},
@@ -95,4 +100,4 @@
95100
]
96101
}
97102
}
98-
}
103+
}

packages/stack/blockchain/src/index.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export default class Blockchain {
1818
this.startedClient = null;
1919
this.plugins = options.plugins;
2020
this.blockchainClients = {};
21+
this.warnIfPackageNotDefinedLocally = options.warnIfPackageNotDefinedLocally ?? warnIfPackageNotDefinedLocally;
22+
this.Web3 = options.Web3 ?? Web3;
2123

2224
this.registerConsoleCommands();
2325

@@ -30,17 +32,17 @@ export default class Blockchain {
3032
}
3133

3234
this.blockchainNodes = {};
33-
this.events.setCommandHandler("blockchain:node:register", (clientName, clientFunctions) => {
35+
this.events.setCommandHandler("blockchain:node:register", (clientName, clientFunctions, cb = () => {}) => {
3436
const {isStartedFn, launchFn, stopFn, provider} = clientFunctions;
3537

3638
if (!isStartedFn) {
37-
throw new Error(`Blockchain client '${clientName}' must be registered with an 'isStarted' function, client not registered.`);
39+
return cb(`Blockchain client '${clientName}' must be registered with an 'isStarted' function, client not registered.`);
3840
}
3941
if (!launchFn) {
40-
throw new Error(`Blockchain client '${clientName}' must be registered with a 'launchFn' function, client not registered.`);
42+
return cb(`Blockchain client '${clientName}' must be registered with a 'launchFn' function, client not registered.`);
4143
}
4244
if (!stopFn) {
43-
throw new Error(`Blockchain client '${clientName}' must be registered with a 'stopFn' function, client not registered.`);
45+
return cb(`Blockchain client '${clientName}' must be registered with a 'stopFn' function, client not registered.`);
4446
}
4547
if (!provider) {
4648
// Set default provider function
@@ -50,6 +52,7 @@ export default class Blockchain {
5052
}
5153

5254
this.blockchainNodes[clientName] = clientFunctions;
55+
cb();
5356
});
5457

5558
this.events.setCommandHandler("blockchain:node:start", (blockchainConfig, cb) => {
@@ -135,8 +138,9 @@ export default class Blockchain {
135138
cb(null, this.getProviderFromTemplate(this.blockchainConfig.endpoint));
136139
});
137140

138-
this.events.setCommandHandler("blockchain:client:register", (clientName, getProviderFunction) => {
141+
this.events.setCommandHandler("blockchain:client:register", (clientName, getProviderFunction, cb = () => {}) => {
139142
this.blockchainClients[clientName] = getProviderFunction;
143+
cb();
140144
});
141145

142146
this.events.setCommandHandler("blockchain:client:provider", async (clientName, cb) => {
@@ -161,26 +165,26 @@ export default class Blockchain {
161165
this.blockchainApi.registerRequests("ethereum");
162166

163167
if (this.blockchainConfig.enabled && this.blockchainConfig.client === "geth") {
164-
warnIfPackageNotDefinedLocally("embark-geth", this.embark.logger.warn.bind(this.embark.logger), this.embark.config.embarkConfig);
168+
this.warnIfPackageNotDefinedLocally("embark-geth", this.embark.logger.warn.bind(this.embark.logger), this.embark.config.embarkConfig);
165169
}
166170
if (this.blockchainConfig.enabled && this.blockchainConfig.client === "parity") {
167-
warnIfPackageNotDefinedLocally("embark-parity", this.embark.logger.warn.bind(this.embark.logger), this.embark.config.embarkConfig);
171+
this.warnIfPackageNotDefinedLocally("embark-parity", this.embark.logger.warn.bind(this.embark.logger), this.embark.config.embarkConfig);
168172
}
169173
}
170174

171175
getProviderFromTemplate(endpoint) {
172176
if (endpoint.startsWith('ws')) {
173-
return new Web3.providers.WebsocketProvider(endpoint, {
177+
return new this.Web3.providers.WebsocketProvider(endpoint, {
174178
headers: { Origin: constants.embarkResourceOrigin }
175179
});
176180
}
177-
const web3 = new Web3(endpoint);
181+
const web3 = new this.Web3(endpoint);
178182
return web3.currentProvider;
179183
}
180184

181185
async addArtifactFile(_params, cb) {
182186
if (!this.blockchainConfig.enabled) {
183-
cb();
187+
return cb();
184188
}
185189

186190
try {

0 commit comments

Comments
 (0)