forked from vergnet/application-amicale
Moved functions to manager and wrote tests
This commit is contained in:
parent
3a301bcbef
commit
7a3d5f16b1
3 changed files with 53 additions and 28 deletions
|
@ -59,7 +59,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
|
|||
onAgendaRef: Function;
|
||||
onCalendarToggled: Function;
|
||||
onBackButtonPressAndroid: Function;
|
||||
currentDate = this.getCurrentDate();
|
||||
currentDate = PlanningEventManager.getCurrentDateString();
|
||||
|
||||
constructor(props: any) {
|
||||
super(props);
|
||||
|
@ -107,23 +107,13 @@ export default class PlanningScreen extends React.Component<Props, State> {
|
|||
}
|
||||
};
|
||||
|
||||
getCurrentDate() {
|
||||
let today = new Date();
|
||||
return this.getFormattedDate(today);
|
||||
}
|
||||
|
||||
getFormattedDate(date: Date) {
|
||||
let dd = String(date.getDate()).padStart(2, '0');
|
||||
let mm = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
|
||||
let yyyy = date.getFullYear();
|
||||
return yyyy + '-' + mm + '-' + dd;
|
||||
}
|
||||
|
||||
generateEmptyCalendar() {
|
||||
let end = new Date(new Date().setMonth(new Date().getMonth() + AGENDA_MONTH_SPAN + 1));
|
||||
let daysOfYear = {};
|
||||
for (let d = new Date(); d <= end; d.setDate(d.getDate() + 1)) {
|
||||
daysOfYear[this.getFormattedDate(new Date(d))] = []
|
||||
daysOfYear[PlanningEventManager.dateToString(new Date(d))] = []
|
||||
}
|
||||
return daysOfYear;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,17 @@ export default class PlanningEventManager {
|
|||
// Regex used to check date string validity
|
||||
static dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
||||
|
||||
/**
|
||||
* Gets the current day string representation in the format
|
||||
* YYYY-MM-DD
|
||||
*
|
||||
* @return {string} The string representation
|
||||
*/
|
||||
static getCurrentDateString() {
|
||||
return PlanningEventManager.dateToString(new Date());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if the given date is before the other.
|
||||
*
|
||||
|
@ -27,7 +38,7 @@ export default class PlanningEventManager {
|
|||
* @return {string|undefined} Date in format YYYY:MM:DD or undefined if given string is invalid
|
||||
*/
|
||||
static getDateOnlyString(dateString: ?string) {
|
||||
if (PlanningEventManager.isDateStringFormatValid(dateString))
|
||||
if (PlanningEventManager.isEventDateStringFormatValid(dateString))
|
||||
return dateString.split(" ")[0];
|
||||
else
|
||||
return undefined;
|
||||
|
@ -40,7 +51,7 @@ export default class PlanningEventManager {
|
|||
* @param dateString The string to check
|
||||
* @return {boolean}
|
||||
*/
|
||||
static isDateStringFormatValid(dateString: ?string) {
|
||||
static isEventDateStringFormatValid(dateString: ?string) {
|
||||
return dateString !== undefined
|
||||
&& dateString !== null
|
||||
&& PlanningEventManager.dateRegExp.test(dateString);
|
||||
|
@ -55,7 +66,7 @@ export default class PlanningEventManager {
|
|||
*/
|
||||
static stringToDate(dateString: ?string): Date | undefined {
|
||||
let date = new Date();
|
||||
if (PlanningEventManager.isDateStringFormatValid(dateString)) {
|
||||
if (PlanningEventManager.isEventDateStringFormatValid(dateString)) {
|
||||
let stringArray = dateString.split(' ');
|
||||
let dateArray = stringArray[0].split('-');
|
||||
let timeArray = stringArray[1].split(':');
|
||||
|
@ -76,6 +87,20 @@ export default class PlanningEventManager {
|
|||
return date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a date object to a string in the format
|
||||
* YYYY-MM-DD
|
||||
*
|
||||
* @param date The date object to convert
|
||||
* @return {string} The converted string
|
||||
*/
|
||||
static dateToString(date: Date) {
|
||||
let dd = String(date.getDate()).padStart(2, '0');
|
||||
let mm = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
|
||||
let yyyy = date.getFullYear();
|
||||
return yyyy + '-' + mm + '-' + dd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a padded string for the given number if it is lower than 10
|
||||
*
|
||||
|
|
|
@ -27,20 +27,20 @@ test('toPaddedString', () => {
|
|||
expect(PlanningEventManager.toPaddedString(100)).toBe("100");
|
||||
});
|
||||
|
||||
test('isDateStringFormatValid', () => {
|
||||
expect(PlanningEventManager.isDateStringFormatValid("2020-03-21 09:00:00")).toBeTrue();
|
||||
expect(PlanningEventManager.isDateStringFormatValid("3214-64-12 01:16:65")).toBeTrue();
|
||||
test('isEventDateStringFormatValid', () => {
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21 09:00:00")).toBeTrue();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 01:16:65")).toBeTrue();
|
||||
|
||||
expect(PlanningEventManager.isDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
|
||||
expect(PlanningEventManager.isDateStringFormatValid("3214-f4-12 01:16:65")).toBeFalse();
|
||||
expect(PlanningEventManager.isDateStringFormatValid("sqdd 09:00:00")).toBeFalse();
|
||||
expect(PlanningEventManager.isDateStringFormatValid("2020-03-21")).toBeFalse();
|
||||
expect(PlanningEventManager.isDateStringFormatValid("2020-03-21 truc")).toBeFalse();
|
||||
expect(PlanningEventManager.isDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
|
||||
expect(PlanningEventManager.isDateStringFormatValid("garbage")).toBeFalse();
|
||||
expect(PlanningEventManager.isDateStringFormatValid("")).toBeFalse();
|
||||
expect(PlanningEventManager.isDateStringFormatValid(undefined)).toBeFalse();
|
||||
expect(PlanningEventManager.isDateStringFormatValid(null)).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("3214-f4-12 01:16:65")).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("sqdd 09:00:00")).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21")).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21 truc")).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("garbage")).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid("")).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid(undefined)).toBeFalse();
|
||||
expect(PlanningEventManager.isEventDateStringFormatValid(null)).toBeFalse();
|
||||
});
|
||||
|
||||
test('stringToDate', () => {
|
||||
|
@ -124,3 +124,13 @@ test('isEventBefore', () => {
|
|||
undefined, undefined)).toBeFalse();
|
||||
});
|
||||
|
||||
test('dateToString', () => {
|
||||
let testDate = new Date();
|
||||
testDate.setFullYear(2020, 2, 21);
|
||||
expect(PlanningEventManager.dateToString(testDate)).toBe("2020-03-21");
|
||||
testDate.setFullYear(2021, 0, 12);
|
||||
expect(PlanningEventManager.dateToString(testDate)).toBe("2021-01-12");
|
||||
testDate.setFullYear(2022, 11, 31);
|
||||
expect(PlanningEventManager.dateToString(testDate)).toBe("2022-12-31");
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue