diff --git a/__tests__/utils/Proxiwash.test.js b/__tests__/utils/Proxiwash.test.js new file mode 100644 index 0000000..26f8329 --- /dev/null +++ b/__tests__/utils/Proxiwash.test.js @@ -0,0 +1,134 @@ +import React from 'react'; +import {getCleanedMachineWatched, getMachineEndDate, getMachineOfId, isMachineWatched} from "../../src/utils/Proxiwash"; + +test('getMachineEndDate', () => { + jest.spyOn(Date, 'now') + .mockImplementation(() => + new Date('2020-01-14T00:00:00.000Z').getTime() + ); + let expectDate = new Date('2020-01-14T00:00:00.000Z'); + expectDate.setHours(23); + expectDate.setMinutes(10); + expect(getMachineEndDate({endTime: "23:10"}).getTime()).toBe(expectDate.getTime()); + + expectDate.setHours(15); + expectDate.setMinutes(30); + expect(getMachineEndDate({endTime: "15:30"}).getTime()).toBe(expectDate.getTime()); + + expectDate.setHours(0); + expectDate.setMinutes(10); + expectDate.setDate(expectDate.getDate() + 1); + expect(getMachineEndDate({endTime: "00:10"}).getTime()).toBe(expectDate.getTime()); +}); + +test('isMachineWatched', () => { + let machineList = [ + { + number: "0", + endTime: "23:30", + }, + { + number: "1", + endTime: "20:30", + }, + ]; + expect(isMachineWatched({number: "0", endTime: "23:30"}, machineList)).toBeTrue(); + expect(isMachineWatched({number: "1", endTime: "20:30"}, machineList)).toBeTrue(); + expect(isMachineWatched({number: "3", endTime: "20:30"}, machineList)).toBeFalse(); + expect(isMachineWatched({number: "1", endTime: "23:30"}, machineList)).toBeFalse(); +}); + +test('getMachineOfId', () => { + let machineList = [ + { + number: "0", + }, + { + number: "1", + }, + ]; + expect(getMachineOfId("0", machineList)).toStrictEqual({number: "0"}); + expect(getMachineOfId("1", machineList)).toStrictEqual({number: "1"}); + expect(getMachineOfId("3", machineList)).toBeNull(); +}); + +test('getCleanedMachineWatched', () => { + let machineList = [ + { + number: "0", + endTime: "23:30", + }, + { + number: "1", + endTime: "20:30", + }, + { + number: "2", + endTime: "", + }, + ]; + let watchList = [ + { + number: "0", + endTime: "23:30", + }, + { + number: "1", + endTime: "20:30", + }, + { + number: "2", + endTime: "", + }, + ]; + let cleanedList = watchList; + expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList); + + watchList = [ + { + number: "0", + endTime: "23:30", + }, + { + number: "1", + endTime: "20:30", + }, + { + number: "2", + endTime: "15:30", + }, + ]; + cleanedList = [ + { + number: "0", + endTime: "23:30", + }, + { + number: "1", + endTime: "20:30", + }, + ]; + expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList); + + watchList = [ + { + number: "0", + endTime: "23:30", + }, + { + number: "1", + endTime: "20:31", + }, + { + number: "3", + endTime: "15:30", + }, + ]; + cleanedList = [ + { + number: "0", + endTime: "23:30", + }, + ]; + expect(getCleanedMachineWatched(watchList, machineList)).toStrictEqual(cleanedList); +}); \ No newline at end of file diff --git a/package.json b/package.json index f66c041..475eacc 100644 --- a/package.json +++ b/package.json @@ -63,9 +63,10 @@ "@react-native-community/eslint-config": "^0.0.5", "babel-jest": "^24.9.0", "eslint": "^6.5.1", + "flow-bin": "^0.122.0", "jest": "^24.9.0", + "jest-extended": "^0.11.5", "metro-react-native-babel-preset": "^0.58.0", - "react-test-renderer": "16.9.0", - "flow-bin": "^0.122.0" + "react-test-renderer": "16.9.0" } } diff --git a/src/utils/Proxiwash.js b/src/utils/Proxiwash.js index d906294..0814e25 100644 --- a/src/utils/Proxiwash.js +++ b/src/utils/Proxiwash.js @@ -1,13 +1,19 @@ // @flow import type {Machine} from "../screens/Proxiwash/ProxiwashScreen"; -import ProxiwashConstants from "../constants/ProxiwashConstants"; +/** + * Gets the machine end Date object. + * If the time is before the current time, it will be considered as tomorrow + * + * @param machine The machine to get the date from + * @returns {Date} The date object representing the end time. + */ export function getMachineEndDate(machine: Machine) { const array = machine.endTime.split(":"); - let date = new Date(); + let date = new Date(Date.now()); date.setHours(parseInt(array[0]), parseInt(array[1])); - if (date < new Date()) + if (date < new Date(Date.now())) date.setDate(date.getDate() + 1); return date; } @@ -15,8 +21,8 @@ export function getMachineEndDate(machine: Machine) { /** * Checks whether the machine of the given ID has scheduled notifications * - * @param machine - * @param machineList + * @param machine The machine to check + * @param machineList The machine list * @returns {boolean} */ export function isMachineWatched(machine: Machine, machineList: Array) { @@ -30,7 +36,14 @@ export function isMachineWatched(machine: Machine, machineList: Array) return watched; } -function getMachineOfId(id: string, allMachines: Array) { +/** + * Gets the machine of the given id + * + * @param id The machine's ID + * @param allMachines The machine list + * @returns {null|Machine} The machine or null if not found + */ +export function getMachineOfId(id: string, allMachines: Array) { for (let i = 0; i < allMachines.length; i++) { if (allMachines[i].number === id) return allMachines[i]; @@ -38,13 +51,22 @@ function getMachineOfId(id: string, allMachines: Array) { return null; } -export function getCleanedMachineWatched(machineList: Array, allMachines: Array) { +/** + * Gets a cleaned machine watched list by removing invalid entries. + * An entry is considered invalid if the end time in the watched list + * and in the full list does not match (a new machine cycle started) + * + * @param machineWatchedList The current machine watch list + * @param allMachines The current full machine list + * @returns {Array} + */ +export function getCleanedMachineWatched(machineWatchedList: Array, allMachines: Array) { let newList = []; - for (let i = 0; i < machineList.length; i++) { - let machine = getMachineOfId(machineList[i].number, allMachines); + for (let i = 0; i < machineWatchedList.length; i++) { + let machine = getMachineOfId(machineWatchedList[i].number, allMachines); if (machine !== null - && machineList[i].number === machine.number && machineList[i].endTime === machine.endTime - && ProxiwashConstants.machineStates[machineList[i].state] === ProxiwashConstants.machineStates["EN COURS"]) { + && machineWatchedList[i].number === machine.number + && machineWatchedList[i].endTime === machine.endTime) { newList.push(machine); } }