Browse Source

Added tests for proxiwash util functions

Arnaud Vergnet 1 year ago
parent
commit
1835fcadf9
3 changed files with 170 additions and 13 deletions
  1. 134
    0
      __tests__/utils/Proxiwash.test.js
  2. 3
    2
      package.json
  3. 33
    11
      src/utils/Proxiwash.js

+ 134
- 0
__tests__/utils/Proxiwash.test.js View File

@@ -0,0 +1,134 @@
1
+import React from 'react';
2
+import {getCleanedMachineWatched, getMachineEndDate, getMachineOfId, isMachineWatched} from "../../src/utils/Proxiwash";
3
+
4
+test('getMachineEndDate', () => {
5
+    jest.spyOn(Date, 'now')
6
+        .mockImplementation(() =>
7
+            new Date('2020-01-14T00:00:00.000Z').getTime()
8
+        );
9
+    let expectDate = new Date('2020-01-14T00:00:00.000Z');
10
+    expectDate.setHours(23);
11
+    expectDate.setMinutes(10);
12
+    expect(getMachineEndDate({endTime: "23:10"}).getTime()).toBe(expectDate.getTime());
13
+
14
+    expectDate.setHours(15);
15
+    expectDate.setMinutes(30);
16
+    expect(getMachineEndDate({endTime: "15:30"}).getTime()).toBe(expectDate.getTime());
17
+
18
+    expectDate.setHours(0);
19
+    expectDate.setMinutes(10);
20
+    expectDate.setDate(expectDate.getDate() + 1);
21
+    expect(getMachineEndDate({endTime: "00:10"}).getTime()).toBe(expectDate.getTime());
22
+});
23
+
24
+test('isMachineWatched', () => {
25
+    let machineList = [
26
+        {
27
+            number: "0",
28
+            endTime: "23:30",
29
+        },
30
+        {
31
+            number: "1",
32
+            endTime: "20:30",
33
+        },
34
+    ];
35
+    expect(isMachineWatched({number: "0", endTime: "23:30"}, machineList)).toBeTrue();
36
+    expect(isMachineWatched({number: "1", endTime: "20:30"}, machineList)).toBeTrue();
37
+    expect(isMachineWatched({number: "3", endTime: "20:30"}, machineList)).toBeFalse();
38
+    expect(isMachineWatched({number: "1", endTime: "23:30"}, machineList)).toBeFalse();
39
+});
40
+
41
+test('getMachineOfId', () => {
42
+    let machineList = [
43
+        {
44
+            number: "0",
45
+        },
46
+        {
47
+            number: "1",
48
+        },
49
+    ];
50
+    expect(getMachineOfId("0", machineList)).toStrictEqual({number: "0"});
51
+    expect(getMachineOfId("1", machineList)).toStrictEqual({number: "1"});
52
+    expect(getMachineOfId("3", machineList)).toBeNull();
53
+});
54
+
55
+test('getCleanedMachineWatched', () => {
56
+    let machineList = [
57
+        {
58
+            number: "0",
59
+            endTime: "23:30",
60
+        },
61
+        {
62
+            number: "1",
63
+            endTime: "20:30",
64
+        },
65
+        {
66
+            number: "2",
67
+            endTime: "",
68
+        },
69
+    ];
70
+    let watchList = [
71
+        {
72
+            number: "0",
73
+            endTime: "23:30",
74
+        },
75
+        {
76
+            number: "1",
77
+            endTime: "20:30",
78
+        },
79
+        {
80
+            number: "2",
81
+            endTime: "",
82
+        },
83
+    ];
84
+    let cleanedList = watchList;
85
+    expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList);
86
+
87
+    watchList = [
88
+        {
89
+            number: "0",
90
+            endTime: "23:30",
91
+        },
92
+        {
93
+            number: "1",
94
+            endTime: "20:30",
95
+        },
96
+        {
97
+            number: "2",
98
+            endTime: "15:30",
99
+        },
100
+    ];
101
+    cleanedList = [
102
+        {
103
+            number: "0",
104
+            endTime: "23:30",
105
+        },
106
+        {
107
+            number: "1",
108
+            endTime: "20:30",
109
+        },
110
+    ];
111
+    expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList);
112
+
113
+    watchList = [
114
+        {
115
+            number: "0",
116
+            endTime: "23:30",
117
+        },
118
+        {
119
+            number: "1",
120
+            endTime: "20:31",
121
+        },
122
+        {
123
+            number: "3",
124
+            endTime: "15:30",
125
+        },
126
+    ];
127
+    cleanedList = [
128
+        {
129
+            number: "0",
130
+            endTime: "23:30",
131
+        },
132
+    ];
133
+    expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList);
134
+});

+ 3
- 2
package.json View File

@@ -63,9 +63,10 @@
63 63
     "@react-native-community/eslint-config": "^0.0.5",
64 64
     "babel-jest": "^24.9.0",
65 65
     "eslint": "^6.5.1",
66
+    "flow-bin": "^0.122.0",
66 67
     "jest": "^24.9.0",
68
+    "jest-extended": "^0.11.5",
67 69
     "metro-react-native-babel-preset": "^0.58.0",
68
-    "react-test-renderer": "16.9.0",
69
-    "flow-bin": "^0.122.0"
70
+    "react-test-renderer": "16.9.0"
70 71
   }
71 72
 }

+ 33
- 11
src/utils/Proxiwash.js View File

@@ -1,13 +1,19 @@
1 1
 // @flow
2 2
 
3 3
 import type {Machine} from "../screens/Proxiwash/ProxiwashScreen";
4
-import ProxiwashConstants from "../constants/ProxiwashConstants";
5 4
 
5
+/**
6
+ * Gets the machine end Date object.
7
+ * If the time is before the current time, it will be considered as tomorrow
8
+ *
9
+ * @param machine The machine to get the date from
10
+ * @returns {Date} The date object representing the end time.
11
+ */
6 12
 export function getMachineEndDate(machine: Machine) {
7 13
     const array = machine.endTime.split(":");
8
-    let date = new Date();
14
+    let date = new Date(Date.now());
9 15
     date.setHours(parseInt(array[0]), parseInt(array[1]));
10
-    if (date < new Date())
16
+    if (date < new Date(Date.now()))
11 17
         date.setDate(date.getDate() + 1);
12 18
     return date;
13 19
 }
@@ -15,8 +21,8 @@ export function getMachineEndDate(machine: Machine) {
15 21
 /**
16 22
  * Checks whether the machine of the given ID has scheduled notifications
17 23
  *
18
- * @param machine
19
- * @param machineList
24
+ * @param machine The machine to check
25
+ * @param machineList The machine list
20 26
  * @returns {boolean}
21 27
  */
22 28
 export function isMachineWatched(machine: Machine, machineList: Array<Machine>) {
@@ -30,7 +36,14 @@ export function isMachineWatched(machine: Machine, machineList: Array<Machine>)
30 36
     return watched;
31 37
 }
32 38
 
33
-function getMachineOfId(id: string, allMachines: Array<Machine>) {
39
+/**
40
+ * Gets the machine of the given id
41
+ *
42
+ * @param id The machine's ID
43
+ * @param allMachines The machine list
44
+ * @returns {null|Machine} The machine or null if not found
45
+ */
46
+export function getMachineOfId(id: string, allMachines: Array<Machine>) {
34 47
     for (let i = 0; i < allMachines.length; i++) {
35 48
         if (allMachines[i].number === id)
36 49
             return allMachines[i];
@@ -38,13 +51,22 @@ function getMachineOfId(id: string, allMachines: Array<Machine>) {
38 51
     return null;
39 52
 }
40 53
 
41
-export function getCleanedMachineWatched(machineList: Array<Machine>, allMachines: Array<Machine>) {
54
+/**
55
+ * Gets a cleaned machine watched list by removing invalid entries.
56
+ * An entry is considered invalid if the end time in the watched list
57
+ * and in the full list does not match (a new machine cycle started)
58
+ *
59
+ * @param machineWatchedList The current machine watch list
60
+ * @param allMachines The current full machine list
61
+ * @returns {Array<Machine>}
62
+ */
63
+export function getCleanedMachineWatched(machineWatchedList: Array<Machine>, allMachines: Array<Machine>) {
42 64
     let newList = [];
43
-    for (let i = 0; i < machineList.length; i++) {
44
-        let machine = getMachineOfId(machineList[i].number, allMachines);
65
+    for (let i = 0; i < machineWatchedList.length; i++) {
66
+        let machine = getMachineOfId(machineWatchedList[i].number, allMachines);
45 67
         if (machine !== null
46
-            && machineList[i].number === machine.number && machineList[i].endTime === machine.endTime
47
-            && ProxiwashConstants.machineStates[machineList[i].state] === ProxiwashConstants.machineStates["EN COURS"]) {
68
+            && machineWatchedList[i].number === machine.number
69
+            && machineWatchedList[i].endTime === machine.endTime) {
48 70
             newList.push(machine);
49 71
         }
50 72
     }

Loading…
Cancel
Save