Skip to content

Commit

Permalink
Fix #889: Adapting calendar tests to new format
Browse files Browse the repository at this point in the history
  • Loading branch information
araujoarthur0 committed Oct 19, 2023
1 parent 647c9c0 commit cc32c2a
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 65 deletions.
22 changes: 13 additions & 9 deletions __tests__/__main__/main-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('main-window.js', () =>
expect(mainWindow).toBeInstanceOf(BrowserWindow);
expect(ipcMain.listenerCount('TOGGLE_TRAY_PUNCH_TIME')).toBe(1);
expect(ipcMain.listenerCount('RESIZE_MAIN_WINDOW')).toBe(1);
expect(ipcMain.listenerCount('VIEW_CHANGED')).toBe(1);
expect(ipcMain.listenerCount('SWITCH_VIEW')).toBe(1);
expect(ipcMain.listenerCount('RECEIVE_LEAVE_BY')).toBe(1);
expect(mainWindow.listenerCount('minimize')).toBe(2);
expect(mainWindow.listenerCount('close')).toBe(1);
Expand All @@ -73,30 +73,34 @@ describe('main-window.js', () =>
const mainWindow = getMainWindow();
mainWindow.on('ready-to-show', () =>
{
ipcMain.emit('RESIZE_MAIN_WINDOW', {}, 500, 600);
expect(mainWindow.getSize()).toEqual([500, 600]);
ipcMain.emit('RESIZE_MAIN_WINDOW');
expect(mainWindow.getSize()).toEqual([1010, 800]);
done();
});
});
test('It should not resize window if values are smaller than minimum', (done) =>
{
savePreferences({
...defaultPreferences,
['view']: 'day'
});
createWindow();
/**
* @type {BrowserWindow}
*/
const mainWindow = getMainWindow();
mainWindow.on('ready-to-show', () =>
{
ipcMain.emit('RESIZE_MAIN_WINDOW', {}, 100, 100);
expect(mainWindow.getSize()).toEqual([450, 450]);
ipcMain.emit('RESIZE_MAIN_WINDOW');
expect(mainWindow.getSize()).toEqual([500, 500]);
done();
});
});
});

describe('emit VIEW_CHANGED', () =>
describe('emit SWITCH_VIEW', () =>
{
test('It should send new event to ipcRendered', (done) =>
test('It should send new event to ipcRenderer', (done) =>
{
createWindow();
/**
Expand All @@ -117,10 +121,10 @@ describe('main-window.js', () =>
ipcMain.on('FINISH_TEST', (event, savedPreferences) =>
{
expect(windowSpy).toBeCalledTimes(1);
expect(savedPreferences).toEqual({ new: 'prefrences' });
expect(savedPreferences['view']).toEqual('day');
done();
});
ipcMain.emit('VIEW_CHANGED', {}, { new: 'prefrences' });
ipcMain.emit('SWITCH_VIEW');
windowSpy.mockRestore();
});
});
Expand Down
57 changes: 48 additions & 9 deletions __tests__/__renderer__/classes/BaseCalendar.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import ElectronStore from 'electron-store';
import { BaseCalendar } from '../../../js/classes/BaseCalendar.js';
import { BaseCalendar } from '../../../renderer/classes/BaseCalendar.js';
import { generateKey } from '../../../js/date-db-formatter.js';
import { getUserPreferences, resetPreferences, savePreferences } from '../../../js/user-preferences.js';
import { getUserPreferences, resetPreferences, savePreferences, switchCalendarView } from '../../../js/user-preferences.js';
const Store = require('electron-store');
const timeBalance = require('../../../js/time-balance');
import { calendarApi } from '../../../renderer/preload-scripts/calendar-api.js';

jest.mock('../../../js/time-math', () =>
{
Expand All @@ -17,6 +18,19 @@ jest.mock('../../../js/time-math', () =>
const timeMath = require('../../../js/time-math');
window.$ = require('jquery');

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

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

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

describe('BaseCalendar.js', () =>
{
class ExtendedClass extends BaseCalendar
Expand All @@ -39,6 +53,15 @@ describe('BaseCalendar.js', () =>
waivedWorkdays.clear();
ExtendedClass.prototype._initCalendar = () => {};
ExtendedClass.prototype._getTargetDayForAllTimeBalance = () => {};

window.mainApi.getFlexibleStoreContents = () =>
{
return flexibleStore.store;
};
window.mainApi.getWaiverStoreContents = () =>
{
return waivedWorkdays.store;
};
});

describe('constructor', () =>
Expand All @@ -60,20 +83,27 @@ describe('BaseCalendar.js', () =>
expect(() => new ExtendedClass(preferences, languageData)._getTargetDayForAllTimeBalance()).toThrow('Please implement this.');
});

test('Should build with default values', (done) =>
test('Should build with default values', async(done) =>
{
ExtendedClass.prototype._initCalendar = () => { done(); };
const preferences = {view: 'day'};
const languageData = {hello: 'hola'};
const calendar = new ExtendedClass(preferences, languageData);
expect(calendar._calendarDate).toBeInstanceOf(Date);
expect(calendar._languageData).toEqual(languageData);
expect(calendar._preferences).toEqual(preferences);

// These no longer get set in the constructor
expect(calendar._internalStore).toEqual(undefined);
expect(calendar._internalWaiverStore).toEqual(undefined);

// But are set after awaiting for initialization
await calendar.initializeStores();
expect(calendar._internalStore).toEqual({});
expect(calendar._internalWaiverStore).toEqual({});
expect(calendar._preferences).toEqual(preferences);
});

test('Should build with default internal store values', (done) =>
test('Should build with default internal store values', async(done) =>
{
ExtendedClass.prototype._initCalendar = () => { done(); };
const flexibleStore = new ElectronStore({name: 'flexible-store'});
Expand All @@ -90,6 +120,14 @@ describe('BaseCalendar.js', () =>
const calendar = new ExtendedClass(preferences, languageData);
expect(calendar._calendarDate).toBeInstanceOf(Date);
expect(calendar._languageData).toEqual(languageData);
expect(calendar._preferences).toEqual(preferences);

// These no longer get set in the constructor
expect(calendar._internalStore).toEqual(undefined);
expect(calendar._internalWaiverStore).toEqual(undefined);

// But are set after awaiting for initialization
await calendar.initializeStores();
expect(calendar._internalStore).toEqual({
flexible: 'store'
});
Expand All @@ -99,7 +137,6 @@ describe('BaseCalendar.js', () =>
hours: '10:00'
}
});
expect(calendar._preferences).toEqual(preferences);
});
});

Expand All @@ -116,7 +153,7 @@ describe('BaseCalendar.js', () =>
expect(mocks.compute).toHaveBeenCalledTimes(0);
});

test('Should not update value because of rejection', () =>
test('Should not update value because of rejection', async() =>
{
mocks.compute = jest.spyOn(timeBalance, 'computeAllTimeBalanceUntilAsync').mockImplementation(() => Promise.reject());
const preferences = {view: 'day'};
Expand Down Expand Up @@ -388,20 +425,21 @@ describe('BaseCalendar.js', () =>

describe('_updateDayTotal()', () =>
{
test('Should not update when day has not ended', () =>
test('Should not update when day has not ended', async() =>
{
const newDate = new Date();
const key = generateKey(newDate.getFullYear(), newDate.getMonth(), newDate.getDate());
$('body').append(`<div id="${key}" ></div>`);
$(`#${key}`).append('<input type="time" value="--:--" />');
const calendar = new ExtendedClass(getUserPreferences(), {});
await calendar.initializeStores();
calendar._updateDayTotal(key);
const dayTotalSpan = $('#' + key).parent().find('.day-total-cell span');
$(`#${key}`).remove();
expect(dayTotalSpan.text()).toBe('');
});

test('Should update when day has ended', () =>
test('Should update when day has ended', async() =>
{
const flexibleStore = new Store({name: 'flexible-store'});
const newDate = new Date();
Expand All @@ -413,6 +451,7 @@ describe('BaseCalendar.js', () =>
$(`#${key}`).append('<input type="time" value="08:00" />');
$(`#${key}`).append('<input type="time" value="16:00" />');
const calendar = new ExtendedClass(getUserPreferences(), {});
await calendar.initializeStores();
calendar._setStore(key, ['08:00', '16:30']);
calendar._updateDayTotal(key);
const dayTotalSpan = $('#' + key).parent().find('.day-total-cell span');
Expand Down
54 changes: 33 additions & 21 deletions __tests__/__renderer__/classes/CalendarFactory.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { CalendarFactory } from '../../../js/classes/CalendarFactory.js';
import { FlexibleDayCalendar } from '../../../js/classes/FlexibleDayCalendar.js';
import { FlexibleMonthCalendar } from '../../../js/classes/FlexibleMonthCalendar.js';
import { CalendarFactory } from '../../../renderer/classes/CalendarFactory.js';
import { FlexibleDayCalendar } from '../../../renderer/classes/FlexibleDayCalendar.js';
import { FlexibleMonthCalendar } from '../../../renderer/classes/FlexibleMonthCalendar.js';

jest.mock('../../../js/classes/BaseCalendar.js', () =>
import { calendarApi } from '../../../renderer/preload-scripts/calendar-api.js';

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

jest.mock('../../../renderer/classes/BaseCalendar.js', () =>
{
class BaseCalendar
{
constructor() { }

async reload() { }
}

return { BaseCalendar };
Expand All @@ -29,16 +36,21 @@ const { ipcRenderer } = require('electron');

describe('CalendarFactory', () =>
{
test('Should fail wrong view', () =>
test('Should fail wrong view', async() =>
{
expect(() => CalendarFactory.getInstance({
const promise = CalendarFactory.getInstance({
view: 'not_supported'
})).toThrow('Could not instantiate not_supported');
}, {});
expect(promise).toBeInstanceOf(Promise);
promise.then(() => {}).catch((reason) =>
{
expect(reason).toBe('Could not instantiate not_supported');
});
});

describe('FlexibleDayCalendar', () =>
{
test('Should fail wrong view', () =>
test('Should fail wrong view', async() =>
{
let calls = 0;
const testCalendar = {
Expand All @@ -49,14 +61,14 @@ describe('CalendarFactory', () =>
updatePreferences: () => { calls++; },
redraw: () => { calls++; },
};
const calendar = CalendarFactory.getInstance({
const calendar = await CalendarFactory.getInstance({
view: 'day',
}, {}, testCalendar);
expect(calendar).toEqual(testCalendar);
expect(calls).toBe(3);
});

test('Should return new calendar without resizing', () =>
test('Should return new calendar without resizing', async() =>
{
let calls = 0;
const testCalendar = {
Expand All @@ -67,28 +79,28 @@ describe('CalendarFactory', () =>
updatePreferences: () => { calls++; },
redraw: () => { calls++; },
};
const calendar = CalendarFactory.getInstance({
const calendar = await CalendarFactory.getInstance({
view: 'day',
}, {}, testCalendar);
expect(calendar).toBeInstanceOf(FlexibleDayCalendar);
expect(calls).toBe(0);
});

test('Should return new calendar without resizing', () =>
test('Should return new calendar without resizing', async() =>
{
let calls = 0;
jest.spyOn(ipcRenderer, 'send').mockImplementation(() =>
{
calls++;
});
const calendar = CalendarFactory.getInstance({
const calendar = await CalendarFactory.getInstance({
view: 'day',
}, {}, undefined);
expect(calendar).toBeInstanceOf(FlexibleDayCalendar);
expect(calls).toBe(0);
});

test('Should return new calendar with resizing', () =>
test('Should return new calendar with resizing', async() =>
{
let calls = 0;
const testCalendar = {
Expand All @@ -103,7 +115,7 @@ describe('CalendarFactory', () =>
{
calls++;
});
const calendar = CalendarFactory.getInstance({
const calendar = await CalendarFactory.getInstance({
view: 'day',
}, {}, testCalendar);
expect(calendar).toBeInstanceOf(FlexibleDayCalendar);
Expand All @@ -113,7 +125,7 @@ describe('CalendarFactory', () =>

describe('FlexibleMonthCalendar', () =>
{
test('Should fail wrong view', () =>
test('Should fail wrong view', async() =>
{
let calls = 0;
const testCalendar = {
Expand All @@ -124,28 +136,28 @@ describe('CalendarFactory', () =>
updatePreferences: () => { calls++; },
redraw: () => { calls++; },
};
const calendar = CalendarFactory.getInstance({
const calendar = await CalendarFactory.getInstance({
view: 'month',
}, {}, testCalendar);
expect(calendar).toEqual(testCalendar);
expect(calls).toBe(3);
});

test('Should return new calendar without resizing', () =>
test('Should return new calendar without resizing', async() =>
{
let calls = 0;
jest.spyOn(ipcRenderer, 'send').mockImplementation(() =>
{
calls++;
});
const calendar = CalendarFactory.getInstance({
const calendar = await CalendarFactory.getInstance({
view: 'month',
}, {}, undefined);
expect(calendar).toBeInstanceOf(FlexibleMonthCalendar);
expect(calls).toBe(0);
});

test('Should return new calendar with resizing', () =>
test('Should return new calendar with resizing', async() =>
{
let calls = 0;
const testCalendar = {
Expand All @@ -160,7 +172,7 @@ describe('CalendarFactory', () =>
{
calls++;
});
const calendar = CalendarFactory.getInstance({
const calendar = await CalendarFactory.getInstance({
view: 'month',
}, {}, testCalendar);
expect(calendar).toBeInstanceOf(FlexibleMonthCalendar);
Expand Down
Loading

0 comments on commit cc32c2a

Please sign in to comment.