Skip to content

Commit 18b29a6

Browse files
committed
front: throw error if no track range found in findTrackSectionOffset utils
Signed-off-by: Clara Ni <clara.ni@outlook.fr>
1 parent d9dbf7c commit 18b29a6

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

front/src/modules/pathfinding/helpers/__tests__/findTrackSectionOffset.spec.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('findTrackSectionOffset', () => {
6565
expect(result).toEqual({ track: 'track_2', offset: 1050 });
6666
});
6767

68-
it('should not find the track offset if past cumulative sums', () => {
68+
it('should throw an error if the given position on path is beyond the last position of the path', () => {
6969
const trackRangesLengthCumulativeSums = [1000, 2000, 3000];
7070
const trackRanges = [
7171
{ track_section: 'track_0' },
@@ -74,13 +74,10 @@ describe('findTrackSectionOffset', () => {
7474
] as TrackRange[];
7575

7676
const offsetOnPath = 3001;
77-
const result = findTrackSectionOffset(
78-
offsetOnPath,
79-
trackRangesLengthCumulativeSums,
80-
trackRanges
81-
);
8277

83-
expect(result).toEqual(null);
78+
expect(() =>
79+
findTrackSectionOffset(offsetOnPath, trackRangesLengthCumulativeSums, trackRanges)
80+
).toThrow('No track range found for the given position on path');
8481
});
8582

8683
it('should correctly find the track offset if it is located on the first track range', () => {

front/src/modules/pathfinding/helpers/findTrackSectionOffset.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ const findTrackSectionOffset = (
1313
(cumulativeSum) => positionOnPath <= cumulativeSum
1414
);
1515
const trackRange = trackRangesOnPath[index];
16-
if (!trackRange) return null;
16+
17+
if (!trackRange) {
18+
throw new Error('No track range found for the given position on path');
19+
}
1720

1821
// compute offset
1922
const inferiorSum = index > 0 ? tracksLengthCumulativeSums[index - 1] : 0;

front/src/modules/pathfinding/helpers/getPointOnPathCoordinates.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const getPointOnPathCoordinates = (
2222
trackRanges
2323
);
2424

25-
const track = tracks[trackOffset!.track];
25+
const track = tracks[trackOffset.track];
2626

27-
return getPointOnTrackCoordinates(track.geo, mToMm(track.length), trackOffset!.offset);
27+
return getPointOnTrackCoordinates(track.geo, mToMm(track.length), trackOffset.offset);
2828
};
2929

3030
export default getPointOnPathCoordinates;

front/src/modules/powerRestriction/helpers/createPathStep.ts

-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const createPathStep = (
2929
tracksLengthCumulativeSums,
3030
pathProperties.trackSectionRanges
3131
);
32-
if (!trackOffset) return undefined;
3332

3433
const coordinates = getPointOnPathCoordinates(
3534
tracksById,
@@ -85,8 +84,6 @@ export const createCutAtPathStep = (
8584
pathProperties.trackSectionRanges
8685
);
8786

88-
if (!trackOffset) return null;
89-
9087
const coordinatesAtCut = getPointOnPathCoordinates(
9188
tracksById,
9289
pathProperties.trackSectionRanges,

0 commit comments

Comments
 (0)