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;
|
onAgendaRef: Function;
|
||||||
onCalendarToggled: Function;
|
onCalendarToggled: Function;
|
||||||
onBackButtonPressAndroid: Function;
|
onBackButtonPressAndroid: Function;
|
||||||
currentDate = this.getCurrentDate();
|
currentDate = PlanningEventManager.getCurrentDateString();
|
||||||
|
|
||||||
constructor(props: any) {
|
constructor(props: any) {
|
||||||
super(props);
|
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() {
|
generateEmptyCalendar() {
|
||||||
let end = new Date(new Date().setMonth(new Date().getMonth() + AGENDA_MONTH_SPAN + 1));
|
let end = new Date(new Date().setMonth(new Date().getMonth() + AGENDA_MONTH_SPAN + 1));
|
||||||
let daysOfYear = {};
|
let daysOfYear = {};
|
||||||
for (let d = new Date(); d <= end; d.setDate(d.getDate() + 1)) {
|
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;
|
return daysOfYear;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,17 @@ export default class PlanningEventManager {
|
||||||
// Regex used to check date string validity
|
// Regex used to check date string validity
|
||||||
static dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
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.
|
* 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
|
* @return {string|undefined} Date in format YYYY:MM:DD or undefined if given string is invalid
|
||||||
*/
|
*/
|
||||||
static getDateOnlyString(dateString: ?string) {
|
static getDateOnlyString(dateString: ?string) {
|
||||||
if (PlanningEventManager.isDateStringFormatValid(dateString))
|
if (PlanningEventManager.isEventDateStringFormatValid(dateString))
|
||||||
return dateString.split(" ")[0];
|
return dateString.split(" ")[0];
|
||||||
else
|
else
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -40,7 +51,7 @@ export default class PlanningEventManager {
|
||||||
* @param dateString The string to check
|
* @param dateString The string to check
|
||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
*/
|
*/
|
||||||
static isDateStringFormatValid(dateString: ?string) {
|
static isEventDateStringFormatValid(dateString: ?string) {
|
||||||
return dateString !== undefined
|
return dateString !== undefined
|
||||||
&& dateString !== null
|
&& dateString !== null
|
||||||
&& PlanningEventManager.dateRegExp.test(dateString);
|
&& PlanningEventManager.dateRegExp.test(dateString);
|
||||||
|
@ -55,7 +66,7 @@ export default class PlanningEventManager {
|
||||||
*/
|
*/
|
||||||
static stringToDate(dateString: ?string): Date | undefined {
|
static stringToDate(dateString: ?string): Date | undefined {
|
||||||
let date = new Date();
|
let date = new Date();
|
||||||
if (PlanningEventManager.isDateStringFormatValid(dateString)) {
|
if (PlanningEventManager.isEventDateStringFormatValid(dateString)) {
|
||||||
let stringArray = dateString.split(' ');
|
let stringArray = dateString.split(' ');
|
||||||
let dateArray = stringArray[0].split('-');
|
let dateArray = stringArray[0].split('-');
|
||||||
let timeArray = stringArray[1].split(':');
|
let timeArray = stringArray[1].split(':');
|
||||||
|
@ -76,6 +87,20 @@ export default class PlanningEventManager {
|
||||||
return date;
|
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
|
* 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");
|
expect(PlanningEventManager.toPaddedString(100)).toBe("100");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('isDateStringFormatValid', () => {
|
test('isEventDateStringFormatValid', () => {
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("2020-03-21 09:00:00")).toBeTrue();
|
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21 09:00:00")).toBeTrue();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("3214-64-12 01:16:65")).toBeTrue();
|
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 01:16:65")).toBeTrue();
|
||||||
|
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("3214-f4-12 01:16:65")).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid("3214-f4-12 01:16:65")).toBeFalse();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("sqdd 09:00:00")).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid("sqdd 09:00:00")).toBeFalse();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("2020-03-21")).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21")).toBeFalse();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("2020-03-21 truc")).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid("2020-03-21 truc")).toBeFalse();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid("3214-64-12 1:16:65")).toBeFalse();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("garbage")).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid("garbage")).toBeFalse();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid("")).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid("")).toBeFalse();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid(undefined)).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid(undefined)).toBeFalse();
|
||||||
expect(PlanningEventManager.isDateStringFormatValid(null)).toBeFalse();
|
expect(PlanningEventManager.isEventDateStringFormatValid(null)).toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('stringToDate', () => {
|
test('stringToDate', () => {
|
||||||
|
@ -124,3 +124,13 @@ test('isEventBefore', () => {
|
||||||
undefined, undefined)).toBeFalse();
|
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