Added more tests
This commit is contained in:
parent
df79d78165
commit
0b84da8b93
4 changed files with 162 additions and 46 deletions
|
@ -26,12 +26,12 @@ function PreviewEventDashboardItem(props) {
|
||||||
{hasImage ?
|
{hasImage ?
|
||||||
<Card.Title
|
<Card.Title
|
||||||
title={props.event['title']}
|
title={props.event['title']}
|
||||||
subtitle={PlanningEventManager.getFormattedEventTime(props.event)}
|
subtitle={PlanningEventManager.getFormattedEventTime(props.event['date_begin'], props.event['date_end'])}
|
||||||
left={getImage}
|
left={getImage}
|
||||||
/> :
|
/> :
|
||||||
<Card.Title
|
<Card.Title
|
||||||
title={props.event['title']}
|
title={props.event['title']}
|
||||||
subtitle={PlanningEventManager.getFormattedEventTime(props.event)}
|
subtitle={PlanningEventManager.getFormattedEventTime(props.event['date_begin'], props.event['date_end'])}
|
||||||
/>}
|
/>}
|
||||||
{!isEmpty ?
|
{!isEmpty ?
|
||||||
<Card.Content style={{
|
<Card.Content style={{
|
||||||
|
|
|
@ -10,6 +10,7 @@ import {Text, withTheme} from 'react-native-paper';
|
||||||
import FeedItem from "../components/FeedItem";
|
import FeedItem from "../components/FeedItem";
|
||||||
import SquareDashboardItem from "../components/SquareDashboardItem";
|
import SquareDashboardItem from "../components/SquareDashboardItem";
|
||||||
import PreviewEventDashboardItem from "../components/PreviewEventDashboardItem";
|
import PreviewEventDashboardItem from "../components/PreviewEventDashboardItem";
|
||||||
|
import PlanningEventManager from "../utils/PlanningEventManager";
|
||||||
// import DATA from "../dashboard_data.json";
|
// import DATA from "../dashboard_data.json";
|
||||||
|
|
||||||
|
|
||||||
|
@ -152,23 +153,6 @@ class HomeScreen extends React.Component<Props> {
|
||||||
return this.getDashboardMiddleItem(content);
|
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:
|
* Get the time limit depending on the current day:
|
||||||
* 17:30 for every day of the week except for thursday 11:30
|
* 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
|
* @return {number} The number of milliseconds
|
||||||
*/
|
*/
|
||||||
getEventDuration(event: Object): number {
|
getEventDuration(event: Object): number {
|
||||||
let start = this.stringToDate(event['date_begin']);
|
let start = PlanningEventManager.stringToDate(event['date_begin']);
|
||||||
let end = this.stringToDate(event['date_end']);
|
let end = PlanningEventManager.stringToDate(event['date_end']);
|
||||||
let duration = 0;
|
let duration = 0;
|
||||||
if (start !== undefined && start !== null && end !== undefined && end !== null)
|
if (start !== undefined && start !== null && end !== undefined && end !== null)
|
||||||
duration = end - start;
|
duration = end - start;
|
||||||
|
@ -209,7 +193,7 @@ class HomeScreen extends React.Component<Props> {
|
||||||
getEventsAfterLimit(events: Object, limit: Date): Array<Object> {
|
getEventsAfterLimit(events: Object, limit: Date): Array<Object> {
|
||||||
let validEvents = [];
|
let validEvents = [];
|
||||||
for (let event of events) {
|
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) {
|
if (startDate !== undefined && startDate !== null && startDate >= limit) {
|
||||||
validEvents.push(event);
|
validEvents.push(event);
|
||||||
}
|
}
|
||||||
|
@ -244,8 +228,8 @@ class HomeScreen extends React.Component<Props> {
|
||||||
let validEvents = [];
|
let validEvents = [];
|
||||||
let now = new Date();
|
let now = new Date();
|
||||||
for (let event of events) {
|
for (let event of events) {
|
||||||
let startDate = this.stringToDate(event['date_begin']);
|
let startDate = PlanningEventManager.stringToDate(event['date_begin']);
|
||||||
let endDate = this.stringToDate(event['date_end']);
|
let endDate = PlanningEventManager.stringToDate(event['date_end']);
|
||||||
if (startDate !== undefined && startDate !== null) {
|
if (startDate !== undefined && startDate !== null) {
|
||||||
if (startDate > now)
|
if (startDate > now)
|
||||||
validEvents.push(event);
|
validEvents.push(event);
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
|
||||||
export default class PlanningEventManager {
|
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) {
|
static isEventBefore(event1: Object, event2: Object) {
|
||||||
let date1 = new Date();
|
let date1 = new Date();
|
||||||
let date2 = 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
|
* Checks if the given date string is in the format
|
||||||
* @param dateString
|
* YYYY-MM-DD HH:MM:SS
|
||||||
* @return {Date}
|
*
|
||||||
|
* @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();
|
let date = new Date();
|
||||||
if (dateString === undefined || dateString === null)
|
if (PlanningEventManager.isDateStringFormatValid(dateString)) {
|
||||||
date = undefined;
|
let stringArray = dateString.split(' ');
|
||||||
else if (dateString.split(' ').length > 1) {
|
let dateArray = stringArray[0].split('-');
|
||||||
let timeStr = dateString.split(' ')[1];
|
let timeArray = stringArray[1].split(':');
|
||||||
date.setHours(parseInt(timeStr.split(':')[0]), parseInt(timeStr.split(':')[1]), 0);
|
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
|
} else
|
||||||
date = undefined;
|
date = undefined;
|
||||||
|
|
||||||
return date;
|
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 = '';
|
* Returns a string corresponding to the event start and end times in the following format:
|
||||||
let startDate = PlanningEventManager.stringToDate(event['date_begin']);
|
*
|
||||||
let endDate = PlanningEventManager.stringToDate(event['date_end']);
|
* HH:MM - HH:MM
|
||||||
if (startDate !== undefined && startDate !== null && endDate !== undefined && endDate !== null)
|
*
|
||||||
formattedStr = PlanningEventManager.padStr(startDate.getHours()) + ':' + PlanningEventManager.padStr(startDate.getMinutes()) +
|
* If the end date is not specified or is equal to start time, only start time will be shown.
|
||||||
' - ' + PlanningEventManager.padStr(endDate.getHours()) + ':' + PlanningEventManager.padStr(endDate.getMinutes());
|
*
|
||||||
else if (startDate !== undefined && startDate !== null)
|
* If the end date is not on the same day, 00:00 will be shown as end time
|
||||||
formattedStr = PlanningEventManager.padStr(startDate.getHours()) + ':' + PlanningEventManager.padStr(startDate.getMinutes());
|
*
|
||||||
|
* @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
|
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) {
|
if (description !== undefined && description !== null) {
|
||||||
return description
|
return description
|
||||||
.split('<p>').join('') // Equivalent to a replace all
|
.split('<p>').join('') // Equivalent to a replace all
|
||||||
|
|
|
@ -16,4 +16,68 @@ test('isDescriptionEmpty', () => {
|
||||||
expect(PlanningEventManager.isDescriptionEmpty("<p>coucou</p>")).toBeFalse();
|
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');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue