Skip to content

Commit

Permalink
Creating an enum of IpcConstants to have a single source of truth for…
Browse files Browse the repository at this point in the history
… ipc channel strings (#1133)
  • Loading branch information
araujoarthur0 authored Jan 29, 2025
1 parent f058e89 commit 941e65e
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 114 deletions.
52 changes: 16 additions & 36 deletions __tests__/__main__/main-window.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import assert from 'assert';
import { app, BrowserWindow, ipcMain } from 'electron';
import { BrowserWindow, ipcMain } from 'electron';
import { match, spy, stub, useFakeTimers } from 'sinon';

import Notification from '../../js/notification.mjs';
Expand All @@ -16,18 +16,10 @@ import {
} from '../../js/main-window.mjs';

import UpdateManager from '../../js/update-manager.mjs';
import IpcConstants from '../../js/ipc-constants.mjs';

// Mocking USER_DATA_PATH for tests below
ipcMain.handle('USER_DATA_PATH', () =>
{
return new Promise((resolve) =>
{
resolve(app.getPath('userData'));
});
});

ipcMain.removeHandler('GET_LANGUAGE_DATA');
ipcMain.handle('GET_LANGUAGE_DATA', () => ({
ipcMain.removeHandler(IpcConstants.GetLanguageData);
ipcMain.handle(IpcConstants.GetLanguageData, () => ({
'language': 'en',
'data': {}
}));
Expand Down Expand Up @@ -74,10 +66,10 @@ describe('main-window.mjs', () =>
*/
const mainWindow = getMainWindow();
assert.strictEqual(mainWindow instanceof BrowserWindow, true);
assert.strictEqual(ipcMain.listenerCount('TOGGLE_TRAY_PUNCH_TIME'), 1);
assert.strictEqual(ipcMain.listenerCount('RESIZE_MAIN_WINDOW'), 1);
assert.strictEqual(ipcMain.listenerCount('SWITCH_VIEW'), 1);
assert.strictEqual(ipcMain.listenerCount('RECEIVE_LEAVE_BY'), 1);
assert.strictEqual(ipcMain.listenerCount(IpcConstants.ToggleTrayPunchTime), 1);
assert.strictEqual(ipcMain.listenerCount(IpcConstants.ResizeMainWindow), 1);
assert.strictEqual(ipcMain.listenerCount(IpcConstants.SwitchView), 1);
assert.strictEqual(ipcMain.listenerCount(IpcConstants.ReceiveLeaveBy), 1);
assert.strictEqual(mainWindow.listenerCount('minimize'), 2);
assert.strictEqual(mainWindow.listenerCount('close'), 2);
assert.strictEqual(loadFileSpy.calledOnce, true);
Expand All @@ -87,7 +79,7 @@ describe('main-window.mjs', () =>
});
});

describe('emit RESIZE_MAIN_WINDOW', function()
describe('emit IpcConstants.ResizeMainWindow', function()
{
it('It should resize window', (done) =>
{
Expand All @@ -101,7 +93,7 @@ describe('main-window.mjs', () =>
// Wait a bit for values to accomodate
await new Promise(res => setTimeout(res, 500));

assert.strictEqual(ipcMain.emit('RESIZE_MAIN_WINDOW'), true);
assert.strictEqual(ipcMain.emit(IpcConstants.ResizeMainWindow), true);
const windowSize = mainWindow.getSize();
assert.strictEqual(windowSize.length, 2);

Expand All @@ -127,7 +119,7 @@ describe('main-window.mjs', () =>
// Wait a bit for values to accomodate
await new Promise(res => setTimeout(res, 500));

assert.strictEqual(ipcMain.emit('RESIZE_MAIN_WINDOW'), true);
assert.strictEqual(ipcMain.emit(IpcConstants.ResizeMainWindow), true);
const windowSize = mainWindow.getSize();
assert.strictEqual(windowSize.length, 2);

Expand All @@ -139,7 +131,7 @@ describe('main-window.mjs', () =>
});
});

describe('emit SWITCH_VIEW', () =>
describe('emit IpcConstants.SwitchView', () =>
{
it('It should send new event to ipcRenderer', (done) =>
{
Expand All @@ -158,22 +150,18 @@ describe('main-window.mjs', () =>
await new Promise(res => setTimeout(res, 500));

const windowStub = stub(mainWindow.webContents, 'send').callsFake((event, savedPreferences) =>
{
ipcMain.emit('FINISH_TEST', event, savedPreferences);
});
ipcMain.on('FINISH_TEST', (event, savedPreferences) =>
{
assert.strictEqual(windowStub.calledOnce, true);
assert.strictEqual(savedPreferences['view'], 'day');
done();
});
ipcMain.emit('SWITCH_VIEW');
ipcMain.emit(IpcConstants.SwitchView);
windowStub.restore();
});
});
});

describe('emit RECEIVE_LEAVE_BY', () =>
describe('emit IpcConstants.ReceiveLeaveBy', () =>
{
it('Should not show notification when notifications is not sent', (done) =>
{
Expand All @@ -188,7 +176,7 @@ describe('main-window.mjs', () =>
{
return false;
});
ipcMain.emit('RECEIVE_LEAVE_BY', {}, undefined);
ipcMain.emit(IpcConstants.ReceiveLeaveBy, {}, undefined);
assert.strictEqual(Notification.createLeaveNotification.calledOnce, true);
Notification.createLeaveNotification.restore();
done();
Expand Down Expand Up @@ -216,7 +204,7 @@ describe('main-window.mjs', () =>
{
const now = new Date();
ipcMain.emit(
'RECEIVE_LEAVE_BY',
IpcConstants.ReceiveLeaveBy,
{},
`0${now.getHours()}`.slice(-2) + ':' + `0${now.getMinutes()}`.slice(-2)
);
Expand All @@ -238,10 +226,6 @@ describe('main-window.mjs', () =>
mainWindow.on('ready-to-show', () =>
{
showSpy.callsFake(() =>
{
ipcMain.emit('FINISH_TEST');
});
ipcMain.on('FINISH_TEST', () =>
{
assert.strictEqual(showSpy.calledTwice, true);
showSpy.resetBehavior();
Expand All @@ -264,10 +248,6 @@ describe('main-window.mjs', () =>
mainWindow.on('ready-to-show', () =>
{
const trayStub = stub(global.tray, 'popUpContextMenu').callsFake(() =>
{
ipcMain.emit('FINISH_TEST');
});
ipcMain.on('FINISH_TEST', () =>
{
assert.strictEqual(trayStub.calledOnce, true);
trayStub.restore();
Expand Down
13 changes: 7 additions & 6 deletions __tests__/__main__/menus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../../js/menus.mjs';

import i18NextConfig from '../../src/configs/i18next.config.mjs';
import IpcConstants from '../../js/ipc-constants.mjs';
import Windows from '../../js/windows.mjs';
import Notification from '../../js/notification.mjs';
import UpdateManager from '../../js/update-manager.mjs';
Expand Down Expand Up @@ -116,7 +117,7 @@ describe('menus.js', () =>
webContents: {
send: (key) =>
{
assert.strictEqual(key, 'PUNCH_DATE');
assert.strictEqual(key, IpcConstants.PunchDate);
}
}
};
Expand Down Expand Up @@ -177,7 +178,7 @@ describe('menus.js', () =>
webContents: {
send: (key) =>
{
assert.strictEqual(key, 'PUNCH_DATE');
assert.strictEqual(key, IpcConstants.PunchDate);
}
}
};
Expand Down Expand Up @@ -435,7 +436,7 @@ describe('menus.js', () =>
webContents: {
send: (key) =>
{
assert.strictEqual(key, 'RELOAD_CALENDAR');
assert.strictEqual(key, IpcConstants.ReloadCalendar);
}
}
};
Expand All @@ -460,7 +461,7 @@ describe('menus.js', () =>
webContents: {
send: (key) =>
{
assert.strictEqual(key, 'RELOAD_CALENDAR');
assert.strictEqual(key, IpcConstants.ReloadCalendar);
}
}
};
Expand All @@ -485,7 +486,7 @@ describe('menus.js', () =>
webContents: {
send: (key) =>
{
assert.strictEqual(key, 'RELOAD_CALENDAR');
assert.strictEqual(key, IpcConstants.ReloadCalendar);
}
}
};
Expand Down Expand Up @@ -547,7 +548,7 @@ describe('menus.js', () =>
webContents: {
send: (key) =>
{
assert.strictEqual(key, 'RELOAD_CALENDAR');
assert.strictEqual(key, IpcConstants.ReloadCalendar);
}
}
};
Expand Down
3 changes: 2 additions & 1 deletion __tests__/__renderer__/notification-channel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../../__mocks__/jquery.mjs';
import assert from 'assert';

import { searchLeaveByElement } from '../../renderer/notification-channel.js';
import IpcConstants from '../../js/ipc-constants.mjs';

describe('Notifications channel', () =>
{
Expand All @@ -16,7 +17,7 @@ describe('Notifications channel', () =>
sender: {
send: (channel, value) =>
{
assert.strictEqual(channel, 'RECEIVE_LEAVE_BY');
assert.strictEqual(channel, IpcConstants.ReceiveLeaveBy);
assert.strictEqual(value, '12:12');
done();
}
Expand Down
37 changes: 37 additions & 0 deletions js/ipc-constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const IpcConstants = Object.freeze(
{
ChangeLanguage: 'CHANGE_LANGUAGE',
ComputeAllTimeBalanceUntil: 'COMPUTE_ALL_TIME_BALANCE_UNTIL',
DeleteStoreData: 'DELETE_STORE_DATA',
DeleteWaiver: 'DELETE_WAIVER',
GetCountries: 'GET_COUNTRIES',
GetHolidays: 'GET_HOLIDAYS',
GetLanguageData: 'GET_LANGUAGE_DATA',
GetLeaveBy: 'GET_LEAVE_BY',
GetRegions: 'GET_REGIONS',
GetStates: 'GET_STATES',
GetStoreContents: 'GET_STORE_CONTENTS',
GetWaiverDay: 'GET_WAIVER_DAY',
GetWaiverStoreContents: 'GET_WAIVER_STORE_CONTENTS',
HasWaiver: 'HAS_WAIVER',
PreferenceSaveDataNeeded: 'PREFERENCE_SAVE_DATA_NEEDED',
PreferencesSaved: 'PREFERENCES_SAVED',
PunchDate: 'PUNCH_DATE',
ReceiveLeaveBy: 'RECEIVE_LEAVE_BY',
RefreshOnDayChange: 'REFRESH_ON_DAY_CHANGE',
ReloadCalendar: 'RELOAD_CALENDAR',
ReloadTheme: 'RELOAD_THEME',
ResizeMainWindow: 'RESIZE_MAIN_WINDOW',
SetStoreData: 'SET_STORE_DATA',
SetWaiver: 'SET_WAIVER',
SetWaiverDay: 'SET_WAIVER_DAY',
ShowAlert: 'SHOW_ALERT',
ShowDialog: 'SHOW_DIALOG',
SwitchView: 'SWITCH_VIEW',
ToggleTrayPunchTime: 'TOGGLE_TRAY_PUNCH_TIME',
WaiverSaved: 'WAIVER_SAVED',
});

export default IpcConstants;
15 changes: 8 additions & 7 deletions js/main-window.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Notification from './notification.mjs';
import UpdateManager from './update-manager.mjs';
import { getDefaultWidthHeight, getUserPreferences, switchCalendarView } from './user-preferences.mjs';
import i18NextConfig from '../src/configs/i18next.config.mjs';
import IpcConstants from './ipc-constants.mjs';

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
Expand Down Expand Up @@ -98,35 +99,35 @@ function createWindow()
// and load the main html of the app as the default window
mainWindow.loadFile(path.join(rootDir, 'src/calendar.html'));

ipcMain.on('TOGGLE_TRAY_PUNCH_TIME', (_event, arg) =>
ipcMain.on(IpcConstants.ToggleTrayPunchTime, (_event, arg) =>
{
const contextMenuTemplate = getContextMenuTemplate(mainWindow);
contextMenuTemplate[0].enabled = arg;
global.contextMenu = Menu.buildFromTemplate(contextMenuTemplate);
global.tray.setContextMenu(global.contextMenu);
});

ipcMain.on('RESIZE_MAIN_WINDOW', () =>
ipcMain.on(IpcConstants.ResizeMainWindow, () =>
{
const widthHeight = getDefaultWidthHeight();
mainWindow.setSize(widthHeight.width, widthHeight.height);
});

ipcMain.on('SWITCH_VIEW', () =>
ipcMain.on(IpcConstants.SwitchView, () =>
{
const preferences = switchCalendarView();
mainWindow.webContents.send('PREFERENCES_SAVED', preferences);
mainWindow.webContents.send(IpcConstants.PreferencesSaved, preferences);
});

ipcMain.on('RECEIVE_LEAVE_BY', (event, element) =>
ipcMain.on(IpcConstants.ReceiveLeaveBy, (event, element) =>
{
const notification = Notification.createLeaveNotification(element);
if (notification) notification.show();
});

leaveByInterval = setInterval(() =>
{
mainWindow.webContents.send('GET_LEAVE_BY');
mainWindow.webContents.send(IpcConstants.GetLeaveBy);
}, 60 * 1000);

global.tray = new Tray(appConfig.trayIcon);
Expand Down Expand Up @@ -174,7 +175,7 @@ function createWindow()
const theme = savedPreferences['theme'];
if (theme === 'system-default')
{
mainWindow.webContents.send('RELOAD_THEME', theme);
mainWindow.webContents.send(IpcConstants.ReloadTheme, theme);
}
});
}
Expand Down
11 changes: 6 additions & 5 deletions js/menus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import UpdateManager from './update-manager.mjs';
import { getUserPreferences, savePreferences } from './user-preferences.mjs';
import Windows from './windows.mjs';
import i18NextConfig from '../src/configs/i18next.config.mjs';
import IpcConstants from './ipc-constants.mjs';

function getMainMenuTemplate(mainWindow)
{
Expand Down Expand Up @@ -46,7 +47,7 @@ function getContextMenuTemplate(mainWindow)
{
const now = new Date();

mainWindow.webContents.send('PUNCH_DATE');
mainWindow.webContents.send(IpcConstants.PunchDate);
// Slice keeps "HH:MM" part of "HH:MM:SS GMT+HHMM (GMT+HH:MM)" time string
Notification.createNotification(
`${i18NextConfig.getCurrentTranslation(
Expand Down Expand Up @@ -81,7 +82,7 @@ function getDockMenuTemplate(mainWindow)
{
const now = new Date();

mainWindow.webContents.send('PUNCH_DATE');
mainWindow.webContents.send(IpcConstants.PunchDate);
// Slice keeps "HH:MM" part of "HH:MM:SS GMT+HHMM (GMT+HH:MM)" time string
Notification.createNotification(
`${i18NextConfig.getCurrentTranslation(
Expand Down Expand Up @@ -157,7 +158,7 @@ function getEditMenuTemplate(mainWindow)
if (savedPreferences !== null)
{
savePreferences(savedPreferences);
mainWindow.webContents.send('PREFERENCES_SAVED', savedPreferences);
mainWindow.webContents.send(IpcConstants.PreferencesSaved, savedPreferences);
}
});
global.prefWindow.webContents.on('before-input-event', (event, input) =>
Expand Down Expand Up @@ -239,7 +240,7 @@ function getEditMenuTemplate(mainWindow)
{
const importResult = ImportExport.importDatabaseFromFile(response);
// Reload only the calendar itself to avoid a flash
mainWindow.webContents.send('RELOAD_CALENDAR');
mainWindow.webContents.send(IpcConstants.ReloadCalendar);
if (importResult['result'])
{
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), {
Expand Down Expand Up @@ -305,7 +306,7 @@ function getEditMenuTemplate(mainWindow)
waivedWorkdays.clear();
calendarStore.clear();
// Reload only the calendar itself to avoid a flash
mainWindow.webContents.send('RELOAD_CALENDAR');
mainWindow.webContents.send(IpcConstants.ReloadCalendar);
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), {
title: 'Time to Leave',
message: i18NextConfig.getCurrentTranslation('$Menu.clear-database'),
Expand Down
3 changes: 2 additions & 1 deletion js/saved-preferences.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { app, ipcMain } from 'electron';

import i18NextConfig from '../src/configs/i18next.config.mjs';
import IpcConstants from './ipc-constants.mjs';

let savedPreferences = null;

Expand All @@ -11,7 +12,7 @@ function getSavedPreferences()
return savedPreferences;
}

ipcMain.on('PREFERENCE_SAVE_DATA_NEEDED', (event, preferences) =>
ipcMain.on(IpcConstants.PreferenceSaveDataNeeded, (event, preferences) =>
{
savedPreferences = preferences;
app.setLoginItemSettings({
Expand Down
Loading

0 comments on commit 941e65e

Please sign in to comment.