Skip to content

Commit

Permalink
front: write unit tests for transformRowDataOnChange
Browse files Browse the repository at this point in the history
No change in behavior, just testing the previous behavior of the simulation input table, when a value changes.
  • Loading branch information
anisometropie committed Sep 16, 2024
1 parent 3fa263f commit e8b9591
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 24 deletions.
28 changes: 4 additions & 24 deletions front/src/modules/timesStops/TimesStops.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useAppDispatch } from 'store';
import { time2sec } from 'utils/timeManipulation';

import { marginRegExValidation } from './consts';
import { formatSuggestedViasToRowVias } from './helpers/utils';
import { formatSuggestedViasToRowVias, transformRowDataOnChange } from './helpers/utils';
import { useTimeStopsColumns } from './hooks/useTimeStopsColumns';
import { TableType } from './types';
import type { PathWaypointRow } from './types';
Expand Down Expand Up @@ -78,31 +78,11 @@ const TimesStops = ({
if (!isInputTable) {
return;
}
const rowData = { ...row[op.fromRowIndex] };
const previousRowData = rows[op.fromRowIndex];
if (
rowData.departure &&
rowData.arrival &&
(rowData.arrival !== previousRowData.arrival ||
rowData.departure !== previousRowData.departure)
) {
rowData.stopFor = String(time2sec(rowData.departure) - time2sec(rowData.arrival));
}
if (!rowData.stopFor && op.fromRowIndex !== allWaypoints.length - 1) {
rowData.onStopSignal = false;
}
if (rowData.theoreticalMargin && !marginRegExValidation.test(rowData.theoreticalMargin)) {
rowData.isMarginValid = false;
const newRowData = transformRowDataOnChange(row[op.fromRowIndex], rows[op.fromRowIndex], op, allWaypoints.length);
if (!newRowData.isMarginValid) {
setRows(row);
} else {
rowData.isMarginValid = true;
if (op.fromRowIndex === 0) {
rowData.arrival = null;
// As we put 0% by default for origin's margin, if the user removes a margin without
// replacing it to 0% (undefined), we change it to 0%
if (!rowData.theoreticalMargin) rowData.theoreticalMargin = '0%';
}
dispatch(upsertViaFromSuggestedOP(rowData as SuggestedOP));
dispatch(upsertViaFromSuggestedOP(newRowData as SuggestedOP));
}
}}
stickyRightColumn={stickyRightColumn}
Expand Down
75 changes: 75 additions & 0 deletions front/src/modules/timesStops/helpers/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type { PathWaypointRow } from 'modules/timesStops/types';
import { transformRowDataOnChange } from '../utils';


describe('transformRowDataOnChange', () => {
const whateverOperation = { fromRowIndex: 2 };

describe('arrival is set, departure just changed', () => {
it('should update stop duration from the arrival and departure', () => {
const rowData = {
opId: 'd94a2af4-6667-11e3-89ff-01f464e0362d',
name: 'Gr',
arrival: '23:40:00',
departure: '23:50:00',
stopFor: '300', // no longer correct, not yet updated by the function
} as PathWaypointRow;
const previousRowData = {
opId: 'd94a2af4-6667-11e3-89ff-01f464e0362d',
name: 'Gr',
arrival: '23:40:00',
departure: '23:45:00',
stopFor: '300',
} as PathWaypointRow;
const result = transformRowDataOnChange(rowData, previousRowData, whateverOperation, 4);
expect(result).toEqual({
opId: 'd94a2af4-6667-11e3-89ff-01f464e0362d',
name: 'Gr',
arrival: '23:40:00',
departure: '23:50:00',
stopFor: '600', // now correct with the new arrival and departure
isMarginValid: true,
});
});
});
describe('theoritical margin is incorrect', () => {
it('should set isMarginValid flag to false', () => {
const rowData = {
opId: 'd94a2af4-6667-11e3-89ff-01f464e0362d',
name: 'Gr',
theoreticalMargin: '10',
} as PathWaypointRow;
const previousRowData = {
opId: 'd94a2af4-6667-11e3-89ff-01f464e0362d',
name: 'Gr',
} as PathWaypointRow;
const result = transformRowDataOnChange(rowData, previousRowData, whateverOperation, 4);
expect(result.isMarginValid).toBe(false);
});
});
describe('user removed first row theoritical margin', () => {
it('should set the theoritical margin back to 0%', () => {
const rowData = {
opId: 'd94a2af4-6667-11e3-89ff-01f464e0362d',
name: 'Gr',
} as PathWaypointRow;
const previousRowData = {
opId: 'd94a2af4-6667-11e3-89ff-01f464e0362d',
name: 'Gr',
theoreticalMargin: '10%',
} as PathWaypointRow;
const operation = {
fromRowIndex: 0,
};
const result = transformRowDataOnChange(rowData, previousRowData, operation, 4);
expect(result).toEqual({
opId: 'd94a2af4-6667-11e3-89ff-01f464e0362d',
name: 'Gr',
arrival: null,
isMarginValid: true,
onStopSignal: false,
theoreticalMargin: '0%',
});
});
});
});
30 changes: 30 additions & 0 deletions front/src/modules/timesStops/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,33 @@ export function disabledTextColumn(
disabled: true,
};
}

export function transformRowDataOnChange(
rowData: PathWaypointRow,
previousRowData: PathWaypointRow,
op: { fromRowIndex: number },
allWaypointsLength: number,
) {
const newRowData = { ...rowData };
if (
newRowData.departure &&
newRowData.arrival &&
(newRowData.arrival !== previousRowData.arrival ||
newRowData.departure !== previousRowData.departure)
) {
newRowData.stopFor = String(time2sec(newRowData.departure) - time2sec(newRowData.arrival));
}
if (!newRowData.stopFor && op.fromRowIndex !== allWaypointsLength - 1) {
newRowData.onStopSignal = false;
}
newRowData.isMarginValid = !(newRowData.theoreticalMargin && !marginRegExValidation.test(newRowData.theoreticalMargin));
if (newRowData.isMarginValid && op.fromRowIndex === 0) {
newRowData.arrival = null;
// As we put 0% by default for origin's margin, if the user removes a margin without
// replacing it to 0% (undefined), we change it to 0%
if (!newRowData.theoreticalMargin) {
newRowData.theoreticalMargin = '0%';
}
}
return newRowData;
}

0 comments on commit e8b9591

Please sign in to comment.