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

Improving coverage for auxiliary functions in calendar #204

Closed
wants to merge 6 commits into from
Closed
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
60 changes: 60 additions & 0 deletions __tests__/__main__/calendar-aux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable no-undef */

const { hasInputError,
getDaysEntries
} = require('../../js/calendar-aux');
const Store = require('electron-store');


describe('Calendar helper functions', () => {
process.env.NODE_ENV = 'test';

describe('hasInputError(dayBegin, lunchBegin, lunchEnd, dayEnd)', () => {
test('Test scenarios where there is no error on the inputs', () => {
expect(hasInputError('00:00', '12:00', '13:00', '20:00')).not.toBeTruthy();
expect(hasInputError('00:00', '12:00', '13:00', '')).not.toBeTruthy();
expect(hasInputError('00:00', '12:00', '', '')).not.toBeTruthy();
expect(hasInputError('00:00', '', '', '')).not.toBeTruthy();
expect(hasInputError('', '', '', '')).not.toBeTruthy();
expect(hasInputError('00:00', '12:00', '', '20:00')).not.toBeTruthy();
expect(hasInputError('00:00', '', '13:00', '20:00')).not.toBeTruthy();
thamara marked this conversation as resolved.
Show resolved Hide resolved
expect(hasInputError('00:00', '', '', '20:00')).not.toBeTruthy();
});

test('Test scenarios where there is error on the inputs', () => {
expect(hasInputError('not-valid-hour', '', '', 'not-valid-hour')).toBeTruthy();
expect(hasInputError('23:00', '', '', '00:00')).toBeTruthy();
expect(hasInputError('', '23:00', '', '00:00')).toBeTruthy();
expect(hasInputError('', '', '23:00', '00:00')).toBeTruthy();
});
});

const store = new Store();
store.clear();
const regularEntries = {
'2020-3-1-day-begin': '08:00',
'2020-3-1-day-end': '17:00',
'2020-3-1-day-total': '08:00',
'2020-3-1-lunch-begin': '12:00',
'2020-3-1-lunch-end': '13:00',
'2020-3-1-lunch-total': '01:00',
'2020-3-2-day-begin': '10:00',
'2020-3-2-day-end': '18:00',
'2020-3-2-day-total': '08:00',
};
store.set(regularEntries);

describe('getDaysEntries(year, month, day)', () => {
test('getDaysEntries(2020, 3, 1)', () => {
expect(getDaysEntries(2020, 3, 1)).toStrictEqual(['08:00', '12:00', '13:00', '17:00']);
});
test('getDaysEntries(2020, 3, 2)', () => {
expect(getDaysEntries(2020, 3, 2)).toStrictEqual(['10:00', undefined, undefined, '18:00']);
});
test('getDaysEntries(<not registered input>)', () => {
expect(getDaysEntries(2020, 3, 3)).toStrictEqual([undefined, undefined, undefined, undefined]);
});
});
});


66 changes: 66 additions & 0 deletions js/calendar-aux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const {
validateTime,
} = require('./time-math.js');
const Store = require('electron-store');

/*
* Check validity of a time input
*/
function isInvalidInputTime(time) {
if (time === undefined || time === '') {
return false;
}
return !validateTime(time);
}

/*
* Analyze the inputs of a day, and return if it has an error.
* An error means that an input earlier in the day is higher than another.
*/
function hasInputError(dayBegin, lunchBegin, lunchEnd, dayEnd) {
var dayValues = new Array();

if (isInvalidInputTime(dayBegin) ||
isInvalidInputTime(lunchBegin) ||
isInvalidInputTime(lunchEnd) ||
isInvalidInputTime(dayEnd)) {
return true;
}

if (validateTime(dayBegin)) {
dayValues.push(dayBegin);
}
if (validateTime(lunchBegin)) {
dayValues.push(lunchBegin);
}
if (validateTime(lunchEnd)) {
dayValues.push(lunchEnd);
}
if (validateTime(dayEnd)) {
dayValues.push(dayEnd);
}
for (var index = 0; index < dayValues.length; index++) {
if (index > 0 && (dayValues[index-1] >= dayValues[index])) {
return true;
}
}
return false;
}

/*
* Returns the entries for the day.
*/
function getDaysEntries(year, month, day) {
const db = new Store();
var dayStr = year + '-' + month + '-' + day + '-';
return [db.get(dayStr + 'day-begin'),
db.get(dayStr + 'lunch-begin'),
db.get(dayStr + 'lunch-end'),
db.get(dayStr + 'day-end')];
}


module.exports = {
hasInputError,
getDaysEntries
};
52 changes: 8 additions & 44 deletions js/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { notify } = require('./js/notification.js');
const { getUserPreferences, showDay } = require('./js/user-preferences.js');
const { applyTheme } = require('./js/themes.js');
const { getDateStr } = require('./js/date-aux.js');
const { hasInputError, getDaysEntries } = require('./js/calendar-aux.js');
const {
formatDayId,
sendWaiverDay,
Expand Down Expand Up @@ -50,32 +51,6 @@ function getHoursPerDay() {
return preferences['hours-per-day'];
}

/*
* Analyze the inputs of a day, and return if it has an error.
* An error means that an input earlier in the day is higher than another.
*/
function hasInputError(dayBegin, lunchBegin, lunchEnd, dayEnd) {
var dayValues = new Array();
if (validateTime(dayBegin)) {
dayValues.push(dayBegin);
}
if (validateTime(lunchBegin)) {
dayValues.push(lunchBegin);
}
if (validateTime(lunchEnd)) {
dayValues.push(lunchEnd);
}
if (validateTime(dayEnd)) {
dayValues.push(dayEnd);
}
for (var index = 0; index < dayValues.length; index++) {
if (index > 0 && (dayValues[index-1] >= dayValues[index])) {
return true;
}
}
return false;
}

/*
* Display next month of the calendar.
*/
Expand Down Expand Up @@ -446,7 +421,7 @@ class Calendar {
'</tr>';
return code;
}

/*
* Returns the html code for the row with workng days, month total and balance
*/
Expand Down Expand Up @@ -499,14 +474,14 @@ class Calendar {
'<td class="ti ti-total">' + Calendar._getTotalCode(year, month, day, 'day-total') + '</td>' +
'</tr>\n';
return waivedLineHtmlCode;
}
}

var htmlCode =
'<tr'+ (isToday ? ' class="isToday"' : '') + ' id="' + trID + '">' +
'<td class="weekday waiver-trigger ti" title="Add a waiver for this day">' + this.options.weekabbrs[weekDay] + '</td>' +
'<td class="day ti">' +
'<td class="day ti">' +
'<span class="day-number"> ' + day + ' </span>' +
'<img src="assets/waiver.svg" height="15" class="waiver-img">' +
'<img src="assets/waiver.svg" height="15" class="waiver-img">' +
'</td>' +
'<td class="ti">' + Calendar._getInputCode(year, month, day, 'day-begin') + '</td>' +
'<td class="ti">' + Calendar._getInputCode(year, month, day, 'lunch-begin') + '</td>' +
Expand Down Expand Up @@ -559,7 +534,7 @@ class Calendar {
'</tr>' +
'</thead>\n';
}

/*
* Returns the last valid day before the current one, to print the balance row
*/
Expand All @@ -574,7 +549,7 @@ class Calendar {
balanceRowPosition = day;
}
}

return balanceRowPosition;
}

Expand All @@ -588,7 +563,7 @@ class Calendar {
html += '<table class="table-body">';
html += this._getTableHeaderCode();
var balanceRowPosition = this._getBalanceRowPosition();

for (var day = 1; day <= monthLength; ++day) {
html += this._getInputsRowCode(this.year, this.month, day);
if (day === balanceRowPosition) {
Expand All @@ -602,17 +577,6 @@ class Calendar {
}
}

/*
* Returns the entries for the day.
*/
function getDaysEntries(year, month, day) {
var dayStr = year + '-' + month + '-' + day + '-';
return [store.get(dayStr + 'day-begin'),
store.get(dayStr + 'lunch-begin'),
store.get(dayStr + 'lunch-end'),
store.get(dayStr + 'day-end')];
}

/*
* Returns the entries for the day, from HTML (for performance).
*/
Expand Down