Skip to content

Commit

Permalink
Pass preferences to BrowserWindows via command line arguments (#1131)
Browse files Browse the repository at this point in the history
* Passing original preferences to preferences window on argv for less ipc dependencies on startup

* Creating a renderer-api file for reuse

* Passing original preferences to workday-waiver api

* Passing original preferences to calendar window
  • Loading branch information
araujoarthur0 authored Jan 28, 2025
1 parent 4fcf481 commit f058e89
Show file tree
Hide file tree
Showing 25 changed files with 232 additions and 325 deletions.
37 changes: 0 additions & 37 deletions __tests__/__main__/user-preferences.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
getPreferencesFilePath,
getUserLanguage,
getUserPreferences,
getUserPreferencesPromise,
notificationIsEnabled,
repetitionIsEnabled,
resetPreferences,
Expand All @@ -29,14 +28,6 @@ function setNewPreference(preference, value)
savePreferences(preferences);
}

function mockGetPreferencesFilePathPromise(path)
{
return new Promise((resolve) =>
{
resolve(path);
});
}

describe('Preferences Main', () =>
{
// Remove preferences file to guarantee equal execution of tests
Expand Down Expand Up @@ -443,34 +434,6 @@ describe('Preferences Main', () =>
}
});

describe('getUserPreferencesPromise()', () =>
{
before(() =>
{
fs.writeFileSync('./dummy_file.txt', 'This should be tried to be parsed and fail');
});

it('Should return a promise', () =>
{
assert.strictEqual(getUserPreferencesPromise() instanceof Promise, true);
});

it('Should resolve promise to empty if file is broken', async() =>
{
assert.deepStrictEqual(await getUserPreferencesPromise(mockGetPreferencesFilePathPromise('./')), {});
});

it('Should resolve promise to default preferences if file is unparseable', async() =>
{
assert.deepStrictEqual(await getUserPreferencesPromise(mockGetPreferencesFilePathPromise('./dummy_file.txt')), getDefaultPreferences());
});

after(() =>
{
fs.unlinkSync('./dummy_file.txt', () => {});
});
});

describe('App config languages', () =>
{
it('getLanguageMap() should have language code keys', () =>
Expand Down
15 changes: 9 additions & 6 deletions __tests__/__renderer__/classes/BaseCalendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getUserPreferences,
resetPreferences,
savePreferences,
showDay,
switchCalendarView
} from '../../../js/user-preferences.mjs';
import TimeBalance from '../../../js/time-balance.mjs';
Expand Down Expand Up @@ -45,40 +46,42 @@ describe('BaseCalendar.js', () =>
ExtendedClass.prototype._getTargetDayForAllTimeBalance = () => {};

// Mocked APIs from the preload script of the calendar window
window.mainApi = calendarApi;
window.calendarApi = calendarApi;
window.rendererApi = {};

window.mainApi.computeAllTimeBalanceUntilPromise = (targetDate) =>
window.calendarApi.computeAllTimeBalanceUntilPromise = (targetDate) =>
{
return TimeBalance.computeAllTimeBalanceUntilAsync(targetDate);
};

window.mainApi.switchView = () =>
window.calendarApi.switchView = () =>
{
switchCalendarView();
};

window.mainApi.getStoreContents = () =>
window.calendarApi.getStoreContents = () =>
{
return new Promise((resolve) =>
{
resolve(calendarStore.store);
});
};
window.mainApi.getWaiverStoreContents = () =>
window.rendererApi.getWaiverStoreContents = () =>
{
return new Promise((resolve) =>
{
resolve(waivedWorkdays.store);
});
};
window.mainApi.setStoreData = (key, contents) =>
window.calendarApi.setStoreData = (key, contents) =>
{
calendarStore.set(key, contents);
return new Promise((resolve) =>
{
resolve(true);
});
};
window.rendererApi.showDay = showDay;
});

describe('constructor', () =>
Expand Down
20 changes: 10 additions & 10 deletions __tests__/__renderer__/classes/CalendarFactory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ describe('CalendarFactory', () =>
before(() =>
{
// Mocked APIs from the preload script of the calendar window
window.mainApi = calendarApi;
window.calendarApi = calendarApi;

window.mainApi.resizeMainWindow = stub();
window.calendarApi.resizeMainWindow = stub();

Object.setPrototypeOf(DayCalendar, stub());
Object.setPrototypeOf(MonthCalendar, stub());
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('CalendarFactory', () =>

it('Should return new calendar with resizing if passing in an instance that is not a DayCalendar', async() =>
{
window.mainApi.resizeMainWindow.resetHistory();
window.calendarApi.resizeMainWindow.resetHistory();
let calls = 0;
const testCalendar = {
constructor: {
Expand All @@ -76,17 +76,17 @@ describe('CalendarFactory', () =>
}, {}, testCalendar);
assert.strictEqual(calendar instanceof DayCalendar, true);
assert.strictEqual(calls, 0);
assert.strictEqual(window.mainApi.resizeMainWindow.calledOnce, true);
assert.strictEqual(window.calendarApi.resizeMainWindow.calledOnce, true);
});

it('Should return new calendar without resizing if passing in an undefined instance', async() =>
{
window.mainApi.resizeMainWindow.resetHistory();
window.calendarApi.resizeMainWindow.resetHistory();
const calendar = await CalendarFactory.getInstance({
view: 'day',
}, {}, undefined);
assert.strictEqual(calendar instanceof DayCalendar, true);
assert.strictEqual(window.mainApi.resizeMainWindow.notCalled, true);
assert.strictEqual(window.calendarApi.resizeMainWindow.notCalled, true);
});
});

Expand All @@ -112,17 +112,17 @@ describe('CalendarFactory', () =>

it('Should return new calendar without resizing if passing in an undefined instance', async() =>
{
window.mainApi.resizeMainWindow.resetHistory();
window.calendarApi.resizeMainWindow.resetHistory();
const calendar = await CalendarFactory.getInstance({
view: 'month',
}, {}, undefined);
assert.strictEqual(calendar instanceof MonthCalendar, true);
assert.strictEqual(window.mainApi.resizeMainWindow.notCalled, true);
assert.strictEqual(window.calendarApi.resizeMainWindow.notCalled, true);
});

it('Should return new calendar with resizing if passing in an instance that is not a MonthCalendar', async() =>
{
window.mainApi.resizeMainWindow.resetHistory();
window.calendarApi.resizeMainWindow.resetHistory();
let calls = 0;
const testCalendar = {
constructor: {
Expand All @@ -137,7 +137,7 @@ describe('CalendarFactory', () =>
}, {}, testCalendar);
assert.strictEqual(calendar instanceof MonthCalendar, true);
assert.strictEqual(calls, 0);
assert.strictEqual(window.mainApi.resizeMainWindow.calledOnce, true);
assert.strictEqual(window.calendarApi.resizeMainWindow.calledOnce, true);
});
});

Expand Down
17 changes: 9 additions & 8 deletions __tests__/__renderer__/classes/DayCalendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,34 @@ describe('DayCalendar class Tests', () =>
waivedWorkdays.set(waivedEntries);

// APIs from the preload script of the calendar window
window.mainApi = calendarApi;
window.calendarApi = calendarApi;
window.rendererApi = {};

// Stubbing methods that don't need the actual implementation for the tests
window.mainApi.toggleTrayPunchTime = () => {};
window.mainApi.resizeMainWindow = () => {};
window.calendarApi.toggleTrayPunchTime = () => {};
window.calendarApi.resizeMainWindow = () => {};
BaseCalendar.prototype._getTranslation = () => {};
BaseCalendar.prototype.redraw = () => {};

window.mainApi.getStoreContents = () => { return new Promise((resolve) => { resolve(entryStore.store); }); };
window.mainApi.getWaiverStoreContents = () => { return new Promise((resolve) => resolve(waivedWorkdays.store)); };
window.mainApi.setStoreData = (key, contents) =>
window.calendarApi.getStoreContents = () => { return new Promise((resolve) => { resolve(entryStore.store); }); };
window.rendererApi.getWaiverStoreContents = () => { return new Promise((resolve) => resolve(waivedWorkdays.store)); };
window.calendarApi.setStoreData = (key, contents) =>
{
return new Promise((resolve) =>
{
entryStore.set(key, contents);
resolve(true);
});
};
window.mainApi.deleteStoreData = (key) =>
window.calendarApi.deleteStoreData = (key) =>
{
return new Promise((resolve) =>
{
entryStore.delete(key);
resolve(true);
});
};
window.mainApi.computeAllTimeBalanceUntilPromise = (targetDate) =>
window.calendarApi.computeAllTimeBalanceUntilPromise = (targetDate) =>
{
return new Promise((resolve) =>
{
Expand Down
17 changes: 9 additions & 8 deletions __tests__/__renderer__/classes/MonthCalendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,33 @@ describe('MonthCalendar class Tests', () =>
waivedWorkdays.set(waivedEntries);

// APIs from the preload script of the calendar window
window.mainApi = calendarApi;
window.calendarApi = calendarApi;
window.rendererApi = {};

// Stubbing methods that don't need the actual implementation for the tests
// window.mainApi.toggleTrayPunchTime = () => {};
window.mainApi.resizeMainWindow = () => {};
// window.calendarApi.toggleTrayPunchTime = () => {};
window.calendarApi.resizeMainWindow = () => {};
BaseCalendar.prototype._getTranslation = () => {};
BaseCalendar.prototype.redraw = () => {};
window.mainApi.getStoreContents = () => { return new Promise((resolve) => { resolve(entryStore.store); }); };
window.mainApi.getWaiverStoreContents = () => { return new Promise((resolve) => resolve(waivedWorkdays.store)); };
window.mainApi.setStoreData = (key, contents) =>
window.calendarApi.getStoreContents = () => { return new Promise((resolve) => { resolve(entryStore.store); }); };
window.rendererApi.getWaiverStoreContents = () => { return new Promise((resolve) => resolve(waivedWorkdays.store)); };
window.calendarApi.setStoreData = (key, contents) =>
{
return new Promise((resolve) =>
{
entryStore.set(key, contents);
resolve(true);
});
};
window.mainApi.deleteStoreData = (key) =>
window.calendarApi.deleteStoreData = (key) =>
{
return new Promise((resolve) =>
{
entryStore.delete(key);
resolve(true);
});
};
window.mainApi.computeAllTimeBalanceUntilPromise = (targetDate) =>
window.calendarApi.computeAllTimeBalanceUntilPromise = (targetDate) =>
{
return new Promise((resolve) =>
{
Expand Down
31 changes: 14 additions & 17 deletions __tests__/__renderer__/preferences.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { rootDir } from '../../js/app-config.mjs';
import {
getDefaultPreferences,
getPreferencesFilePath,
getUserPreferences,
savePreferences,
} from '../../js/user-preferences.mjs';
import { preferencesApi } from '../../renderer/preload-scripts/preferences-api.mjs';
Expand Down Expand Up @@ -83,7 +82,6 @@ const testPreferences = Object.assign({}, getDefaultPreferences());
let convertTimeFormat;
let listenerLanguage;
let populateLanguages;
let refreshContent;
let renderPreferencesWindow;
let setupListeners;
let resetContent;
Expand All @@ -95,22 +93,23 @@ describe('Test Preferences Window', () =>
stub(i18nTranslator, 'getTranslationInLanguageData').returnsThis();

// APIs from the preload script of the preferences window
window.mainApi = preferencesApi;
window.preferencesApi = preferencesApi;

// Mocking with the actual access that main would have
window.mainApi.getUserPreferencesPromise = () => { return new Promise((resolve) => resolve(getUserPreferences())); };
// Mocking with the actual value
window.rendererApi = {
getLanguageDataPromise: () =>
{
return new Promise((resolve) => resolve({
'language': 'en',
'data': {}
}));
},
getOriginalUserPreferences: () => { return testPreferences; },
showDialogSync: () => { return new Promise((resolve) => resolve({ response: 0 })); }
};

// Stub methods
window.mainApi.notifyNewPreferences = () => {};
window.mainApi.showDialogSync = () => { return new Promise((resolve) => resolve({ response: 0 })); };

window.mainApi.getLanguageDataPromise = () =>
{
return new Promise((resolve) => resolve({
'language': 'en',
'data': {}
}));
};
window.preferencesApi.notifyNewPreferences = () => {};

resetPreferenceFile();

Expand All @@ -120,7 +119,6 @@ describe('Test Preferences Window', () =>
convertTimeFormat = file.convertTimeFormat;
listenerLanguage = file.listenerLanguage;
populateLanguages = file.populateLanguages;
refreshContent = file.refreshContent;
renderPreferencesWindow = file.renderPreferencesWindow;
setupListeners = file.setupListeners;
resetContent = file.resetContent;
Expand All @@ -131,7 +129,6 @@ describe('Test Preferences Window', () =>
beforeEach(async function()
{
await prepareMockup();
await refreshContent();
renderPreferencesWindow();
populateLanguages();
listenerLanguage();
Expand Down
Loading

0 comments on commit f058e89

Please sign in to comment.