Browse Source

Added more tests and removed useless functions

Arnaud Vergnet 1 year ago
parent
commit
3a301bcbef

+ 5
- 2
screens/Planning/PlanningDisplayScreen.js View File

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

+ 9
- 9
screens/Planning/PlanningScreen.js View File

@@ -37,7 +37,7 @@ const AGENDA_MONTH_SPAN = 3;
37 37
  */
38 38
 export default class PlanningScreen extends React.Component<Props, State> {
39 39
 
40
-    agendaRef: Agenda;
40
+    agendaRef: Object;
41 41
     webDataManager: WebDataManager;
42 42
 
43 43
     lastRefresh: Date;
@@ -122,7 +122,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
122 122
     generateEmptyCalendar() {
123 123
         let end = new Date(new Date().setMonth(new Date().getMonth() + AGENDA_MONTH_SPAN + 1));
124 124
         let daysOfYear = {};
125
-        for (let d = new Date(2019, 8, 1); d <= end; d.setDate(d.getDate() + 1)) {
125
+        for (let d = new Date(); d <= end; d.setDate(d.getDate() + 1)) {
126 126
             daysOfYear[this.getFormattedDate(new Date(d))] = []
127 127
         }
128 128
         return daysOfYear;
@@ -136,8 +136,8 @@ export default class PlanningScreen extends React.Component<Props, State> {
136 136
                     <Divider/>
137 137
                     <List.Item
138 138
                         title={item.title}
139
-                        description={PlanningEventManager.getFormattedTime(item)}
140
-                        left={props => <Avatar.Image
139
+                        description={PlanningEventManager.getFormattedEventTime(item["date_begin"], item["date_end"])}
140
+                        left={() => <Avatar.Image
141 141
                             source={{uri: item.logo}}
142 142
                             style={{backgroundColor: 'transparent'}}
143 143
                         />}
@@ -151,7 +151,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
151 151
                     <Divider/>
152 152
                     <List.Item
153 153
                         title={item.title}
154
-                        description={PlanningEventManager.getFormattedTime(item)}
154
+                        description={PlanningEventManager.getFormattedEventTime(item["date_begin"], item["date_end"])}
155 155
                         onPress={onPress}
156 156
                     />
157 157
                 </View>
@@ -205,8 +205,8 @@ export default class PlanningScreen extends React.Component<Props, State> {
205 205
     generateEventAgenda(eventList: Array<Object>) {
206 206
         let agendaItems = this.generateEmptyCalendar();
207 207
         for (let i = 0; i < eventList.length; i++) {
208
-            if (agendaItems[PlanningEventManager.getEventStartDate(eventList[i])] !== undefined) {
209
-                this.pushEventInOrder(agendaItems, eventList[i], PlanningEventManager.getEventStartDate(eventList[i]));
208
+            if (PlanningEventManager.getDateOnlyString(eventList[i]["date_begin"]) !== undefined) {
209
+                this.pushEventInOrder(agendaItems, eventList[i], PlanningEventManager.getDateOnlyString(eventList[i]["date_begin"]));
210 210
             }
211 211
         }
212 212
         this.setState({agendaItems: agendaItems})
@@ -217,7 +217,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
217 217
             agendaItems[startDate].push(event);
218 218
         else {
219 219
             for (let i = 0; i < agendaItems[startDate].length; i++) {
220
-                if (PlanningEventManager.isEventBefore(event, agendaItems[startDate][i])) {
220
+                if (PlanningEventManager.isEventBefore(event["date_begin"], agendaItems[startDate][i]["date_begin"])) {
221 221
                     agendaItems[startDate].splice(i, 0, event);
222 222
                     break;
223 223
                 } else if (i === agendaItems[startDate].length - 1) {
@@ -228,7 +228,7 @@ export default class PlanningScreen extends React.Component<Props, State> {
228 228
         }
229 229
     }
230 230
 
231
-    onAgendaRef(ref: Agenda) {
231
+    onAgendaRef(ref: Object) {
232 232
         this.agendaRef = ref;
233 233
     }
234 234
 

+ 2
- 33
screens/SelfMenuScreen.js View File

@@ -2,7 +2,7 @@
2 2
 
3 3
 import * as React from 'react';
4 4
 import {View} from 'react-native';
5
-import i18n from "i18n-js";
5
+import DateManager from "../utils/DateManager";
6 6
 import WebSectionList from "../components/WebSectionList";
7 7
 import {Card, Text, withTheme} from 'react-native-paper';
8 8
 import AprilFoolsManager from "../utils/AprilFoolsManager";
@@ -19,10 +19,6 @@ type Props = {
19 19
  */
20 20
 class SelfMenuScreen extends React.Component<Props> {
21 21
 
22
-    // Hard code strings as toLocaleDateString does not work on current android JS engine
23
-    daysOfWeek = [];
24
-    monthsOfYear = [];
25
-
26 22
     getRenderItem: Function;
27 23
     getRenderSectionHeader: Function;
28 24
     createDataset: Function;
@@ -30,26 +26,6 @@ class SelfMenuScreen extends React.Component<Props> {
30 26
 
31 27
     constructor(props) {
32 28
         super(props);
33
-        this.daysOfWeek.push(i18n.t("date.daysOfWeek.monday"));
34
-        this.daysOfWeek.push(i18n.t("date.daysOfWeek.tuesday"));
35
-        this.daysOfWeek.push(i18n.t("date.daysOfWeek.wednesday"));
36
-        this.daysOfWeek.push(i18n.t("date.daysOfWeek.thursday"));
37
-        this.daysOfWeek.push(i18n.t("date.daysOfWeek.friday"));
38
-        this.daysOfWeek.push(i18n.t("date.daysOfWeek.saturday"));
39
-        this.daysOfWeek.push(i18n.t("date.daysOfWeek.sunday"));
40
-
41
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.january"));
42
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.february"));
43
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.march"));
44
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.april"));
45
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.may"));
46
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.june"));
47
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.july"));
48
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.august"));
49
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.september"));
50
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.october"));
51
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.november"));
52
-        this.monthsOfYear.push(i18n.t("date.monthsOfYear.december"));
53 29
 
54 30
         this.getRenderItem = this.getRenderItem.bind(this);
55 31
         this.getRenderSectionHeader = this.getRenderSectionHeader.bind(this);
@@ -80,7 +56,7 @@ class SelfMenuScreen extends React.Component<Props> {
80 56
         for (let i = 0; i < fetchedData.length; i++) {
81 57
             result.push(
82 58
                 {
83
-                    title: this.getFormattedDate(fetchedData[i].date),
59
+                    title: DateManager.getInstance().getTranslatedDate(fetchedData[i].date),
84 60
                     data: fetchedData[i].meal[0].foodcategory,
85 61
                     extraData: super.state,
86 62
                     keyExtractor: this.getKeyExtractor,
@@ -90,13 +66,6 @@ class SelfMenuScreen extends React.Component<Props> {
90 66
         return result
91 67
     }
92 68
 
93
-    getFormattedDate(dateString: string) {
94
-        let dateArray = dateString.split('-');
95
-        let date = new Date();
96
-        date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
97
-        return this.daysOfWeek[date.getDay() - 1] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
98
-    }
99
-
100 69
     getRenderSectionHeader({section}: Object) {
101 70
         return (
102 71
             <Card style={{

+ 50
- 0
utils/DateManager.js View File

@@ -0,0 +1,50 @@
1
+import i18n from 'i18n-js';
2
+
3
+export default class DateManager {
4
+    static instance: DateManager | null = null;
5
+
6
+    // Hard code strings as toLocaleDateString does not work on current android JS engine
7
+    daysOfWeek = [];
8
+    monthsOfYear = [];
9
+
10
+    constructor() {
11
+        this.daysOfWeek.push(i18n.t("date.daysOfWeek.monday"));
12
+        this.daysOfWeek.push(i18n.t("date.daysOfWeek.tuesday"));
13
+        this.daysOfWeek.push(i18n.t("date.daysOfWeek.wednesday"));
14
+        this.daysOfWeek.push(i18n.t("date.daysOfWeek.thursday"));
15
+        this.daysOfWeek.push(i18n.t("date.daysOfWeek.friday"));
16
+        this.daysOfWeek.push(i18n.t("date.daysOfWeek.saturday"));
17
+        this.daysOfWeek.push(i18n.t("date.daysOfWeek.sunday"));
18
+
19
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.january"));
20
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.february"));
21
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.march"));
22
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.april"));
23
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.may"));
24
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.june"));
25
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.july"));
26
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.august"));
27
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.september"));
28
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.october"));
29
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.november"));
30
+        this.monthsOfYear.push(i18n.t("date.monthsOfYear.december"));
31
+    }
32
+
33
+    /**
34
+     * Get this class instance or create one if none is found
35
+     * @returns {DateManager}
36
+     */
37
+    static getInstance(): DateManager {
38
+        return DateManager.instance === null ?
39
+            DateManager.instance = new DateManager() :
40
+            DateManager.instance;
41
+    }
42
+
43
+    getTranslatedDate(dateString: string) {
44
+        let dateArray = dateString.split('-');
45
+        let date = new Date();
46
+        date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
47
+        return this.daysOfWeek[date.getDay() - 1] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
48
+    }
49
+
50
+}

+ 25
- 35
utils/PlanningEventManager.js View File

@@ -3,44 +3,34 @@ export default class PlanningEventManager {
3 3
     // Regex used to check date string validity
4 4
     static dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
5 5
 
6
-    static isEventBefore(event1: Object, event2: Object) {
7
-        let date1 = new Date();
8
-        let date2 = new Date();
9
-        let timeArray = PlanningEventManager.getEventStartTime(event1).split(":");
10
-        date1.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
11
-        timeArray = PlanningEventManager.getEventStartTime(event2).split(":");
12
-        date2.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
13
-        return date1 < date2;
14
-    }
15
-
16
-    static getEventStartDate(event: Object) {
17
-        return event.date_begin.split(" ")[0];
18
-    }
19
-
20
-    static getEventStartTime(event: Object) {
21
-        if (event !== undefined && Object.keys(event).length > 0 && event.date_begin !== null)
22
-            return PlanningEventManager.formatTime(event.date_begin.split(" ")[1]);
23
-        else
24
-            return "";
25
-    }
26
-
27
-    static getEventEndTime(event: Object) {
28
-        if (event !== undefined && Object.keys(event).length > 0 && event.date_end !== null)
29
-            return PlanningEventManager.formatTime(event.date_end.split(" ")[1]);
6
+    /**
7
+     * Checks if the given date is before the other.
8
+     *
9
+     * @param event1Date Event 1 date in format YYYY-MM-DD HH:MM:SS
10
+     * @param event2Date Event 2 date in format YYYY-MM-DD HH:MM:SS
11
+     * @return {boolean}
12
+     */
13
+    static isEventBefore(event1Date: ?string, event2Date: ?string) {
14
+        let date1 = PlanningEventManager.stringToDate(event1Date);
15
+        let date2 = PlanningEventManager.stringToDate(event2Date);
16
+        if (date1 !== undefined && date2 !== undefined)
17
+            return date1 < date2;
30 18
         else
31
-            return "";
19
+            return false;
32 20
     }
33 21
 
34
-    static getFormattedTime(event: Object) {
35
-        if (PlanningEventManager.getEventEndTime(event) !== "")
36
-            return PlanningEventManager.getEventStartTime(event) + " - " + PlanningEventManager.getEventEndTime(event);
22
+    /**
23
+     * Gets only the date part of the given event date string in the format
24
+     * YYYY-MM-DD HH:MM:SS
25
+     *
26
+     * @param dateString The string to get the date from
27
+     * @return {string|undefined} Date in format YYYY:MM:DD or undefined if given string is invalid
28
+     */
29
+    static getDateOnlyString(dateString: ?string) {
30
+        if (PlanningEventManager.isDateStringFormatValid(dateString))
31
+            return dateString.split(" ")[0];
37 32
         else
38
-            return PlanningEventManager.getEventStartTime(event);
39
-    }
40
-
41
-    static formatTime(time: string) {
42
-        let array = time.split(':');
43
-        return array[0] + ':' + array[1];
33
+            return undefined;
44 34
     }
45 35
 
46 36
     /**
@@ -61,7 +51,7 @@ export default class PlanningEventManager {
61 51
      * Accepted format: YYYY-MM-DD HH:MM:SS
62 52
      *
63 53
      * @param dateString The string to convert
64
-     * @return {Date} The date object or undefined if the given string is invalid
54
+     * @return {Date|undefined} The date object or undefined if the given string is invalid
65 55
      */
66 56
     static stringToDate(dateString: ?string): Date | undefined {
67 57
         let date = new Date();

+ 43
- 0
utils/__test__/PlanningEventManager.test.js View File

@@ -4,6 +4,7 @@ import PlanningEventManager from "../PlanningEventManager";
4 4
 test('isDescriptionEmpty', () => {
5 5
     expect(PlanningEventManager.isDescriptionEmpty("")).toBeTrue();
6 6
     expect(PlanningEventManager.isDescriptionEmpty("   ")).toBeTrue();
7
+    // noinspection CheckTagEmptyBody
7 8
     expect(PlanningEventManager.isDescriptionEmpty("<p></p>")).toBeTrue();
8 9
     expect(PlanningEventManager.isDescriptionEmpty("<p>   </p>")).toBeTrue();
9 10
     expect(PlanningEventManager.isDescriptionEmpty("<p><br></p>")).toBeTrue();
@@ -81,3 +82,45 @@ test('getFormattedEventTime', () => {
81 82
         .toBe('20:30 - 23:00');
82 83
 });
83 84
 
85
+test('getDateOnlyString', () => {
86
+    expect(PlanningEventManager.getDateOnlyString("2020-03-21 09:00:00")).toBe("2020-03-21");
87
+    expect(PlanningEventManager.getDateOnlyString("2021-12-15 09:00:00")).toBe("2021-12-15");
88
+    expect(PlanningEventManager.getDateOnlyString("2021-12-o5 09:00:00")).toBeUndefined();
89
+    expect(PlanningEventManager.getDateOnlyString("2021-12-15 09:")).toBeUndefined();
90
+    expect(PlanningEventManager.getDateOnlyString("2021-12-15")).toBeUndefined();
91
+    expect(PlanningEventManager.getDateOnlyString("garbage")).toBeUndefined();
92
+});
93
+
94
+test('isEventBefore', () => {
95
+    expect(PlanningEventManager.isEventBefore(
96
+        "2020-03-21 09:00:00", "2020-03-21 10:00:00")).toBeTrue();
97
+    expect(PlanningEventManager.isEventBefore(
98
+        "2020-03-21 10:00:00", "2020-03-21 10:15:00")).toBeTrue();
99
+    expect(PlanningEventManager.isEventBefore(
100
+        "2020-03-21 10:15:05", "2020-03-21 10:15:54")).toBeTrue();
101
+    expect(PlanningEventManager.isEventBefore(
102
+        "2020-03-21 10:15:05", "2021-03-21 10:15:05")).toBeTrue();
103
+    expect(PlanningEventManager.isEventBefore(
104
+        "2020-03-21 10:15:05", "2020-05-21 10:15:05")).toBeTrue();
105
+    expect(PlanningEventManager.isEventBefore(
106
+        "2020-03-21 10:15:05", "2020-03-30 10:15:05")).toBeTrue();
107
+
108
+    expect(PlanningEventManager.isEventBefore(
109
+        "2020-03-21 10:00:00", "2020-03-21 09:00:00")).toBeFalse();
110
+    expect(PlanningEventManager.isEventBefore(
111
+        "2020-03-21 10:15:00", "2020-03-21 10:00:00")).toBeFalse();
112
+    expect(PlanningEventManager.isEventBefore(
113
+        "2020-03-21 10:15:54", "2020-03-21 10:15:05")).toBeFalse();
114
+    expect(PlanningEventManager.isEventBefore(
115
+        "2021-03-21 10:15:05", "2020-03-21 10:15:05")).toBeFalse();
116
+    expect(PlanningEventManager.isEventBefore(
117
+        "2020-05-21 10:15:05", "2020-03-21 10:15:05")).toBeFalse();
118
+    expect(PlanningEventManager.isEventBefore(
119
+        "2020-03-30 10:15:05", "2020-03-21 10:15:05")).toBeFalse();
120
+
121
+    expect(PlanningEventManager.isEventBefore(
122
+        "garbage", "2020-03-21 10:15:05")).toBeFalse();
123
+    expect(PlanningEventManager.isEventBefore(
124
+        undefined, undefined)).toBeFalse();
125
+});
126
+

Loading…
Cancel
Save