forked from vergnet/application-amicale
Added tests for proxiwash util functions
This commit is contained in:
parent
7fb4de3c5b
commit
1835fcadf9
3 changed files with 170 additions and 13 deletions
134
__tests__/utils/Proxiwash.test.js
Normal file
134
__tests__/utils/Proxiwash.test.js
Normal 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);
|
||||||
|
});
|
|
@ -63,9 +63,10 @@
|
||||||
"@react-native-community/eslint-config": "^0.0.5",
|
"@react-native-community/eslint-config": "^0.0.5",
|
||||||
"babel-jest": "^24.9.0",
|
"babel-jest": "^24.9.0",
|
||||||
"eslint": "^6.5.1",
|
"eslint": "^6.5.1",
|
||||||
|
"flow-bin": "^0.122.0",
|
||||||
"jest": "^24.9.0",
|
"jest": "^24.9.0",
|
||||||
|
"jest-extended": "^0.11.5",
|
||||||
"metro-react-native-babel-preset": "^0.58.0",
|
"metro-react-native-babel-preset": "^0.58.0",
|
||||||
"react-test-renderer": "16.9.0",
|
"react-test-renderer": "16.9.0"
|
||||||
"flow-bin": "^0.122.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,19 @@
|
||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
import type {Machine} from "../screens/Proxiwash/ProxiwashScreen";
|
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) {
|
export function getMachineEndDate(machine: Machine) {
|
||||||
const array = machine.endTime.split(":");
|
const array = machine.endTime.split(":");
|
||||||
let date = new Date();
|
let date = new Date(Date.now());
|
||||||
date.setHours(parseInt(array[0]), parseInt(array[1]));
|
date.setHours(parseInt(array[0]), parseInt(array[1]));
|
||||||
if (date < new Date())
|
if (date < new Date(Date.now()))
|
||||||
date.setDate(date.getDate() + 1);
|
date.setDate(date.getDate() + 1);
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
@ -15,8 +21,8 @@ export function getMachineEndDate(machine: Machine) {
|
||||||
/**
|
/**
|
||||||
* Checks whether the machine of the given ID has scheduled notifications
|
* Checks whether the machine of the given ID has scheduled notifications
|
||||||
*
|
*
|
||||||
* @param machine
|
* @param machine The machine to check
|
||||||
* @param machineList
|
* @param machineList The machine list
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
export function isMachineWatched(machine: Machine, machineList: Array<Machine>) {
|
export function isMachineWatched(machine: Machine, machineList: Array<Machine>) {
|
||||||
|
@ -30,7 +36,14 @@ export function isMachineWatched(machine: Machine, machineList: Array<Machine>)
|
||||||
return watched;
|
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++) {
|
for (let i = 0; i < allMachines.length; i++) {
|
||||||
if (allMachines[i].number === id)
|
if (allMachines[i].number === id)
|
||||||
return allMachines[i];
|
return allMachines[i];
|
||||||
|
@ -38,13 +51,22 @@ function getMachineOfId(id: string, allMachines: Array<Machine>) {
|
||||||
return null;
|
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 = [];
|
let newList = [];
|
||||||
for (let i = 0; i < machineList.length; i++) {
|
for (let i = 0; i < machineWatchedList.length; i++) {
|
||||||
let machine = getMachineOfId(machineList[i].number, allMachines);
|
let machine = getMachineOfId(machineWatchedList[i].number, allMachines);
|
||||||
if (machine !== null
|
if (machine !== null
|
||||||
&& machineList[i].number === machine.number && machineList[i].endTime === machine.endTime
|
&& machineWatchedList[i].number === machine.number
|
||||||
&& ProxiwashConstants.machineStates[machineList[i].state] === ProxiwashConstants.machineStates["EN COURS"]) {
|
&& machineWatchedList[i].endTime === machine.endTime) {
|
||||||
newList.push(machine);
|
newList.push(machine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue