-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front: create train settings reducer
Signed-off-by: Clara Ni <[email protected]>
- Loading branch information
Showing
10 changed files
with
209 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
front/src/reducers/osrdconf/operationalStudiesConf/__tests__/trainSettingsReducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { beforeEach, it, expect } from 'vitest'; | ||
|
||
import type { Distribution, TrainScheduleBase } from 'common/api/osrdEditoastApi'; | ||
import { | ||
operationalStudiesConfSlice, | ||
type OperationalStudiesConfState, | ||
} from 'reducers/osrdconf/operationalStudiesConf'; | ||
import { defaultCommonConf } from 'reducers/osrdconf/osrdConfCommon'; | ||
import { createStoreWithoutMiddleware } from 'store'; | ||
|
||
const createStore = (extraInitialState?: Partial<OperationalStudiesConfState>) => | ||
createStoreWithoutMiddleware({ | ||
[operationalStudiesConfSlice.name]: { | ||
...defaultCommonConf, | ||
...extraInitialState, | ||
}, | ||
}); | ||
|
||
const testTrainSettingsReducer = () => { | ||
let defaultStore: ReturnType<typeof createStore>; | ||
const slice = operationalStudiesConfSlice; | ||
const { | ||
updateConstraintDistribution, | ||
updateName, | ||
updateTrainCount, | ||
updateTrainDelta, | ||
updateTrainStep, | ||
toggleUsingElectricalProfiles, | ||
updateLabels, | ||
updateInitialSpeed, | ||
updateRollingStockComfort, | ||
updateStartTime, | ||
} = slice.actions; | ||
|
||
const getState = () => defaultStore.getState()[operationalStudiesConfSlice.name]; | ||
|
||
beforeEach(() => { | ||
defaultStore = createStore(operationalStudiesConfSlice); | ||
}); | ||
|
||
it('should handle updateConstraintDistribution', () => { | ||
const newConstraintDistribution: Distribution = 'STANDARD'; | ||
defaultStore.dispatch(updateConstraintDistribution(newConstraintDistribution)); | ||
|
||
const state = getState(); | ||
expect(state.constraintDistribution).toBe(newConstraintDistribution); | ||
}); | ||
|
||
it('should handle updateName', () => { | ||
const newName = 'New Simulation Name'; | ||
defaultStore.dispatch(updateName(newName)); | ||
|
||
const state = getState(); | ||
expect(state.name).toBe(newName); | ||
}); | ||
|
||
it('should handle updateTrainCount', () => { | ||
const newTrainCount = 5; | ||
defaultStore.dispatch(updateTrainCount(newTrainCount)); | ||
|
||
const state = getState(); | ||
expect(state.trainCount).toBe(newTrainCount); | ||
}); | ||
|
||
it('should handle updateTrainDelta', () => { | ||
const newTrainDelta = 5; | ||
defaultStore.dispatch(updateTrainDelta(newTrainDelta)); | ||
|
||
const state = getState(); | ||
expect(state.trainDelta).toBe(newTrainDelta); | ||
}); | ||
|
||
it('should handle updateTrainStep', () => { | ||
const newTrainStep = 5; | ||
defaultStore.dispatch(updateTrainStep(newTrainStep)); | ||
|
||
const state = getState(); | ||
expect(state.trainStep).toBe(newTrainStep); | ||
}); | ||
|
||
it('should handle toggleUsingElectricalProfiles', () => { | ||
defaultStore.dispatch(toggleUsingElectricalProfiles()); | ||
|
||
let state = getState(); | ||
expect(state.usingElectricalProfiles).toBe(false); | ||
|
||
defaultStore.dispatch(toggleUsingElectricalProfiles()); | ||
state = getState(); | ||
expect(state.usingElectricalProfiles).toBe(true); | ||
}); | ||
|
||
it('should handle updateLabels', () => { | ||
const newLabels = ['A', 'B']; | ||
defaultStore.dispatch(updateLabels(newLabels)); | ||
const state = getState(); | ||
expect(state.labels).toBe(newLabels); | ||
}); | ||
|
||
it('should handle updateInitialSpeed', () => { | ||
const newInitialSpeed = 50; | ||
defaultStore.dispatch(updateInitialSpeed(newInitialSpeed)); | ||
const state = getState(); | ||
expect(state.initialSpeed).toBe(newInitialSpeed); | ||
}); | ||
|
||
it('should handle updateRollingStockComfort', () => { | ||
const newRollingStockComfort: TrainScheduleBase['comfort'] = 'AIR_CONDITIONING'; | ||
defaultStore.dispatch(updateRollingStockComfort(newRollingStockComfort)); | ||
const state = getState(); | ||
expect(state.rollingStockComfort).toBe(newRollingStockComfort); | ||
}); | ||
|
||
it('should handle updateStartTime', () => { | ||
const newStartTime = new Date('2024-05-01T11:08:00.000+01:00'); | ||
defaultStore.dispatch(updateStartTime(newStartTime)); | ||
const state = getState(); | ||
expect(state.startTime).toBe(newStartTime); | ||
}); | ||
}; | ||
|
||
export default testTrainSettingsReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
front/src/reducers/osrdconf/operationalStudiesConf/trainSettingsReducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { PayloadAction } from '@reduxjs/toolkit'; | ||
import type { Draft } from 'immer'; | ||
|
||
import type { OsrdConfState } from 'reducers/osrdconf/types'; | ||
import type { OperationalStudiesConfState } from '.'; | ||
|
||
export function buildTrainSettingsReducer() { | ||
return { | ||
updateConstraintDistribution( | ||
state: Draft<OperationalStudiesConfState>, | ||
action: PayloadAction<OperationalStudiesConfState['constraintDistribution']> | ||
) { | ||
state.constraintDistribution = action.payload; | ||
}, | ||
updateName( | ||
state: Draft<OperationalStudiesConfState>, | ||
action: PayloadAction<OperationalStudiesConfState['name']> | ||
) { | ||
state.name = action.payload; | ||
}, | ||
updateTrainCount( | ||
state: Draft<OperationalStudiesConfState>, | ||
action: PayloadAction<OperationalStudiesConfState['trainCount']> | ||
) { | ||
state.trainCount = action.payload; | ||
}, | ||
updateTrainDelta( | ||
state: Draft<OperationalStudiesConfState>, | ||
action: PayloadAction<OsrdConfState['trainDelta']> | ||
) { | ||
state.trainDelta = action.payload; | ||
}, | ||
updateTrainStep( | ||
state: Draft<OperationalStudiesConfState>, | ||
action: PayloadAction<OperationalStudiesConfState['trainStep']> | ||
) { | ||
state.trainStep = action.payload; | ||
}, | ||
toggleUsingElectricalProfiles(state: Draft<OperationalStudiesConfState>) { | ||
state.usingElectricalProfiles = !state.usingElectricalProfiles; | ||
}, | ||
updateLabels( | ||
state: Draft<OperationalStudiesConfState>, | ||
action: PayloadAction<OperationalStudiesConfState['labels']> | ||
) { | ||
state.labels = action.payload; | ||
}, | ||
updateInitialSpeed( | ||
state: Draft<OperationalStudiesConfState>, | ||
action: PayloadAction<OperationalStudiesConfState['initialSpeed']> | ||
) { | ||
state.initialSpeed = action.payload; | ||
}, | ||
updateStartTime( | ||
state: Draft<OperationalStudiesConfState>, | ||
action: PayloadAction<OperationalStudiesConfState['startTime']> | ||
) { | ||
state.startTime = action.payload; | ||
}, | ||
updateRollingStockComfort( | ||
state: Draft<OperationalStudiesConfState>, | ||
action: PayloadAction<OperationalStudiesConfState['rollingStockComfort']> | ||
) { | ||
state.rollingStockComfort = action.payload; | ||
}, | ||
}; | ||
} |
Oops, something went wrong.