Skip to content

Commit

Permalink
Fix unit test for Node.js environment
Browse files Browse the repository at this point in the history
  • Loading branch information
StefansArya committed Mar 20, 2022
1 parent 4223856 commit 72890af
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Blackprint = window.Blackprint.loadScope({

// Prepare variable
var polkadotApi, polkadotKeyring, polkadotTypes, polkadotUtilCrypto, polkadotUtil;
var polkadotExtensionDapp;

// Import for different environment
let crypto = window.crypto;
Expand All @@ -30,7 +31,7 @@ if(Blackprint.Environment.loadFromURL === false) {
if(window.Blackprint.Environment.isBrowser)
await import(path+'/node_modules/@polkadot/extension-dapp/bundle-polkadot-extension-dapp.js');

({ polkadotApi, polkadotKeyring, polkadotTypes, polkadotUtilCrypto, polkadotUtil, polkadotExtensionDapp } = window);
({ polkadotApi, polkadotKeyring, polkadotTypes, polkadotUtilCrypto, polkadotUtil } = window);
}
else{
/* Parallely load dependencies from CDN */
Expand All @@ -53,9 +54,11 @@ else{
await sf.loader.js(_remoteModule, {ordered: true});
}

({ polkadotApi, polkadotKeyring, polkadotTypes, polkadotUtilCrypto, polkadotUtil, polkadotExtensionDapp } = window);
({ polkadotApi, polkadotKeyring, polkadotTypes, polkadotUtilCrypto, polkadotUtil } = window);
}

if(window.polkadotExtensionDapp != null)
polkadotExtensionDapp = window.polkadotExtensionDapp;

// Global shared context
var Context = Blackprint.createContext('Polkadot.js');
Expand Down
8 changes: 4 additions & 4 deletions tests/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ test('Blackprint.Sketch does exist on window', async () => {
// This may took longer to finish because will also load polkadot.js's module
// -> Prefer using the bundled version to reduce load time
test("Load required modules", async () => {
// Force to browser environment
Blackprint.Environment.isBrowser = true;
Blackprint.Environment.isNode = false;

// Alternative for Blackprint.loadModuleFromURL(...);
await import("../dist/nodes-polkadotjs.mjs"); // For Browser/Node.js
await import("../dist/nodes-polkadotjs.sf.mjs"); // For Browser UI
Expand All @@ -43,10 +47,6 @@ test("Load required modules", async () => {
await Blackprint.getContext('Polkadot.js');
await new Promise(resolve => setTimeout(resolve, 1000));

// Force to browser environment
Blackprint.Environment.isBrowser = true;
Blackprint.Environment.isNode = false;

// Check if the nodes has been registered
expect(Blackprint.nodes['Polkadot.js']).toBeDefined();
});
Expand Down
6 changes: 5 additions & 1 deletion tests/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('Blackprint.Engine does exist on window', async () => {
// Throw when any error happen
MyInstance.on('error', ev => {
console.error(ev);
throw new Error("Something was wrong");
throw new Error("Something was wrong, please check the console.error");
});

// Remove log when the cable was replaced
Expand All @@ -24,6 +24,10 @@ test('Blackprint.Engine does exist on window', async () => {
// This may took longer to finish because will also load polkadot.js's module
// -> Prefer using the bundled version to reduce load time
test("Load required modules", async () => {
// Force to Node.js environment
Blackprint.Environment.isBrowser = false;
Blackprint.Environment.isNode = true;

// Alternative for Blackprint.loadModuleFromURL(...);
await import("../dist/nodes-polkadotjs.mjs"); // For Browser/Node.js

Expand Down

0 comments on commit 72890af

Please sign in to comment.