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

Using JS resizeTo directly on renderer #1136

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions __mocks__/jquery.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ window.matchMedia = window.matchMedia || function()

// Mocking requestAnimationFrame since it's not usually defined but we use it
global.requestAnimationFrame = callback => callback();

// Mocking resizeTo since it's not implemented by JSDOM but used by the calendar
window.resizeTo = () => {};
183 changes: 121 additions & 62 deletions __tests__/__main__/main-window.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,77 @@ import IpcConstants from '../../js/ipc-constants.mjs';
ipcMain.removeHandler(IpcConstants.GetLanguageData);
ipcMain.handle(IpcConstants.GetLanguageData, () => ({
'language': 'en',
'data': {}
'data': {
'translation': {
'$BaseCalendar': {
'day-done-balance': '',
'month-balance': '',
'month-balance-title': '',
'overall-balance': '',
'overall-balance-title': '',
'switch-view': ''
},
'$DateUtil': {
'april': '',
'august': '',
'december': '',
'february': '',
'fri': '',
'january': '',
'july': '',
'june': '',
'march': '',
'may': '',
'mon': '',
'november': '',
'october': '',
'sat': '',
'september': '',
'sun': '',
'thu': '',
'tue': '',
'wed': ''
},
'$DayCalendar': {
'add-entry': '',
'current-day': '',
'day-total': '',
'entry': '',
'entry-removal-confirmation': '',
'leave-by': '',
'next-day': '',
'no': '',
'not-a-working-day': '',
'previous-day': '',
'remove-entry': '',
'waived-day': '',
'yes': ''
},
'$Menu': {
'punch-time': ''
},
'$MonthCalendar': {
'add-entry': '',
'add-waiver-day': '',
'current-month': '',
'day': '',
'entry-removal-confirmation': '',
'last-day-balance': '',
'leave-by': '',
'next-month': '',
'no': '',
'on': '',
'previous-month': '',
'remove-entry': '',
'scroll-left-entry': '',
'scroll-right-entry': '',
'total': '',
'waived-day': '',
'working-days': '',
'working-days-title': '',
'yes': ''
}
}}
}));

describe('main-window.mjs', () =>
Expand All @@ -31,6 +101,10 @@ describe('main-window.mjs', () =>
{
// Avoid showing the window
showSpy = stub(BrowserWindow.prototype, 'show');

ipcMain.handle(IpcConstants.GetStoreContents, () => new Promise(resolve => resolve({})));
ipcMain.handle(IpcConstants.GetWaiverStoreContents, () => new Promise(resolve => resolve({})));
ipcMain.handle(IpcConstants.ComputeAllTimeBalanceUntil, () => new Promise(resolve => resolve({})));
});

beforeEach(() =>
Expand Down Expand Up @@ -71,7 +145,6 @@ describe('main-window.mjs', () =>
const mainWindow = getMainWindow();
assert.strictEqual(mainWindow instanceof BrowserWindow, true);
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);
Expand All @@ -87,84 +160,66 @@ describe('main-window.mjs', () =>
});
});

describe('emit IpcConstants.ResizeMainWindow', function()
describe('emit IpcConstants.SwitchView', () =>
{
it('It should resize window', (done) =>
it('It should send new event to ipcRenderer', function(done)
{
createWindow();
/**
* @type {BrowserWindow}
*/
const mainWindow = getMainWindow();
mainWindow.webContents.ipc.on(IpcConstants.WindowReadyToShow, async() =>
{
// Wait a bit for values to accomodate
await new Promise(res => setTimeout(res, 500));
this.timeout(5000);

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

// Width and height are within 5 pixels of the expected values
assert.strictEqual(Math.abs(windowSize[0] - 1010) < 5, true, `Value was ${windowSize[0]}`);
assert.strictEqual(Math.abs(windowSize[1] - 800) < 5, true, `Value was ${windowSize[1]}`);
done();
});
});
it('It should not resize window if values are smaller than minimum', (done) =>
{
assert.strictEqual(savePreferences({
...getDefaultPreferences(),
['view']: 'day'
['view']: 'month'
}), true);
createWindow();
/**
* @type {BrowserWindow}
*/
const mainWindow = getMainWindow();

const windowSpy = spy(mainWindow.webContents, 'send');

mainWindow.webContents.ipc.on(IpcConstants.WindowReadyToShow, async() =>
{
// Wait a bit for values to accomodate
await new Promise(res => setTimeout(res, 500));
// try-catch block to log exceptions even though they get ignored by mocha in async blocks
try
{
// Wait a bit for values to accomodate
await new Promise(res => setTimeout(res, 750));

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

// Width and height are within 5 pixels of the expected values
assert.strictEqual(Math.abs(windowSize[0] - 500) < 5, true, `Value was ${windowSize[0]}`);
assert.strictEqual(Math.abs(windowSize[1] - 500) < 5, true, `Value was ${windowSize[1]}`);
done();
});
});
});
// First within the month view sizes
// // Width and height are within 5 pixels of the expected values
// assert.strictEqual(Math.abs(windowSize[0] - 1010) < 5, true, `Value was ${windowSize[0]}`);
// assert.strictEqual(Math.abs(windowSize[1] - 800) < 5, true, `Value was ${windowSize[1]}`);

describe('emit IpcConstants.SwitchView', () =>
{
it('It should send new event to ipcRenderer', (done) =>
{
assert.strictEqual(savePreferences({
...getDefaultPreferences(),
['view']: 'month'
}), true);
createWindow();
/**
* @type {BrowserWindow}
*/
const mainWindow = getMainWindow();
mainWindow.webContents.ipc.on(IpcConstants.WindowReadyToShow, async() =>
{
// Wait a bit for values to accomodate
await new Promise(res => setTimeout(res, 500));
ipcMain.emit(IpcConstants.SwitchView);

const windowStub = stub(mainWindow.webContents, 'send').callsFake((event, savedPreferences) =>
{
assert.strictEqual(windowStub.calledOnce, true);
assert.strictEqual(savedPreferences['view'], 'day');
// Wait a bit for values to accomodate
await new Promise(res => setTimeout(res, 500));

windowSize = mainWindow.getSize();
assert.strictEqual(windowSize.length, 2);

// Now in day view sizes
assert.strictEqual(Math.abs(windowSize[0] - 500) < 5, true, `Value was ${windowSize[0]}`);
assert.strictEqual(Math.abs(windowSize[1] - 500) < 5, true, `Value was ${windowSize[1]}`);

assert.strictEqual(windowSpy.calledOnce, true);

const firstCall = windowSpy.firstCall;
assert.strictEqual(firstCall.args[0], IpcConstants.PreferencesSaved);
assert.strictEqual(firstCall.args[1]['view'], 'day');

windowSpy.restore();
done();
});
ipcMain.emit(IpcConstants.SwitchView);
windowStub.restore();
}
catch (e)
{
console.log(e);
done(e);
}
});
});
});
Expand Down Expand Up @@ -451,5 +506,9 @@ describe('main-window.mjs', () =>
after(() =>
{
showSpy.restore();

ipcMain.removeHandler(IpcConstants.GetStoreContents);
ipcMain.removeHandler(IpcConstants.GetWaiverStoreContents);
ipcMain.removeHandler(IpcConstants.ComputeAllTimeBalanceUntil);
});
});
16 changes: 4 additions & 12 deletions __tests__/__main__/user-preferences.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,16 @@ describe('Preferences Main', () =>
{
it('Month view', function()
{
// For some reason this test takes longer when running the whole testsuite. My suspicion is that
// writing to file inside getDefaultWidthHeight is taking longer after many tests write to the file.
// Thus, increasing the timeout.
this.timeout(15000);
assert.strictEqual(getDefaultPreferences()['view'], 'month');
savePreferences(getDefaultPreferences());

assert.deepStrictEqual(getDefaultWidthHeight(), { width: 1010, height: 800 });
const preferences = structuredClone(getDefaultPreferences());
assert.strictEqual(preferences['view'], 'month');
assert.deepStrictEqual(getDefaultWidthHeight(preferences), { width: 1010, height: 800 });
});

it('Day view', () =>
{
const preferences = structuredClone(getDefaultPreferences());

preferences['view'] = 'day';
savePreferences(preferences);

assert.deepStrictEqual(getDefaultWidthHeight(), { width: 500, height: 500 });
assert.deepStrictEqual(getDefaultWidthHeight(preferences), { width: 500, height: 500 });
});
});

Expand Down
2 changes: 2 additions & 0 deletions __tests__/__main__/windows.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('Windows tests', () =>
{
return new Promise(resolve => resolve([]));
});

Windows.resetWindowsElements();
});

it('Elements should be null on starting', () =>
Expand Down
19 changes: 10 additions & 9 deletions __tests__/__renderer__/classes/CalendarFactory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('CalendarFactory', () =>
// Mocked APIs from the preload script of the calendar window
window.calendarApi = calendarApi;

window.calendarApi.resizeMainWindow = stub();
stub(CalendarFactory, 'ResizeCalendar');

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.calendarApi.resizeMainWindow.resetHistory();
CalendarFactory.ResizeCalendar.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.calendarApi.resizeMainWindow.calledOnce, true);
assert.strictEqual(CalendarFactory.ResizeCalendar.calledOnce, true);
});

it('Should return new calendar without resizing if passing in an undefined instance', async() =>
{
window.calendarApi.resizeMainWindow.resetHistory();
CalendarFactory.ResizeCalendar.resetHistory();
const calendar = await CalendarFactory.getInstance({
view: 'day',
}, {}, undefined);
assert.strictEqual(calendar instanceof DayCalendar, true);
assert.strictEqual(window.calendarApi.resizeMainWindow.notCalled, true);
assert.strictEqual(CalendarFactory.ResizeCalendar.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.calendarApi.resizeMainWindow.resetHistory();
CalendarFactory.ResizeCalendar.resetHistory();
const calendar = await CalendarFactory.getInstance({
view: 'month',
}, {}, undefined);
assert.strictEqual(calendar instanceof MonthCalendar, true);
assert.strictEqual(window.calendarApi.resizeMainWindow.notCalled, true);
assert.strictEqual(CalendarFactory.ResizeCalendar.notCalled, true);
});

it('Should return new calendar with resizing if passing in an instance that is not a MonthCalendar', async() =>
{
window.calendarApi.resizeMainWindow.resetHistory();
CalendarFactory.ResizeCalendar.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.calendarApi.resizeMainWindow.calledOnce, true);
assert.strictEqual(CalendarFactory.ResizeCalendar.calledOnce, true);
});
});

Expand All @@ -146,5 +146,6 @@ describe('CalendarFactory', () =>
Object.setPrototypeOf(DayCalendar, DayCalendarPrototype);
Object.setPrototypeOf(MonthCalendar, MonthCalendarPrototype);
BaseCalendar.prototype.reload.restore();
CalendarFactory.ResizeCalendar.restore();
});
});
1 change: 0 additions & 1 deletion __tests__/__renderer__/classes/DayCalendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe('DayCalendar class Tests', () =>

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

Expand Down
1 change: 0 additions & 1 deletion __tests__/__renderer__/classes/MonthCalendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ describe('MonthCalendar class Tests', () =>

// Stubbing methods that don't need the actual implementation for the tests
// window.calendarApi.toggleTrayPunchTime = () => {};
window.calendarApi.resizeMainWindow = () => {};
BaseCalendar.prototype._getTranslation = () => {};
BaseCalendar.prototype.redraw = () => {};
window.calendarApi.getStoreContents = () => { return new Promise((resolve) => { resolve(entryStore.store); }); };
Expand Down
1 change: 0 additions & 1 deletion js/ipc-constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const IpcConstants = Object.freeze(
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',
Expand Down
Loading