Added tests for proxiwash util functions

This commit is contained in:
Arnaud Vergnet 2020-04-30 22:16:35 +02:00
parent 7fb4de3c5b
commit 1835fcadf9
3 changed files with 170 additions and 13 deletions

View file

@ -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);
});

View file

@ -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"
}
}

View file

@ -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<Machine>) {
@ -30,7 +36,14 @@ export function isMachineWatched(machine: Machine, machineList: Array<Machine>)
return watched;
}
function getMachineOfId(id: string, allMachines: Array<Machine>) {
/**
* 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<Machine>) {
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<Machine>) {
return null;
}
export function getCleanedMachineWatched(machineList: Array<Machine>, allMachines: Array<Machine>) {
/**
* 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<Machine>}
*/
export function getCleanedMachineWatched(machineWatchedList: Array<Machine>, allMachines: Array<Machine>) {
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);
}
}