Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the most inner nested function to outside to reduce number of nesting layers in file public/src/admin/dashboard/searches.js #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
36 changes: 19 additions & 17 deletions public/src/admin/dashboard/searches.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
'use strict';

define('admin/dashboard/searches', ['alerts', 'bootbox'], (alerts, bootbox) => {
const ACP = {};

ACP.init = () => {
$('#clear-search-history').on('click', () => {
bootbox.confirm('[[admin/dashboard:clear-search-history-confirm]]', function (ok) {
if (ok) {
socket.emit('admin.clearSearchHistory', function (err) {
if (err) {
return alerts.error(err);
}
ajaxify.refresh();
});
}
const createACPModule = (alert, ajaxify, socket, $) => {
const handleError = (err) => {
if (err) {
return alert.error(err);
}
ajaxify.refresh();
};
const ACP = {
init: () => {
$('#clear-search-history').on('click', () => {
bootbox.confirm('[[admin/dashboard:clear-search-history-confirm]]', function (ok) {
if (ok) {
socket.emit('admin.clearSearchHistory', handleError);
}
});
});
});
},
};
return { handleError, ACP };
};

return ACP;
});
module.exports = createACPModule;
48 changes: 48 additions & 0 deletions test/adminDashboardSearches.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const assert = require('assert');
const createACPModule = require('../public/src/admin/dashboard/searches');

describe('handleError', () => {
let handleError;
let ACP;
beforeEach(() => {
const alert = {
error: (err) => {
throw new Error(`Error: ${err.message}`);
},
};

const ajaxify = {
refresh: () => 'Refreshed!',
};

const socket = {
emit: (event, callback) => {
if (event === 'admin.clearSearchHistory') {
callback(null);
} else {
callback(new Error('Unknown event detected'));
}
},
};

const stubACPModule = createACPModule(alert, ajaxify, socket, () => {});
handleError = stubACPModule.handleError;
ACP = stubACPModule.ACP;
});

it('should call alert.error when handleError receives an error', () => {
try {
handleError(new Error('Automated Test Error'));
assert(false, 'handleError should have thrown an error');
} catch (err) {
assert.strictEqual(err.message, 'Error: Automated Test Error');
}
});

it('should return "undefined" when handleError receives no error', () => {
const result = handleError(null);
assert.strictEqual(result, undefined);
});
});
Loading