Skip to content

Commit

Permalink
Merge pull request #1139 from tupaschoal/dialog-refactor
Browse files Browse the repository at this point in the history
Standardize messageBox handling on app
  • Loading branch information
tupaschoal authored Feb 6, 2025
2 parents 022e6dc + e1c3c9b commit d559412
Show file tree
Hide file tree
Showing 44 changed files with 148 additions and 148 deletions.
27 changes: 16 additions & 11 deletions __tests__/__main__/menus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import ImportExport from '../../js/import-export.mjs';

import Store from 'electron-store';

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

const windowAux = require('../../js/window-aux.cjs');

describe('menus.js', () =>
{
const mocks = {};
Expand Down Expand Up @@ -311,7 +316,7 @@ describe('menus.js', () =>
it('Should show about message box and write to clipboard', (done) =>
{
const writeTextStub = stub(clipboard, 'writeText');
const showMessageBoxStub = stub(dialog, 'showMessageBox').resolves({response: 0});
const showMessageBoxStub = stub(windowAux, 'showDialog').resolves({response: 0});
getHelpMenuTemplate({})[4].click();
setTimeout(() =>
{
Expand All @@ -325,7 +330,7 @@ describe('menus.js', () =>
it('Should show about message box', () =>
{
const writeTextStub = stub(clipboard, 'writeText');
const showMessageBoxStub = stub(dialog, 'showMessageBox').resolves({response: 1});
const showMessageBoxStub = stub(windowAux, 'showDialog').resolves({response: 1});
getHelpMenuTemplate({})[4].click();
assert.strictEqual(showMessageBoxStub.calledOnce, true);
assert.strictEqual(writeTextStub.notCalled, true);
Expand All @@ -336,7 +341,7 @@ describe('menus.js', () =>
{
const consoleSpy = stub(console, 'log');
const writeTextStub = stub(clipboard, 'writeText');
const showMessageBoxStub = stub(dialog, 'showMessageBox').rejects({response: 1});
const showMessageBoxStub = stub(windowAux, 'showDialog').rejects({response: 1});
getHelpMenuTemplate({})[4].click();
assert.strictEqual(showMessageBoxStub.calledOnce, true);
assert.strictEqual(writeTextStub.notCalled, true);
Expand All @@ -358,7 +363,7 @@ describe('menus.js', () =>
let showMessageBoxStub;
before(() =>
{
showMessageBoxStub = stub(dialog, 'showMessageBox');
showMessageBoxStub = stub(windowAux, 'showDialog');
});

beforeEach(() =>
Expand Down Expand Up @@ -441,7 +446,7 @@ describe('menus.js', () =>
}
};
const showOpenDialogSyncStub = stub(dialog, 'showOpenDialogSync').returns(true);
const showMessageBoxSyncStub = stub(dialog, 'showMessageBoxSync').returns(0);
const showMessageBoxSyncStub = stub(windowAux, 'showDialogSync').returns(0);
mocks._importDatabaseFromFile = stub(ImportExport, 'importDatabaseFromFile').returns({
result: true,
failed: 0
Expand All @@ -466,7 +471,7 @@ describe('menus.js', () =>
}
};
const showOpenDialogSyncStub = stub(dialog, 'showOpenDialogSync').returns(true);
const showMessageBoxSyncStub = stub(dialog, 'showMessageBoxSync').returns(0);
const showMessageBoxSyncStub = stub(windowAux, 'showDialogSync').returns(0);
mocks._importDatabaseFromFile = stub(ImportExport, 'importDatabaseFromFile').returns({
result: false,
failed: 1
Expand All @@ -491,7 +496,7 @@ describe('menus.js', () =>
}
};
const showOpenDialogSyncStub = stub(dialog, 'showOpenDialogSync').returns(true);
const showMessageBoxSyncStub = stub(dialog, 'showMessageBoxSync').returns(0);
const showMessageBoxSyncStub = stub(windowAux, 'showDialogSync').returns(0);
mocks._importDatabaseFromFile = stub(ImportExport, 'importDatabaseFromFile').returns({
result: false,
failed: 0
Expand All @@ -508,7 +513,7 @@ describe('menus.js', () =>
it('Should not show dialog for importing db', () =>
{
const showOpenDialogSyncStub = stub(dialog, 'showOpenDialogSync').returns(false);
const showMessageBoxSyncStub = stub(dialog, 'showMessageBoxSync').returns(1);
const showMessageBoxSyncStub = stub(windowAux, 'showDialogSync').returns(1);
mocks._importDatabaseFromFile = stub(ImportExport, 'importDatabaseFromFile');
getEditMenuTemplate()[8].click();
assert.strictEqual(showOpenDialogSyncStub.calledOnce, true);
Expand All @@ -522,7 +527,7 @@ describe('menus.js', () =>
it('Should not show dialog for importing db', () =>
{
const showOpenDialogSyncStub = stub(dialog, 'showOpenDialogSync').returns(true);
const showMessageBoxSyncStub = stub(dialog, 'showMessageBoxSync').returns(1);
const showMessageBoxSyncStub = stub(windowAux, 'showDialogSync').returns(1);
mocks._importDatabaseFromFile = stub(ImportExport, 'importDatabaseFromFile');
getEditMenuTemplate()[8].click();
assert.strictEqual(showOpenDialogSyncStub.calledOnce, true);
Expand All @@ -535,7 +540,7 @@ describe('menus.js', () =>

it('Should not show dialog for clearing db', () =>
{
const showMessageBoxSyncStub = stub(dialog, 'showMessageBoxSync').returns(0);
const showMessageBoxSyncStub = stub(windowAux, 'showDialogSync').returns(1);
getEditMenuTemplate()[9].click();
assert.strictEqual(showMessageBoxSyncStub.calledOnce, true);
assert.strictEqual(showMessageBoxStub.notCalled, true);
Expand All @@ -553,7 +558,7 @@ describe('menus.js', () =>
}
};
const clearStoreStub = stub(Store.prototype, 'clear');
const showMessageBoxSyncStub = stub(dialog, 'showMessageBoxSync').returns(1);
const showMessageBoxSyncStub = stub(windowAux, 'showDialogSync').returns(0);
getEditMenuTemplate(mainWindow)[9].click();
assert.strictEqual(showMessageBoxSyncStub.calledOnce, true);
assert.strictEqual(showMessageBoxStub.calledOnce, true);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__renderer__/preferences.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('Test Preferences Window', () =>
}));
},
getOriginalUserPreferences: () => { return testPreferences; },
showDialogSync: () => { return new Promise((resolve) => resolve({ response: 0 })); }
showDialog: () => { return new Promise((resolve) => resolve({ response: 0 })); }
};

// Stub methods
Expand Down
12 changes: 6 additions & 6 deletions __tests__/__renderer__/window-aux.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('window-aux.cjs Testing', function()
// });
// });

describe('showDialogSync(options)', function()
describe('showDialog(options)', function()
{
it('Does not crash', async() =>
{
Expand All @@ -102,12 +102,12 @@ describe('window-aux.cjs Testing', function()
let spy;
testWindow.webContents.on('dom-ready', () =>
{
spy = sinon.spy(windowAux, 'showDialogSync');
spy = sinon.spy(windowAux, 'showDialog');

const options = {
title: 'Time to Leave',
};
windowAux.showDialogSync(options).then(() =>
windowAux.showDialog(options).then(() =>
{
return;
});
Expand All @@ -125,7 +125,7 @@ describe('window-aux.cjs Testing', function()
});
});

describe('showAlert(message)', function()
describe('showDialogSync(message)', function()
{
it('Does not crash', async() =>
{
Expand All @@ -135,8 +135,8 @@ describe('window-aux.cjs Testing', function()
let spy;
testWindow.webContents.on('dom-ready', () =>
{
spy = sinon.stub(windowAux, 'showAlert');
windowAux.showAlert('Test showAlert');
spy = sinon.stub(windowAux, 'showDialogSync');
windowAux.showDialogSync('Test showDialogSync');
});
testWindow.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) =>
{
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__renderer__/workday-waiver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('Test Workday Waiver Window', function()
'data': {}
}));
};
window.rendererApi.showDialogSync = () =>
window.rendererApi.showDialog = () =>
{
return new Promise((resolve) =>
{
Expand All @@ -190,7 +190,7 @@ describe('Test Workday Waiver Window', function()
};
window.rendererApi.showDay = showDay;

window.workdayWaiverApi.showAlert = () => {};
window.workdayWaiverApi.showDialogSync = () => {};

// Making sure the preferences are the default so the tests work as expected
savePreferences(getDefaultPreferences());
Expand Down
2 changes: 1 addition & 1 deletion js/ipc-constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const IpcConstants = Object.freeze(
SetStoreData: 'SET_STORE_DATA',
SetWaiver: 'SET_WAIVER',
SetWaiverDay: 'SET_WAIVER_DAY',
ShowAlert: 'SHOW_ALERT',
ShowDialogSync: 'SHOW_DIALOG_SYNC',
ShowDialog: 'SHOW_DIALOG',
SwitchView: 'SWITCH_VIEW',
ToggleTrayPunchTime: 'TOGGLE_TRAY_PUNCH_TIME',
Expand Down
86 changes: 39 additions & 47 deletions js/menus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import Windows from './windows.mjs';
import i18NextConfig from '../src/configs/i18next.config.mjs';
import IpcConstants from './ipc-constants.mjs';

// Allow require()
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

const WindowAux = require('./window-aux.cjs');

function getMainMenuTemplate(mainWindow)
{
return [
Expand Down Expand Up @@ -192,11 +198,9 @@ function getEditMenuTemplate(mainWindow)
if (response)
{
ImportExport.exportDatabaseToFile(response);
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), {
title: 'Time to Leave',
WindowAux.showDialog({
message: i18NextConfig.getCurrentTranslation('$Menu.database-export'),
type: 'info',
icon: appConfig.iconpath,
detail: i18NextConfig.getCurrentTranslation('$Menu.database-was-exported')
});
}
Expand Down Expand Up @@ -227,27 +231,23 @@ function getEditMenuTemplate(mainWindow)
i18NextConfig.getCurrentTranslation('$Menu.yes-please'),
i18NextConfig.getCurrentTranslation('$Menu.no-thanks')
],
defaultId: 2,
title: i18NextConfig.getCurrentTranslation('$Menu.import-database'),
message: i18NextConfig.getCurrentTranslation('$Menu.confirm-import-db')
defaultId: 1,
cancelId: 1,
message: i18NextConfig.getCurrentTranslation('$Menu.import-database'),
detail: i18NextConfig.getCurrentTranslation('$Menu.confirm-import-db')
};

const confirmation = dialog.showMessageBoxSync(
BrowserWindow.getFocusedWindow(),
options
);
const confirmation = WindowAux.showDialogSync(options);
if (confirmation === /*Yes*/ 0)
{
const importResult = ImportExport.importDatabaseFromFile(response);
// Reload only the calendar itself to avoid a flash
mainWindow.webContents.send(IpcConstants.ReloadCalendar);
if (importResult['result'])
{
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), {
title: 'Time to Leave',
WindowAux.showDialog({
message: i18NextConfig.getCurrentTranslation('$Menu.database-imported'),
type: 'info',
icon: appConfig.iconpath,
detail: i18NextConfig.getCurrentTranslation('$Menu.import-successful')
});
}
Expand All @@ -256,20 +256,18 @@ function getEditMenuTemplate(mainWindow)
const message = `${importResult['failed']}/${
importResult['total']
} ${i18NextConfig.getCurrentTranslation('$Menu.could-not-be-loaded')}`;
dialog.showMessageBoxSync({
icon: appConfig.iconpath,
WindowAux.showDialogSync({
type: 'warning',
title: i18NextConfig.getCurrentTranslation('$Menu.failed-entries'),
message: message
message: i18NextConfig.getCurrentTranslation('$Menu.failed-entries'),
detail: message
});
}
else
{
dialog.showMessageBoxSync({
icon: appConfig.iconpath,
type: 'warning',
title: i18NextConfig.getCurrentTranslation('$Menu.failed-entries'),
message: i18NextConfig.getCurrentTranslation('$Menu.something-went-wrong')
WindowAux.showDialogSync({
type: 'error',
message: i18NextConfig.getCurrentTranslation('$Menu.failed-entries'),
detail: i18NextConfig.getCurrentTranslation('$Menu.something-went-wrong')
});
}
}
Expand All @@ -283,20 +281,17 @@ function getEditMenuTemplate(mainWindow)
const options = {
type: 'question',
buttons: [
i18NextConfig.getCurrentTranslation('$Menu.cancel'),
i18NextConfig.getCurrentTranslation('$Menu.yes-please'),
i18NextConfig.getCurrentTranslation('$Menu.no-thanks')
],
defaultId: 2,
title: i18NextConfig.getCurrentTranslation('$Menu.clear-database'),
message: i18NextConfig.getCurrentTranslation('$Menu.confirm-clear-all-data')
defaultId: 1,
cancelId: 1,
message: i18NextConfig.getCurrentTranslation('$Menu.clear-database'),
detail: i18NextConfig.getCurrentTranslation('$Menu.confirm-clear-all-data')
};

const response = dialog.showMessageBoxSync(
BrowserWindow.getFocusedWindow(),
options
);
if (response === 1)
const response = WindowAux.showDialogSync(options);
if (response === 0)
{
const store = new Store();
const waivedWorkdays = new Store({ name: 'waived-workdays' });
Expand All @@ -307,11 +302,9 @@ function getEditMenuTemplate(mainWindow)
calendarStore.clear();
// Reload only the calendar itself to avoid a flash
mainWindow.webContents.send(IpcConstants.ReloadCalendar);
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), {
title: 'Time to Leave',
WindowAux.showDialog({
message: i18NextConfig.getCurrentTranslation('$Menu.clear-database'),
type: 'info',
icon: appConfig.iconpath,
detail: `\n${i18NextConfig.getCurrentTranslation('$Menu.all-clear')}`
});
}
Expand Down Expand Up @@ -376,19 +369,18 @@ function getHelpMenuTemplate()
click()
{
const detail = getDetails();
dialog
.showMessageBox(BrowserWindow.getFocusedWindow(), {
title: 'Time to Leave',
message: 'Time to Leave',
type: 'info',
icon: appConfig.iconpath,
detail: `\n${detail}`,
buttons: [
i18NextConfig.getCurrentTranslation('$Menu.copy'),
i18NextConfig.getCurrentTranslation('$Menu.ok')
],
noLink: true
})
WindowAux.showDialog({
message: 'Time to Leave',
type: 'info',
detail: `\n${detail}`,
buttons: [
i18NextConfig.getCurrentTranslation('$Menu.copy'),
i18NextConfig.getCurrentTranslation('$Menu.ok')
],
defaultId: 1,
cancelId: 1,
noLink: true
})
.then(result =>
{
const buttonId = result.response;
Expand Down
Loading

0 comments on commit d559412

Please sign in to comment.