Skip to content

Commit

Permalink
Merge branch 'Issue1010-ESLint' of https://github.com/gsriram24/time-…
Browse files Browse the repository at this point in the history
…to-leave into Issue1010-ESLint
  • Loading branch information
gsriram24 committed Oct 19, 2023
2 parents 07b08a9 + 0e6cc76 commit f659c54
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 33 deletions.
4 changes: 2 additions & 2 deletions __tests__/__main__/main-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ describe('main-window.js', () =>

test('Should minimize if minimize-to-tray is false', (done) =>
{
const userPreferencesSpy = jest.spyOn(userPreferences, 'getUserPreferences');
savePreferences({
...defaultPreferences,
['minimize-to-tray']: false
Expand All @@ -266,10 +265,11 @@ describe('main-window.js', () =>
* @type {BrowserWindow}
*/
const mainWindow = getMainWindow();
const minimizeSpy = jest.spyOn(mainWindow, 'minimize');
mainWindow.on('ready-to-show', () =>
{
mainWindow.emit('minimize', {});
expect(userPreferencesSpy).toHaveBeenCalledTimes(1);
expect(minimizeSpy).toBeCalled();
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__main__/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Notifications', function()
{
describe('notify', () =>
{
beforeAll(() =>
beforeEach(() =>
{
// displays a notification in test fails if mocks are not restored
jest.restoreAllMocks();
Expand Down
38 changes: 37 additions & 1 deletion __tests__/__main__/time-balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
const Store = require('electron-store');
import {
computeAllTimeBalanceUntil,
getFirstInputInDb
getFirstInputInDb,
computeAllTimeBalanceUntilAsync
} from '../../js/time-balance.js';
import { resetPreferences } from '../../js/user-preferences.js';

Expand Down Expand Up @@ -93,6 +94,26 @@ describe('Time Balance', () =>
await expect(computeAllTimeBalanceUntil(new Date(2020, 6, 7))).resolves.toBe('-24:00');
});

test('computeAllTimeBalanceUntil: only regular days, timesAreProgressing false', async() =>
{
const entryEx = {
'2020-6-1': {'values': ['08:00', '12:00', '17:00', '13:00']} // wed (8h total)
};
flexibleStore.set(entryEx);
// time balance until thu (excluding thu)
await expect(computeAllTimeBalanceUntil(new Date(2020, 6, 2))).resolves.toBe('-08:00');
// time balance until fri (excluding fri)
await expect(computeAllTimeBalanceUntil(new Date(2020, 6, 3))).resolves.toBe('-16:00');
// time balance until sat (excluding sat)
await expect(computeAllTimeBalanceUntil(new Date(2020, 6, 4))).resolves.toBe('-24:00');
// time balance until sun (excluding sun)
await expect(computeAllTimeBalanceUntil(new Date(2020, 6, 5))).resolves.toBe('-24:00');
// time balance until mon (excluding mon)
await expect(computeAllTimeBalanceUntil(new Date(2020, 6, 6))).resolves.toBe('-24:00');
// time balance until tue (excluding tue)
await expect(computeAllTimeBalanceUntil(new Date(2020, 6, 7))).resolves.toBe('-32:00');
});

test('computeAllTimeBalanceUntil: only regular days (6 entries)', async() =>
{
const entryEx = {
Expand Down Expand Up @@ -318,4 +339,19 @@ describe('Time Balance', () =>
waivedWorkdays.set(waivedEntries);
await expect(computeAllTimeBalanceUntil(new Date(2020, 5, 1))).resolves.toBe('00:00');
});

test('computeAllTimeBalanceUntilAsync: target date in the past of entries', async() =>
{
const entryEx = {
'2020-6-1': {'values': ['08:00', '12:00', '13:00', '17:00']}, // wed (8h total)
'2020-6-3': {'values': ['08:00', '12:00', '13:00', '17:00']} // fri (8h total)
};
flexibleStore.set(entryEx);
const waivedEntries = {
'2020-07-02': { reason: 'Waiver', hours: '02:00' }, // tue
};
waivedWorkdays.set(waivedEntries);
await expect(computeAllTimeBalanceUntilAsync(new Date(2020, 5, 1))).resolves.toBe('00:00');
});

});
50 changes: 40 additions & 10 deletions __tests__/__main__/time-math.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,45 @@ describe('Time Math Functions', () =>

test('validateDate(date)', () =>
{
expect(validateDate('0001-00-00')).toBeFalsy();
expect(validateDate('1-00-00')).toBeFalsy();
expect(validateDate('1996-13-00')).toBeFalsy();
expect(validateDate('1996-1-00')).toBeFalsy();
expect(validateDate('1996-01-1')).toBeFalsy();
expect(validateDate('1996-01-40')).toBeFalsy();
expect(validateDate('1996-01-31')).toBeFalsy();
expect(validateDate('I\'m a date!')).toBeFalsy();
expect(validateDate('1996-01-29')).toBeTruthy();
expect(validateDate('1996-01-30')).toBeFalsy();
const tests = [
{date: '0001-00-00',valid: false},
{date: '1-00-00',valid: false},
{date: '1996-13-00',valid: false},
{date: '1996-1-00',valid: false},
{date: '1996-01-1',valid: false},
{date: '1996-01-40',valid: false},
{date: '1996-01-31',valid: false},
{date: 'I\'m a date!',valid: false},
{date: '1996-01-29',valid: true},
{date: '1996-01-30',valid: false},
{date: '1996-00-01', valid: true},
{date: '1996-01-01', valid: true},
{date: '1996-02-01', valid: true},
{date: '1996-03-01', valid: true},
{date: '1996-04-01', valid: true},
{date: '1996-05-01', valid: true},
{date: '1996-06-01', valid: true},
{date: '1996-07-01', valid: true},
{date: '1996-08-01', valid: true},
{date: '1996-09-01', valid: true},
{date: '1996-10-01', valid: true},
{date: '1996-11-01', valid: true},
{date: '1996-00-40', valid: false},
{date: '1996-01-40', valid: false},
{date: '1996-02-40', valid: false},
{date: '1996-03-40', valid: false},
{date: '1996-04-40', valid: false},
{date: '1996-05-40', valid: false},
{date: '1996-06-40', valid: false},
{date: '1996-07-40', valid: false},
{date: '1996-08-40', valid: false},
{date: '1996-09-40', valid: false},
{date: '1996-10-40', valid: false},
{date: '1996-11-40', valid: false},
];
for (const test of tests)
{
expect(validateDate(test.date)).toBe(test.valid);
}
});
});
85 changes: 85 additions & 0 deletions __tests__/__main__/update-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const ElectronStore = require('electron-store');
const { getDateStr } = require('../../js/date-aux');
const {shouldCheckForUpdates, checkForUpdates} = require('../../js/update-manager');

jest.mock('electron', () =>
{
const original = jest.requireActual('electron');
return {
__esModule: true,
...original,
net: {
...original.net,
request: jest.fn()
}
};
});

jest.mock('is-online', () => () => jest.fn().mockResolvedValueOnce(false).mockResolvedValue(true));

const { net } = require('electron');

describe('js/update-manager.js', () =>
{
const mocks = {};
describe('shouldCheckForUpdates', () =>
{
test('Should return true when was never checked', () =>
{
const store = new ElectronStore();
store.set('update-remind-me-after', false);
expect(shouldCheckForUpdates()).toBe(true);
});

test('Should return true when was checked before today', () =>
{
const now = new Date();
now.setDate(now.getDate() - 1);
const store = new ElectronStore();
store.set('update-remind-me-after', getDateStr(now));
expect(shouldCheckForUpdates()).toBe(true);
});

test('Should return false when was checked today', () =>
{
const now = new Date();
const store = new ElectronStore();
store.set('update-remind-me-after', getDateStr(now));
expect(shouldCheckForUpdates()).toBe(false);
});
});

describe('checkForUpdates', () =>
{
test('should not execute when is offline', () =>
{
mocks.net = jest.spyOn(net, 'request').mockImplementation(() => {});
checkForUpdates();
expect(mocks.net).toBeCalledTimes(0);
});

test('should not execute when is online', (done) =>
{
mocks.net = jest.spyOn(net, 'request').mockImplementation(() =>
{
return {
on: () =>
{
expect(mocks.net).toBeCalledTimes(1);
done();
}
};
});
checkForUpdates();
});

});

afterEach(() =>
{
for (const mock of Object.values(mocks))
{
mock.mockClear();
}
});
});
94 changes: 94 additions & 0 deletions __tests__/__main__/windows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const { BrowserWindow } = require('electron');
const { getDateStr } = require('../../js/date-aux.js');
const windows = require('../../js/windows.js');
const {getWaiverWindow, tray, contextMenu, prefWindow, resetWindowsElements, openWaiverManagerWindow, getDialogCoordinates} = require('../../js/windows.js');

describe('windows.js', () =>
{
let showSpy;
let loadSpy;
beforeEach(() =>
{
// Avoid window being shown
loadSpy = jest.spyOn(BrowserWindow.prototype, 'loadURL').mockImplementation(() => {});
showSpy = jest.spyOn(BrowserWindow.prototype, 'show').mockImplementation(() => {});
jest.spyOn(windows, 'getDialogCoordinates').mockImplementation(() => ({x:0, y:0}));
});

test('Elements should be null on starting', () =>
{
expect(getWaiverWindow()).toBe(null);
expect(tray).toBe(null);
expect(contextMenu).toBe(null);
expect(prefWindow).toBe(null);
});

test('Should create waiver window', (done) =>
{
const mainWindow = new BrowserWindow({
show: false
});
openWaiverManagerWindow(mainWindow);
expect(getWaiverWindow()).not.toBe(null);
expect(getWaiverWindow()).toBeInstanceOf(BrowserWindow);
expect(getWaiverWindow().getSize()).toEqual([600, 500]);
done();
});

test('Should show waiver window it has been created', (done) =>
{
const mainWindow = new BrowserWindow({
show: false
});
openWaiverManagerWindow(mainWindow);
openWaiverManagerWindow(mainWindow);
expect(getWaiverWindow()).not.toBe(null);
// It should only load once the URL because it already exists
expect(showSpy).toHaveBeenCalledTimes(2);
expect(loadSpy).toHaveBeenCalledTimes(1);
done();
});

test('Should set global waiverDay when event is sent', (done) =>
{
const mainWindow = new BrowserWindow({
show: false
});
openWaiverManagerWindow(mainWindow, true);
expect(getWaiverWindow()).not.toBe(null);
expect(global.waiverDay).toBe(getDateStr(new Date()));
done();
});

test('Should reset waiverWindow on close', () =>
{
const mainWindow = new BrowserWindow({
show: false
});
openWaiverManagerWindow(mainWindow, true);
getWaiverWindow().emit('close');
expect(getWaiverWindow()).toBe(null);
});

test('Should get dialog coordinates', () =>
{
const coordinates = getDialogCoordinates(500, 250, {
getBounds: () => ({
x: 200,
y: 300,
width: 400,
height: 600
})
});
expect(coordinates).toEqual({
x: 150,
y: 475
});
});

afterEach(() =>
{
jest.restoreAllMocks();
resetWindowsElements();
});
});
55 changes: 54 additions & 1 deletion __tests__/__renderer__/workday-waiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ describe('Test Workday Waiver Window', function()
expect(mockCallback).toBeCalledTimes(holidaysLength);
});

test('Do not load holidays table on empty holidays', () =>
{
loadHolidaysTable();
const holidaysLength = 0;
const rowLength = $('#holiday-list-table tbody tr').length;
expect($('#holiday-list-table').css('display')).toBe('table');
expect(holidaysLength).toBe(rowLength);
});

test('Load holidays table', async() =>
{
$('#year').append($('<option selected></option>').val(year).html(year));
Expand Down Expand Up @@ -467,12 +476,56 @@ describe('Test Workday Waiver Window', function()
const secondCell = table.find('td')[1].innerHTML;
const thirdCell = table.find('td')[2].innerHTML;
const fourthCell = table.find('td')[4].innerHTML;
const fourthCellContent = `<label class="switch"><input type="checkbox" checked="${conflicts || workingDay === 'No' ? '' : 'checked'}" name="import-${day}" id="import-${day}"><span class="slider round"></span></label>`;
const fourthCellContent = `<label class="switch"><input type="checkbox" checked="" name="import-${day}" id="import-${day}"><span class="slider round"></span></label>`;
expect(firstCell).toBe(day);
expect(secondCell).toBe(reason);
expect(thirdCell).toBe('undefined');
expect(fourthCell).toEqual(fourthCellContent);
});

test('Holiday added not working day, no conflicts', () =>
{
const day = 'test day';
const reason = 'test reason';
const workingDay = 'No';
const conflicts = undefined;
addHolidayToList(day, reason, workingDay);
const table = $('#holiday-list-table tbody');
const rowsLength = table.find('tr').length;
expect(rowsLength).toBe(1);
const firstCell = table.find('td')[0].innerHTML;
const secondCell = table.find('td')[1].innerHTML;
const thirdCell = table.find('td')[2].innerHTML;
const fourthCell = table.find('td')[4].innerHTML;
const fourthCellContent = `<label class="switch"><input type="checkbox" name="import-${day}" id="import-${day}"><span class="slider round"></span></label>`;
expect(firstCell).toBe(day);
expect(secondCell).toBe(reason);
expect(thirdCell).toBe(workingDay);
expect(fourthCell).toEqual(fourthCellContent);
});

test('Holiday added not working day, with conflicts', () =>
{
const day = 'test day';
const reason = 'test reason';
const workingDay = 'No';
const conflicts = '<span>this is a conflict</span>';
addHolidayToList(day, reason, workingDay, conflicts);
const table = $('#holiday-list-table tbody');
const rowsLength = table.find('tr').length;
expect(rowsLength).toBe(1);
const firstCell = table.find('td')[0].innerHTML;
const secondCell = table.find('td')[1].innerHTML;
const thirdCell = table.find('td')[2].innerHTML;
const conflictsCell = table.find('td')[3].innerHTML;
const fourthCell = table.find('td')[4].innerHTML;
const fourthCellContent = `<label class="switch"><input type="checkbox" name="import-${day}" id="import-${day}"><span class="slider round"></span></label>`;
expect(firstCell).toBe(day);
expect(secondCell).toBe(reason);
expect(thirdCell).toBe(workingDay);
expect(conflictsCell).toBe(conflicts);
expect(fourthCell).toEqual(fourthCellContent);
});
});

describe('Clearing the table', () =>
Expand Down
Loading

0 comments on commit f659c54

Please sign in to comment.