Added more tests

This commit is contained in:
Arnaud Vergnet 2020-03-21 17:23:59 +01:00
parent df79d78165
commit 0b84da8b93
4 changed files with 162 additions and 46 deletions

View file

@ -26,12 +26,12 @@ function PreviewEventDashboardItem(props) {
{hasImage ?
<Card.Title
title={props.event['title']}
subtitle={PlanningEventManager.getFormattedEventTime(props.event)}
subtitle={PlanningEventManager.getFormattedEventTime(props.event['date_begin'], props.event['date_end'])}
left={getImage}
/> :
<Card.Title
title={props.event['title']}
subtitle={PlanningEventManager.getFormattedEventTime(props.event)}
subtitle={PlanningEventManager.getFormattedEventTime(props.event['date_begin'], props.event['date_end'])}
/>}
{!isEmpty ?
<Card.Content style={{

View file

@ -10,6 +10,7 @@ import {Text, withTheme} from 'react-native-paper';
import FeedItem from "../components/FeedItem";
import SquareDashboardItem from "../components/SquareDashboardItem";
import PreviewEventDashboardItem from "../components/PreviewEventDashboardItem";
import PlanningEventManager from "../utils/PlanningEventManager";
// import DATA from "../dashboard_data.json";
@ -152,23 +153,6 @@ class HomeScreen extends React.Component<Props> {
return this.getDashboardMiddleItem(content);
}
/**
* Convert the date string given by in the event list json to a date object
* @param dateString
* @return {Date}
*/
stringToDate(dateString: ?string): ?Date {
let date = new Date();
if (dateString === undefined || dateString === null)
date = undefined;
else if (dateString.split(' ').length > 1) {
let timeStr = dateString.split(' ')[1];
date.setHours(parseInt(timeStr.split(':')[0]), parseInt(timeStr.split(':')[1]), 0);
} else
date = undefined;
return date;
}
/**
* Get the time limit depending on the current day:
* 17:30 for every day of the week except for thursday 11:30
@ -191,8 +175,8 @@ class HomeScreen extends React.Component<Props> {
* @return {number} The number of milliseconds
*/
getEventDuration(event: Object): number {
let start = this.stringToDate(event['date_begin']);
let end = this.stringToDate(event['date_end']);
let start = PlanningEventManager.stringToDate(event['date_begin']);
let end = PlanningEventManager.stringToDate(event['date_end']);
let duration = 0;
if (start !== undefined && start !== null && end !== undefined && end !== null)
duration = end - start;
@ -209,7 +193,7 @@ class HomeScreen extends React.Component<Props> {
getEventsAfterLimit(events: Object, limit: Date): Array<Object> {
let validEvents = [];
for (let event of events) {
let startDate = this.stringToDate(event['date_begin']);
let startDate = PlanningEventManager.stringToDate(event['date_begin']);
if (startDate !== undefined && startDate !== null && startDate >= limit) {
validEvents.push(event);
}
@ -244,8 +228,8 @@ class HomeScreen extends React.Component<Props> {
let validEvents = [];
let now = new Date();
for (let event of events) {
let startDate = this.stringToDate(event['date_begin']);
let endDate = this.stringToDate(event['date_end']);
let startDate = PlanningEventManager.stringToDate(event['date_begin']);
let endDate = PlanningEventManager.stringToDate(event['date_end']);
if (startDate !== undefined && startDate !== null) {
if (startDate > now)
validEvents.push(event);

View file

@ -1,5 +1,8 @@
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}$/;
static isEventBefore(event1: Object, event2: Object) {
let date1 = new Date();
let date2 = new Date();
@ -41,39 +44,104 @@ export default class PlanningEventManager {
}
/**
* Convert the date string given by in the event list json to a date object
* @param dateString
* @return {Date}
* Checks if the given date string is in the format
* YYYY-MM-DD HH:MM:SS
*
* @param dateString The string to check
* @return {boolean}
*/
static stringToDate(dateString: ?string): ?Date {
static isDateStringFormatValid(dateString: ?string) {
return dateString !== undefined
&& dateString !== null
&& PlanningEventManager.dateRegExp.test(dateString);
}
/**
* Converts the given date string to a date object.<br>
* Accepted format: YYYY-MM-DD HH:MM:SS
*
* @param dateString The string to convert
* @return {Date} The date object or undefined if the given string is invalid
*/
static stringToDate(dateString: ?string): Date | undefined {
let date = new Date();
if (dateString === undefined || dateString === null)
date = undefined;
else if (dateString.split(' ').length > 1) {
let timeStr = dateString.split(' ')[1];
date.setHours(parseInt(timeStr.split(':')[0]), parseInt(timeStr.split(':')[1]), 0);
if (PlanningEventManager.isDateStringFormatValid(dateString)) {
let stringArray = dateString.split(' ');
let dateArray = stringArray[0].split('-');
let timeArray = stringArray[1].split(':');
date.setFullYear(
parseInt(dateArray[0]),
parseInt(dateArray[1]) - 1, // Month range from 0 to 11
parseInt(dateArray[2])
);
date.setHours(
parseInt(timeArray[0]),
parseInt(timeArray[1]),
parseInt(timeArray[2]),
0,
);
} else
date = undefined;
return date;
}
static padStr(i: number) {
return (i < 10) ? "0" + i : "" + i;
/**
* Returns a padded string for the given number if it is lower than 10
*
* @param i The string to be converted
* @return {string}
*/
static toPaddedString(i: number): string {
return (i < 10 && i >= 0) ? "0" + i.toString(10) : i.toString(10);
}
static getFormattedEventTime(event: Object): string {
let formattedStr = '';
let startDate = PlanningEventManager.stringToDate(event['date_begin']);
let endDate = PlanningEventManager.stringToDate(event['date_end']);
if (startDate !== undefined && startDate !== null && endDate !== undefined && endDate !== null)
formattedStr = PlanningEventManager.padStr(startDate.getHours()) + ':' + PlanningEventManager.padStr(startDate.getMinutes()) +
' - ' + PlanningEventManager.padStr(endDate.getHours()) + ':' + PlanningEventManager.padStr(endDate.getMinutes());
else if (startDate !== undefined && startDate !== null)
formattedStr = PlanningEventManager.padStr(startDate.getHours()) + ':' + PlanningEventManager.padStr(startDate.getMinutes());
/**
* Returns a string corresponding to the event start and end times in the following format:
*
* HH:MM - HH:MM
*
* If the end date is not specified or is equal to start time, only start time will be shown.
*
* If the end date is not on the same day, 00:00 will be shown as end time
*
* @param start Start time in YYYY-MM-DD HH:MM:SS format
* @param end End time in YYYY-MM-DD HH:MM:SS format
* @return {string} Formatted string or "/ - /" on error
*/
static getFormattedEventTime(start: ?string, end: ?string): string {
let formattedStr = '/ - /';
let startDate = PlanningEventManager.stringToDate(start);
let endDate = PlanningEventManager.stringToDate(end);
if (startDate !== undefined && endDate !== undefined && startDate.getTime() !== endDate.getTime()) {
formattedStr = PlanningEventManager.toPaddedString(startDate.getHours()) + ':'
+ PlanningEventManager.toPaddedString(startDate.getMinutes()) + ' - ';
if (endDate.getFullYear() > startDate.getFullYear()
|| endDate.getMonth() > startDate.getMonth()
|| endDate.getDate() > startDate.getDate())
formattedStr += '00:00';
else
formattedStr += PlanningEventManager.toPaddedString(endDate.getHours()) + ':'
+ PlanningEventManager.toPaddedString(endDate.getMinutes());
} else if (startDate !== undefined)
formattedStr =
PlanningEventManager.toPaddedString(startDate.getHours()) + ':'
+ PlanningEventManager.toPaddedString(startDate.getMinutes());
return formattedStr
}
static isDescriptionEmpty (description: ?string) {
/**
* Checks if the given description can be considered empty.
* <br>
* An empty description is composed only of whitespace, <b>br</b> or <b>p</b> tags
*
*
* @param description The text to check
* @return {boolean}
*/
static isDescriptionEmpty(description: ?string): boolean {
if (description !== undefined && description !== null) {
return description
.split('<p>').join('') // Equivalent to a replace all

View file

@ -16,4 +16,68 @@ test('isDescriptionEmpty', () => {
expect(PlanningEventManager.isDescriptionEmpty("<p>coucou</p>")).toBeFalse();
});
test('toPaddedString', () => {
expect(PlanningEventManager.toPaddedString(-1)).toBe("-1");
expect(PlanningEventManager.toPaddedString(0)).toBe("00");
expect(PlanningEventManager.toPaddedString(1)).toBe("01");
expect(PlanningEventManager.toPaddedString(2)).toBe("02");
expect(PlanningEventManager.toPaddedString(10)).toBe("10");
expect(PlanningEventManager.toPaddedString(53)).toBe("53");
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();
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();
});
test('stringToDate', () => {
let testDate = new Date();
expect(PlanningEventManager.stringToDate(undefined)).toBeUndefined();
expect(PlanningEventManager.stringToDate("")).toBeUndefined();
expect(PlanningEventManager.stringToDate("garbage")).toBeUndefined();
expect(PlanningEventManager.stringToDate("2020-03-21")).toBeUndefined();
expect(PlanningEventManager.stringToDate("09:00:00")).toBeUndefined();
expect(PlanningEventManager.stringToDate("2020-03-21 09:g0:00")).toBeUndefined();
expect(PlanningEventManager.stringToDate("2020-03-21 09:g0:")).toBeUndefined();
testDate.setFullYear(2020, 2, 21);
testDate.setHours(9, 0, 0, 0);
expect(PlanningEventManager.stringToDate("2020-03-21 09:00:00")).toEqual(testDate);
testDate.setFullYear(2020, 0, 31);
testDate.setHours(18, 30, 50, 0);
expect(PlanningEventManager.stringToDate("2020-01-31 18:30:50")).toEqual(testDate);
testDate.setFullYear(2020, 50, 50);
testDate.setHours(65, 65, 65, 0);
expect(PlanningEventManager.stringToDate("2020-51-50 65:65:65")).toEqual(testDate);
});
test('getFormattedEventTime', () => {
expect(PlanningEventManager.getFormattedEventTime(null, null))
.toBe('/ - /');
expect(PlanningEventManager.getFormattedEventTime(undefined, undefined))
.toBe('/ - /');
expect(PlanningEventManager.getFormattedEventTime("20:30:00", "23:00:00"))
.toBe('/ - /');
expect(PlanningEventManager.getFormattedEventTime("2020-03-30", "2020-03-31"))
.toBe('/ - /');
expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00:00", "2020-03-21 09:00:00"))
.toBe('09:00');
expect(PlanningEventManager.getFormattedEventTime("2020-03-21 09:00:00", "2020-03-22 17:00:00"))
.toBe('09:00 - 00:00');
expect(PlanningEventManager.getFormattedEventTime("2020-03-30 20:30:00", "2020-03-30 23:00:00"))
.toBe('20:30 - 23:00');
});