diff --git a/tests/js/add-monitors-probe-error.test.js b/tests/js/add-monitors-probe-error.test.js new file mode 100644 index 0000000000..36178fadc4 --- /dev/null +++ b/tests/js/add-monitors-probe-error.test.js @@ -0,0 +1,61 @@ +'use strict'; + +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const vm = require('vm'); + +const src = fs.readFileSync( + path.join(__dirname, '../../web/skins/classic/views/js/add_monitors.js'), 'utf8'); + +function load() { + const state = {tableCalls: [], alerts: [], opts: null}; + const fakeEl = { + on: () => {}, + each: () => {}, + ready: () => {}, + show: () => {}, + bootstrapTable: (cmd) => state.tableCalls.push(cmd), + }; + const sandbox = { + $j: Object.assign(function() { + return fakeEl; + }, { + ajax: (opts) => { + state.opts = opts; + }, + each: (arr, fn) => (arr || []).forEach((v, i) => fn(i, v)), + }), + document: {getElementById: () => ({addEventListener() {}})}, + window: {}, + thisUrl: 'index.php', + zmAuth: {}, + setCookie: () => {}, + logAjaxFail: () => {}, + alert: (message) => state.alerts.push(message), + console: {log: () => {}}, + }; + vm.createContext(sandbox); + vm.runInContext(src, sandbox); + sandbox.table = fakeEl; + return {sandbox, state}; +} + +{ + const {sandbox, state} = load(); + sandbox.ajaxRequest({data: {}, success: () => { + throw new Error('params.success must not be called for result=Error'); + }}); + state.opts.success({result: 'Error', message: 'Insufficient permissions'}); + assert.ok(state.tableCalls.includes('hideLoading')); + assert.deepStrictEqual(state.alerts, ['Insufficient permissions']); + console.log('ok - a result=Error answer stops the loading indicator'); +} + +{ + const {sandbox, state} = load(); + sandbox.ajaxRequest({data: {}}); + state.opts.error({status: 500, statusText: 'Internal Server Error'}); + assert.ok(state.tableCalls.includes('hideLoading')); + console.log('ok - a failed probe stops the loading indicator'); +} diff --git a/web/skins/classic/views/js/add_monitors.js b/web/skins/classic/views/js/add_monitors.js index 79d76294b8..2d73144df5 100644 --- a/web/skins/classic/views/js/add_monitors.js +++ b/web/skins/classic/views/js/add_monitors.js @@ -26,6 +26,7 @@ function probe(params) { timeout: 0, success: function(data) { if (data.result == 'Error') { + table.bootstrapTable('hideLoading'); alert(data.message); return; } @@ -39,6 +40,7 @@ function probe(params) { } }, error: function(jqXHR) { + table.bootstrapTable('hideLoading'); console.log("error", jqXHR); //logAjaxFail(jqXHR); //$j('#eventTable').bootstrapTable('refresh');