Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"DIST_FOLDER": "../bitgesell-wallet-dist"
},
"scripts": {
"test": "eslint 'src/*.js'",
"test": "eslint 'src/*.js' test/fetch_query_cleanup_test.js && node test/fetch_query_cleanup_test.js",
"local-server": "IP=$npm_package_config_IP PORT=$npm_package_config_PORT node local-server.js",
"start": "npm test && npm run local-server",
"build": "./build.sh $npm_package_config_DIST_FOLDER",
Expand Down
1 change: 1 addition & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const fetchQuery = (url, callback, fetchParams = null, errorFunc = null, callbac
}
})
.catch((error) => {
if (callbackAlways) callbackAlways();
if (error == 'TypeError: Failed to fetch') error += '<br><br>Maybe it is CORS! Check please <a class="btn btn-sm btn-info" target="_blank" href="https://github.com/epexa/bitgesell-wallet-dist/blob/master/CORS.md#cors">manual here.</a>';
Swal.fire({
showCloseButton: true,
Expand Down
83 changes: 83 additions & 0 deletions test/fetch_query_cleanup_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vm = require('vm');

const source = fs.readFileSync(path.join(__dirname, '..', 'src', 'api.js'), 'utf8');

const createContext = (fetchImpl) => {
const swalCalls = [];
const context = {
fetch: fetchImpl,
Swal: {
fire: (params) => {
swalCalls.push(params);
},
},
};
context.global = context;
vm.createContext(context);
vm.runInContext(`${source}\nthis.fetchQuery = fetchQuery;`, context);
context.swalCalls = swalCalls;
return context;
};

const flushPromises = () => new Promise((resolve) => setImmediate(resolve));

const testRejectedFetchRunsCleanupBeforeAlert = async () => {
const events = [];
// Matches the string branch already used by the wallet's CORS-help path.
// eslint-disable-next-line prefer-promise-reject-errors
const context = createContext(() => Promise.reject('TypeError: Failed to fetch'));

context.Swal.fire = (params) => {
events.push('alert');
context.swalCalls.push(params);
};

context.fetchQuery(
'https://node.example/rpc',
() => events.push('success'),
null,
null,
() => events.push('cleanup'),
);

await flushPromises();
await flushPromises();

assert.deepStrictEqual(events, [ 'cleanup', 'alert' ]);
assert.strictEqual(context.swalCalls.length, 1);
assert.match(context.swalCalls[0].html, /Maybe it is CORS!/);
};

const testErrorResponseRunsCleanupOnce = async () => {
let cleanupCount = 0;
const context = createContext(() => Promise.resolve({
json: () => Promise.resolve({ error: { message: 'bad tx' } }),
}));

context.fetchQuery(
'https://node.example/rpc',
() => assert.fail('success callback should not run for error response'),
null,
() => ({ message: 'mapped error' }),
() => { cleanupCount++; },
);

await flushPromises();
await flushPromises();

assert.strictEqual(cleanupCount, 1);
assert.strictEqual(context.swalCalls.length, 1);
assert.strictEqual(context.swalCalls[0].html, 'mapped error');
};

(async () => {
await testRejectedFetchRunsCleanupBeforeAlert();
await testErrorResponseRunsCleanupOnce();
console.log('fetchQuery cleanup tests passed');
})().catch((error) => {
console.error(error);
process.exit(1);
});