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

Refactored code in public/src/client/account/settings.js to reduce complexity. #571

Open
wants to merge 6 commits into
base: f24
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
Binary file added dump.rdb
Binary file not shown.
56 changes: 36 additions & 20 deletions public/src/client/account/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';


define('forum/account/settings', [
'forum/account/header', 'components', 'api', 'alerts', 'hooks',
], function (header, components, api, alerts, hooks) {
Expand Down Expand Up @@ -71,28 +70,45 @@ define('forum/account/settings', [
return settings;
}

// Asked chatgpt to explain what the function does and advice on how to reduce complexity.
// Will check if the key is in the config object and if it is, it will update the value of the
// key in the config object with the new value.
function saveSettings(settings) {
api.put(`/users/${ajaxify.data.uid}/settings`, { settings }).then((newSettings) => {
alerts.success('[[success:settings-saved]]');
let languageChanged = false;
for (const key in newSettings) {
if (newSettings.hasOwnProperty(key)) {
if (key === 'userLang' && config.userLang !== newSettings.userLang) {
languageChanged = true;
}
if (key === 'bootswatchSkin') {
savedSkin = newSettings.bootswatchSkin;
config.bootswatchSkin = savedSkin === 'noskin' ? '' : savedSkin;
} else if (config.hasOwnProperty(key)) {
config[key] = newSettings[key];
}
}
}
console.log('Logging Jullia Montejo');
api.put(`/users/${ajaxify.data.uid}/settings`, { settings }).then(handleNewSettings);
}

if (languageChanged && parseInt(app.user.uid, 10) === parseInt(ajaxify.data.theirid, 10)) {
window.location.reload();
// Alerts the user that the settings have been saved and updates the configuration.
function handleNewSettings(newSettings) {
console.log('Logging Jullia Montejo');
alerts.success('[[success:settings-saved]]');
processNewSettings(newSettings);
}

// Processes the new settings and updates the configuration accordingly.
function processNewSettings(newSettings) {
for (const key in newSettings) {
if (newSettings.hasOwnProperty(key)) {
checkAndUpdateConfig(key, newSettings);
}
});
}
}

// Updates the configuration based on the provided key and new settings.
function checkAndUpdateConfig(key, newSettings) {
if (key === 'userLang' && config.userLang !== newSettings.userLang) {
window.location.reload();
} else if (key === 'bootswatchSkin') {
updateSkin(newSettings.bootswatchSkin);
} else if (config.hasOwnProperty(key)) {
config[key] = newSettings[key];
}
}

// Updates the saved skin with the new skin.
function updateSkin(skin) {
savedSkin = skin;
config.bootswatchSkin = skin === 'noskin' ? '' : skin;
}

function toggleCustomRoute() {
Expand Down
81 changes: 81 additions & 0 deletions test/public/src/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

// I asked chatgpt to generate tests to specifically test the function saveSettings in the
// file public/src/client/account/settings.js

const assert = require('assert');

// Mock the implementations of the modules that are used in the file
const apiMock = {
put: (url, data) => Promise.resolve(data.settings),
};

const alertsMock = {
success: () => {},
};

const componentsMock = {
get: () => ({
find: () => ({
timeago: () => {},
}),
}),
};

const hooksMock = {
fire: () => {},
};

// Mock the dependencies by temporarily replacing them in the global scope
const originalApi = global.api;
const originalAlerts = global.alerts;
const originalComponents = global.components;
const originalHooks = global.hooks;

global.api = apiMock;
global.alerts = alertsMock;
global.components = componentsMock;
global.hooks = hooksMock;

// Import the AccountSettings module after setting up the mocks
const AccountSettings = require('../../../public/src/client/account/settings');

describe('AccountSettings - saveSettings', () => {
let savedSkin = '';

// Sets up initial environment and mock global objects before each test
beforeEach(() => {
// Start with initial skin
savedSkin = 'defaultSkin';
global.$ = require('jquery');
global.ajaxify = { data: { template: { name: 'account/settings' }, uid: 1 } };
global.config = { relative_path: '', cache_buster: '123', bootswatchSkin: '', defaultBootswatchSkin: '' };
});

// Restore the original global objects after each test
afterEach(() => {
global.api = originalApi;
global.alerts = originalAlerts;
global.components = originalComponents;
global.hooks = originalHooks;
});

it('should save settings and call API', (done) => {
const settings = { homePageRoute: 'custom', homePageCustom: 'home' };
const originalConsoleLog = console.log;
let consoleLogCalled = false;

console.log = () => {
consoleLogCalled = true;
};

AccountSettings.saveSettings(settings);

setTimeout(() => {
// Check if console log has been called
assert.ok(consoleLogCalled);
console.log = originalConsoleLog;
done();
}, 50);
});
});