diff --git a/__tests__/__main__/calendar-aux.js b/__tests__/__main__/calendar-aux.js new file mode 100644 index 000000000..cbdd03fa8 --- /dev/null +++ b/__tests__/__main__/calendar-aux.js @@ -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(); + 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()', () => { + expect(getDaysEntries(2020, 3, 3)).toStrictEqual([undefined, undefined, undefined, undefined]); + }); + }); +}); + + diff --git a/js/calendar-aux.js b/js/calendar-aux.js new file mode 100644 index 000000000..d9a7df160 --- /dev/null +++ b/js/calendar-aux.js @@ -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 +}; \ No newline at end of file diff --git a/js/calendar.js b/js/calendar.js index c698bc8d1..9e139cb8b 100644 --- a/js/calendar.js +++ b/js/calendar.js @@ -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, @@ -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. */ @@ -446,7 +421,7 @@ class Calendar { ''; return code; } - + /* * Returns the html code for the row with workng days, month total and balance */ @@ -499,14 +474,14 @@ class Calendar { '' + Calendar._getTotalCode(year, month, day, 'day-total') + '' + '\n'; return waivedLineHtmlCode; - } + } var htmlCode = '' + '' + this.options.weekabbrs[weekDay] + '' + - '' + + '' + ' ' + day + ' ' + - '' + + '' + '' + '' + Calendar._getInputCode(year, month, day, 'day-begin') + '' + '' + Calendar._getInputCode(year, month, day, 'lunch-begin') + '' + @@ -559,7 +534,7 @@ class Calendar { '' + '\n'; } - + /* * Returns the last valid day before the current one, to print the balance row */ @@ -574,7 +549,7 @@ class Calendar { balanceRowPosition = day; } } - + return balanceRowPosition; } @@ -588,7 +563,7 @@ class Calendar { html += ''; 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) { @@ -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). */