Skip to content

Commit

Permalink
Passing original preferences to calendar window
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoarthur0 committed Jan 26, 2025
1 parent ea21c38 commit c70ff99
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 193 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
41 changes: 21 additions & 20 deletions __tests__/__renderer__/workday-waiver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getDefaultPreferences,
getUserPreferences,
savePreferences,
showDay,
} from '../../js/user-preferences.mjs';
import i18nTranslator from '../../renderer/i18n-translator.js';

Expand Down Expand Up @@ -72,7 +73,7 @@ async function addTestWaiver(day, reason)

async function testWaiverCount(expected)
{
const waivedWorkdays = await window.mainApi.getWaiverStoreContents();
const waivedWorkdays = await window.rendererApi.getWaiverStoreContents();
assert.strictEqual(waivedWorkdays.size, expected);
assert.strictEqual($('#waiver-list-table tbody')[0].rows.length, expected);
}
Expand Down Expand Up @@ -108,9 +109,10 @@ describe('Test Workday Waiver Window', function()

// APIs from the preload script of the workday waiver window
window.workdayWaiverApi = workdayWaiverApi;
window.rendererApi = {};

// Mocking with the actual access to store that main would have
window.workdayWaiverApi.getWaiverStoreContents = () => { return new Promise((resolve) => resolve(waiverStore.store)); };
window.rendererApi.getWaiverStoreContents = () => { return new Promise((resolve) => resolve(waiverStore.store)); };
window.workdayWaiverApi.setWaiver = (key, contents) =>
{
return new Promise((resolve) =>
Expand Down Expand Up @@ -168,26 +170,25 @@ describe('Test Workday Waiver Window', function()
});
};

window.rendererApi = {
getLanguageDataPromise: () =>
{
return new Promise((resolve) => resolve({
'language': 'en',
'data': {}
}));
},
showDialogSync: () =>
{
return new Promise((resolve) =>
{
resolve({ response: 0 });
});
},
getOriginalUserPreferences: () =>
window.rendererApi.getLanguageDataPromise = () =>
{
return new Promise((resolve) => resolve({
'language': 'en',
'data': {}
}));
};
window.rendererApi.showDialogSync = () =>
{
return new Promise((resolve) =>
{
return getUserPreferences();
}
resolve({ response: 0 });
});
};
window.rendererApi.getOriginalUserPreferences = () =>
{
return getUserPreferences();
};
window.rendererApi.showDay = showDay;

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

Expand Down
6 changes: 5 additions & 1 deletion js/main-window.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function createWindow()
{
// Create the browser window.
const widthHeight = getDefaultWidthHeight();
const userPreferences = getUserPreferences();
mainWindow = new BrowserWindow({
width: widthHeight.width,
height: widthHeight.height,
Expand All @@ -82,7 +83,10 @@ function createWindow()
webPreferences: {
nodeIntegration: true,
preload: path.join(rootDir, '/renderer/preload-scripts/calendar-bridge.mjs'),
contextIsolation: true
contextIsolation: true,
additionalArguments: [
`--preferences=${JSON.stringify(userPreferences)}`,
],
}
});

Expand Down
Loading

0 comments on commit c70ff99

Please sign in to comment.