-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathoperationalStudiesConfReducer.spec.ts
163 lines (144 loc) · 5.5 KB
/
operationalStudiesConfReducer.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { describe, it, expect } from 'vitest';
import type { LightRollingStockWithLiveries } from 'common/api/osrdEditoastApi';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { TrainScheduleWithDetails } from 'modules/trainschedule/components/Timetable/types';
import {
operationalStudiesConfSlice,
type OperationalStudiesConfState,
} from 'reducers/osrdconf/operationalStudiesConf';
import { defaultCommonConf } from 'reducers/osrdconf/osrdConfCommon';
import testCommonConfReducers from 'reducers/osrdconf/osrdConfCommon/__tests__/utils';
import { createStoreWithoutMiddleware } from 'store';
import commonConfBuilder from '../osrdConfCommon/__tests__/commonConfBuilder';
import type { PathStep } from '../types';
const createStore = (extraInitialState?: Partial<OperationalStudiesConfState>) =>
createStoreWithoutMiddleware({
[operationalStudiesConfSlice.name]: {
...defaultCommonConf,
...extraInitialState,
},
});
describe('simulationConfReducer', () => {
it('should return initial state', () => {
const store = createStore();
const state = store.getState()[operationalStudiesConfSlice.name];
expect(state).toEqual(defaultCommonConf);
});
it('selectTrainToEdit', () => {
const trainSchedule: TrainScheduleWithDetails = {
id: 1,
trainName: 'train1',
constraint_distribution: 'MARECO',
rollingStock: { id: 1, name: 'rollingStock1' } as LightRollingStockWithLiveries,
path: [
{ id: 'id1', uic: 123 },
{ id: 'id2', uic: 234 },
],
margins: { boundaries: ['id2'], values: ['10%', '0%'] },
startTime: new Date('2021-01-01T00:00:00Z'),
arrivalTime: null,
duration: 1000,
stopsCount: 2,
pathLength: '100',
mechanicalEnergyConsumed: 100,
speedLimitTag: 'MA100',
labels: ['label1'],
isValid: true,
options: { use_electrical_profiles: false },
};
const store = createStore();
store.dispatch(operationalStudiesConfSlice.actions.selectTrainToEdit(trainSchedule));
const state = store.getState()[operationalStudiesConfSlice.name];
expect(state).toEqual({
...defaultCommonConf,
usingElectricalProfiles: false,
labels: ['label1'],
rollingStockID: 1,
speedLimitByTag: 'MA100',
name: 'train1',
pathSteps: [
{
id: 'id1',
uic: 123,
name: '123',
theoreticalMargin: '10%',
ch: undefined,
arrival: undefined,
stopFor: undefined,
locked: undefined,
receptionSignal: undefined,
},
{
id: 'id2',
uic: 234,
name: '234',
theoreticalMargin: undefined,
ch: undefined,
arrival: undefined,
stopFor: undefined,
locked: undefined,
receptionSignal: undefined,
},
],
startTime: new Date('2021-01-01T00:00:00+00:00'),
});
});
describe('should handle upsertViaFromSuggestedOP', () => {
const testDataBuilder = commonConfBuilder();
// For this action, pathfinding has already been made so we know
// all steps will have a positionOnPath
const pathStepsData = testDataBuilder
.buildPathSteps()
.map((step, i) => step && { ...step, positionOnPath: i * 100 });
const [brest, rennes, lemans, paris, strasbourg] = pathStepsData;
it('should insert a new via if it comes from the suggested vias modal', () => {
const pathSteps = [brest, rennes, paris, strasbourg];
const store = createStore({ pathSteps });
const newVia: SuggestedOP = {
opId: 'lemans',
track: '60ca8dda-6667-11e3-81ff-01f464e0362d',
offsetOnTrack: 426.443,
positionOnPath: 200,
uic: 396002,
coordinates: [47.99542250806296, 0.1918181738752042],
};
const insertedVia: PathStep = {
id: 'id3', // the id generated by nextId()
positionOnPath: 200,
uic: 396002,
coordinates: [47.99542250806296, 0.1918181738752042],
};
store.dispatch(operationalStudiesConfSlice.actions.upsertViaFromSuggestedOP(newVia));
const state = store.getState()[operationalStudiesConfSlice.name];
expect(state.pathSteps).toEqual([brest, rennes, insertedVia, paris, strasbourg]);
});
it('should update an existing via if it comes from the "times and step" table and has been added by selecting it on the map', () => {
const pathSteps = [brest, rennes, lemans, paris, strasbourg];
const store = createStore({ pathSteps });
const newVia: SuggestedOP = {
pathStepId: 'lemans',
track: '60ca8dda-6667-11e3-81ff-01f464e0362d',
offsetOnTrack: 426.443,
positionOnPath: 200,
stopFor: 'PT5M',
coordinates: [47.99542250806296, 0.1918181738752042],
};
const updatedVia: PathStep = {
id: 'lemans',
positionOnPath: 200,
track: '60ca8dda-6667-11e3-81ff-01f464e0362d',
offset: 426.443,
stopFor: 'PT5M',
coordinates: [47.99542250806296, 0.1918181738752042],
arrival: newVia.arrival,
locked: newVia.locked,
deleted: newVia.deleted,
name: newVia.name,
};
store.dispatch(operationalStudiesConfSlice.actions.upsertViaFromSuggestedOP(newVia));
const state = store.getState()[operationalStudiesConfSlice.name];
expect(state.pathSteps).toEqual([brest, rennes, updatedVia, paris, strasbourg]);
});
});
testCommonConfReducers(operationalStudiesConfSlice);
});