Added more tests and removed useless functions

This commit is contained in:
Arnaud Vergnet 2020-03-21 18:46:12 +01:00
parent 0b84da8b93
commit 3a301bcbef
6 changed files with 134 additions and 79 deletions

View file

@ -2,11 +2,11 @@
import * as React from 'react';
import {Image, ScrollView, View} from 'react-native';
import ThemeManager from "../../utils/ThemeManager";
import HTML from "react-native-render-html";
import {Linking} from "expo";
import PlanningEventManager from '../../utils/PlanningEventManager';
import {Card, withTheme} from 'react-native-paper';
import DateManager from "../../utils/DateManager";
type Props = {
navigation: Object,
@ -37,7 +37,10 @@ class PlanningDisplayScreen extends React.Component<Props> {
<ScrollView style={{paddingLeft: 5, paddingRight: 5}}>
<Card.Title
title={this.displayData.title}
subtitle={PlanningEventManager.getFormattedTime(this.displayData) + ' | ' + PlanningEventManager.getEventStartDate(this.displayData)}
subtitle={
PlanningEventManager.getFormattedEventTime(this.displayData["date_begin"], this.displayData["date_end"])
+ ' | '
+ DateManager.getInstance().getTranslatedDate(PlanningEventManager.getDateOnlyString(this.displayData["date_begin"]))}
/>
{this.displayData.logo !== null ?
<View style={{width: '100%', height: 300}}>

View file

@ -37,7 +37,7 @@ const AGENDA_MONTH_SPAN = 3;
*/
export default class PlanningScreen extends React.Component<Props, State> {
agendaRef: Agenda;
agendaRef: Object;
webDataManager: WebDataManager;
lastRefresh: Date;
@ -122,7 +122,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
generateEmptyCalendar() {
let end = new Date(new Date().setMonth(new Date().getMonth() + AGENDA_MONTH_SPAN + 1));
let daysOfYear = {};
for (let d = new Date(2019, 8, 1); 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))] = []
}
return daysOfYear;
@ -136,8 +136,8 @@ export default class PlanningScreen extends React.Component<Props, State> {
<Divider/>
<List.Item
title={item.title}
description={PlanningEventManager.getFormattedTime(item)}
left={props => <Avatar.Image
description={PlanningEventManager.getFormattedEventTime(item["date_begin"], item["date_end"])}
left={() => <Avatar.Image
source={{uri: item.logo}}
style={{backgroundColor: 'transparent'}}
/>}
@ -151,7 +151,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
<Divider/>
<List.Item
title={item.title}
description={PlanningEventManager.getFormattedTime(item)}
description={PlanningEventManager.getFormattedEventTime(item["date_begin"], item["date_end"])}
onPress={onPress}
/>
</View>
@ -205,8 +205,8 @@ export default class PlanningScreen extends React.Component<Props, State> {
generateEventAgenda(eventList: Array<Object>) {
let agendaItems = this.generateEmptyCalendar();
for (let i = 0; i < eventList.length; i++) {
if (agendaItems[PlanningEventManager.getEventStartDate(eventList[i])] !== undefined) {
this.pushEventInOrder(agendaItems, eventList[i], PlanningEventManager.getEventStartDate(eventList[i]));
if (PlanningEventManager.getDateOnlyString(eventList[i]["date_begin"]) !== undefined) {
this.pushEventInOrder(agendaItems, eventList[i], PlanningEventManager.getDateOnlyString(eventList[i]["date_begin"]));
}
}
this.setState({agendaItems: agendaItems})
@ -217,7 +217,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
agendaItems[startDate].push(event);
else {
for (let i = 0; i < agendaItems[startDate].length; i++) {
if (PlanningEventManager.isEventBefore(event, agendaItems[startDate][i])) {
if (PlanningEventManager.isEventBefore(event["date_begin"], agendaItems[startDate][i]["date_begin"])) {
agendaItems[startDate].splice(i, 0, event);
break;
} else if (i === agendaItems[startDate].length - 1) {
@ -228,7 +228,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
}
}
onAgendaRef(ref: Agenda) {
onAgendaRef(ref: Object) {
this.agendaRef = ref;
}

View file

@ -2,7 +2,7 @@
import * as React from 'react';
import {View} from 'react-native';
import i18n from "i18n-js";
import DateManager from "../utils/DateManager";
import WebSectionList from "../components/WebSectionList";
import {Card, Text, withTheme} from 'react-native-paper';
import AprilFoolsManager from "../utils/AprilFoolsManager";
@ -19,10 +19,6 @@ type Props = {
*/
class SelfMenuScreen extends React.Component<Props> {
// Hard code strings as toLocaleDateString does not work on current android JS engine
daysOfWeek = [];
monthsOfYear = [];
getRenderItem: Function;
getRenderSectionHeader: Function;
createDataset: Function;
@ -30,26 +26,6 @@ class SelfMenuScreen extends React.Component<Props> {
constructor(props) {
super(props);
this.daysOfWeek.push(i18n.t("date.daysOfWeek.monday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.tuesday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.wednesday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.thursday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.friday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.saturday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.sunday"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.january"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.february"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.march"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.april"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.may"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.june"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.july"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.august"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.september"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.october"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.november"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.december"));
this.getRenderItem = this.getRenderItem.bind(this);
this.getRenderSectionHeader = this.getRenderSectionHeader.bind(this);
@ -80,7 +56,7 @@ class SelfMenuScreen extends React.Component<Props> {
for (let i = 0; i < fetchedData.length; i++) {
result.push(
{
title: this.getFormattedDate(fetchedData[i].date),
title: DateManager.getInstance().getTranslatedDate(fetchedData[i].date),
data: fetchedData[i].meal[0].foodcategory,
extraData: super.state,
keyExtractor: this.getKeyExtractor,
@ -90,13 +66,6 @@ class SelfMenuScreen extends React.Component<Props> {
return result
}
getFormattedDate(dateString: string) {
let dateArray = dateString.split('-');
let date = new Date();
date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
return this.daysOfWeek[date.getDay() - 1] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
}
getRenderSectionHeader({section}: Object) {
return (
<Card style={{

50
utils/DateManager.js Normal file
View file

@ -0,0 +1,50 @@
import i18n from 'i18n-js';
export default class DateManager {
static instance: DateManager | null = null;
// Hard code strings as toLocaleDateString does not work on current android JS engine
daysOfWeek = [];
monthsOfYear = [];
constructor() {
this.daysOfWeek.push(i18n.t("date.daysOfWeek.monday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.tuesday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.wednesday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.thursday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.friday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.saturday"));
this.daysOfWeek.push(i18n.t("date.daysOfWeek.sunday"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.january"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.february"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.march"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.april"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.may"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.june"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.july"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.august"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.september"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.october"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.november"));
this.monthsOfYear.push(i18n.t("date.monthsOfYear.december"));
}
/**
* Get this class instance or create one if none is found
* @returns {DateManager}
*/
static getInstance(): DateManager {
return DateManager.instance === null ?
DateManager.instance = new DateManager() :
DateManager.instance;
}
getTranslatedDate(dateString: string) {
let dateArray = dateString.split('-');
let date = new Date();
date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
return this.daysOfWeek[date.getDay() - 1] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
}
}

View file

@ -3,44 +3,34 @@ 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();
let timeArray = PlanningEventManager.getEventStartTime(event1).split(":");
date1.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
timeArray = PlanningEventManager.getEventStartTime(event2).split(":");
date2.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
return date1 < date2;
}
static getEventStartDate(event: Object) {
return event.date_begin.split(" ")[0];
}
static getEventStartTime(event: Object) {
if (event !== undefined && Object.keys(event).length > 0 && event.date_begin !== null)
return PlanningEventManager.formatTime(event.date_begin.split(" ")[1]);
/**
* Checks if the given date is before the other.
*
* @param event1Date Event 1 date in format YYYY-MM-DD HH:MM:SS
* @param event2Date Event 2 date in format YYYY-MM-DD HH:MM:SS
* @return {boolean}
*/
static isEventBefore(event1Date: ?string, event2Date: ?string) {
let date1 = PlanningEventManager.stringToDate(event1Date);
let date2 = PlanningEventManager.stringToDate(event2Date);
if (date1 !== undefined && date2 !== undefined)
return date1 < date2;
else
return "";
return false;
}
static getEventEndTime(event: Object) {
if (event !== undefined && Object.keys(event).length > 0 && event.date_end !== null)
return PlanningEventManager.formatTime(event.date_end.split(" ")[1]);
/**
* Gets only the date part of the given event date string in the format
* YYYY-MM-DD HH:MM:SS
*
* @param dateString The string to get the date from
* @return {string|undefined} Date in format YYYY:MM:DD or undefined if given string is invalid
*/
static getDateOnlyString(dateString: ?string) {
if (PlanningEventManager.isDateStringFormatValid(dateString))
return dateString.split(" ")[0];
else
return "";
}
static getFormattedTime(event: Object) {
if (PlanningEventManager.getEventEndTime(event) !== "")
return PlanningEventManager.getEventStartTime(event) + " - " + PlanningEventManager.getEventEndTime(event);
else
return PlanningEventManager.getEventStartTime(event);
}
static formatTime(time: string) {
let array = time.split(':');
return array[0] + ':' + array[1];
return undefined;
}
/**
@ -61,7 +51,7 @@ export default class PlanningEventManager {
* 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
* @return {Date|undefined} The date object or undefined if the given string is invalid
*/
static stringToDate(dateString: ?string): Date | undefined {
let date = new Date();

View file

@ -4,6 +4,7 @@ import PlanningEventManager from "../PlanningEventManager";
test('isDescriptionEmpty', () => {
expect(PlanningEventManager.isDescriptionEmpty("")).toBeTrue();
expect(PlanningEventManager.isDescriptionEmpty(" ")).toBeTrue();
// noinspection CheckTagEmptyBody
expect(PlanningEventManager.isDescriptionEmpty("<p></p>")).toBeTrue();
expect(PlanningEventManager.isDescriptionEmpty("<p> </p>")).toBeTrue();
expect(PlanningEventManager.isDescriptionEmpty("<p><br></p>")).toBeTrue();
@ -81,3 +82,45 @@ test('getFormattedEventTime', () => {
.toBe('20:30 - 23:00');
});
test('getDateOnlyString', () => {
expect(PlanningEventManager.getDateOnlyString("2020-03-21 09:00:00")).toBe("2020-03-21");
expect(PlanningEventManager.getDateOnlyString("2021-12-15 09:00:00")).toBe("2021-12-15");
expect(PlanningEventManager.getDateOnlyString("2021-12-o5 09:00:00")).toBeUndefined();
expect(PlanningEventManager.getDateOnlyString("2021-12-15 09:")).toBeUndefined();
expect(PlanningEventManager.getDateOnlyString("2021-12-15")).toBeUndefined();
expect(PlanningEventManager.getDateOnlyString("garbage")).toBeUndefined();
});
test('isEventBefore', () => {
expect(PlanningEventManager.isEventBefore(
"2020-03-21 09:00:00", "2020-03-21 10:00:00")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:00:00", "2020-03-21 10:15:00")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:05", "2020-03-21 10:15:54")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:05", "2021-03-21 10:15:05")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:05", "2020-05-21 10:15:05")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:05", "2020-03-30 10:15:05")).toBeTrue();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:00:00", "2020-03-21 09:00:00")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:00", "2020-03-21 10:00:00")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2020-03-21 10:15:54", "2020-03-21 10:15:05")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2021-03-21 10:15:05", "2020-03-21 10:15:05")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2020-05-21 10:15:05", "2020-03-21 10:15:05")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"2020-03-30 10:15:05", "2020-03-21 10:15:05")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
"garbage", "2020-03-21 10:15:05")).toBeFalse();
expect(PlanningEventManager.isEventBefore(
undefined, undefined)).toBeFalse();
});