Skip to content

Commit

Permalink
front: add unit tests for stdcm simulation report sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Akctarus committed Jul 8, 2024
1 parent 98550cb commit 8679e92
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { formatDay } from 'utils/date';
import { getStopTime } from 'utils/timeManipulation';

import styles from './SimulationReportStyleSheet';
import { extractSpeedLimit, getStopDurationTime, formatCreationDate } from '../utils';
import {
extractSpeedLimit,
getStopDurationTime,
formatCreationDate,
} from '../utils/simulationReportSheet';

type SimulationReportSheetProps = {
stdcmData: PostStdcmApiResponse;
Expand Down
562 changes: 284 additions & 278 deletions front/src/applications/stdcm/components/SimulationReportSheetV2.tsx

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion front/src/applications/stdcm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type StdcmV2SuccessResponse = Omit<
export type SimulationReportSheetProps = {
stdcmData: StdcmV2SuccessResponse;
pathProperties?: ManageTrainSchedulePathProperties;
rollingStockData: RollingStockWithLiveries;
rollingStockData?: RollingStockWithLiveries;
speedLimitByTag?: string;
simulationReportSheetNumber: string;
mapCanvas?: string;
Expand Down
45 changes: 45 additions & 0 deletions front/src/applications/stdcm/utils/__tests__/sampleData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const simulationReportTest = {
pathProperties: {
suggestedOperationalPoints: [
{
opId: 'op1',
name: 'Point 1',
ch: 'Ch1',
stopFor: 'Stop1',
metadata: { trackName: 'Track 1' },
positionOnPath: 100,
offsetOnTrack: 0,
track: 'Track 1',
coordinates: { lat: 0, lon: 0 },
},
],
},
stdcmData: {
status: 'success',
simulation: {
status: 'success',
base: {
positions: [50, 100, 150],
energy_consumption: 10200307394.39714,
scheduled_points_honored: true,
speeds: [10, 20, 30],
times: [0, 60000, 120000],
},
final_output: {
positions: [50, 100, 150],
energy_consumption: 10200307394.39714,
scheduled_points_honored: true,
speeds: [10, 20, 30],
times: [0, 60000, 120000],
routing_requirements: [],
signal_sightings: [],
spacing_requirements: [],
zone_updates: [],
},
},
departure_time: '2024-07-08T10:00:00Z',
},
simulationReportSheetNumber: '0724-123-456',
};

export default simulationReportTest;
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { SimulationReportSheetProps } from 'applications/stdcm/types';
import {
generateCodeNumber,
formatCreationDate,
getStopDurationTime,
extractSpeedLimit,
getOperationalPointsWithTimes,
} from 'applications/stdcm/utils/simulationReportSheet';

import simulationReportTest from './sampleData';

describe('Utils simulation report sheet functions', () => {
test('should return a formatted string', () => {
const codeNumber = generateCodeNumber();
expect(codeNumber).toMatch(/^\d{2}\d{2}-\d{3}-\d{3}$/);
});

test('should format the date correctly', () => {
const date = '2024-07-08T10:20:30Z';
const formattedDate = formatCreationDate(date);
expect(formattedDate).toEqual({
day: '08',
month: '07',
year: 2024,
hours: 12,
minutes: 20,
});
});

test('should return correct time format', () => {
expect(getStopDurationTime(30)).toBe('30 sec');
expect(getStopDurationTime(90)).toBe('1 min');
});

test('should extract the speed limit correctly', () => {
const speedLimitByTag = 'Some tag - 50';
expect(extractSpeedLimit(speedLimitByTag)).toBe('50');
});

test('should return formatted operational points with times', () => {
const result = getOperationalPointsWithTimes(
simulationReportTest as unknown as SimulationReportSheetProps
);
expect(result).toEqual([
{
opId: 'op1',
positionOnPath: 100,
time: '12:01',
name: 'Point 1',
ch: 'Ch1',
stop: 'Stop1',
duration: 0,
departureTime: '12:00',
stopEndTime: '12:01',
trackName: 'Track 1',
},
]);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SimulationReportSheetProps } from './types';
import type { SimulationReportSheetProps } from '../types';

function generateRandomString(length: number): string {
return Array.from({ length }, () => Math.floor(Math.random() * 10)).join('');
Expand Down
2 changes: 1 addition & 1 deletion front/src/applications/stdcm/views/StdcmResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import SpeedSpaceChart from 'modules/simulationResult/components/SpeedSpaceChart
import type { AllowancesSettings } from 'reducers/osrdsimulation/types';

import SimulationReportSheet from '../components/SimulationReportSheet';
import { generateCodeNumber } from '../utils';
import { generateCodeNumber } from '../utils/simulationReportSheet';

type StcdmResultsProps = {
mapCanvas?: string;
Expand Down
2 changes: 1 addition & 1 deletion front/src/applications/stdcm/views/StdcmResultsV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { TimeScaleDomain } from 'modules/simulationResult/types';
import SimulationReportSheetV2 from '../components/SimulationReportSheetV2';
import { STDCM_TRAIN_ID } from '../consts';
import type { StdcmV2Results } from '../types';
import { generateCodeNumber } from '../utils';
import { generateCodeNumber } from '../utils/simulationReportSheet';

type StcdmResultsProps = {
mapCanvas?: string;
Expand Down

0 comments on commit 8679e92

Please sign in to comment.