generated from graasp/graasp-app-starter-ts-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configure electricity cost (#73)
* feat: add electricity cost parameter * feat: translate the errors of FormValidator * feat(test): add tests for the electricity cost input
- Loading branch information
Showing
9 changed files
with
154 additions
and
20 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
34 changes: 34 additions & 0 deletions
34
src/modules/scenes/SimulationControlPanel/ElectricityCostControl.tsx
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,34 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { useSimulation } from '@/context/SimulationContext'; | ||
import { FormControlValidator } from '@/modules/common/FormControlValidator'; | ||
|
||
export const ElectricityCostControl = (): JSX.Element => { | ||
const { t } = useTranslation('SIMULATION_CONTROL_PANEL'); | ||
const { setPricekWh } = useSimulation(); | ||
|
||
const handleElectricityCostChange = (newValue: string): void => { | ||
const newPrice = Number.parseFloat(newValue); | ||
|
||
if (!Number.isNaN(newPrice)) { | ||
setPricekWh(newPrice); | ||
} else { | ||
console.error(`Invalid price for ${newValue}`); | ||
} | ||
}; | ||
|
||
return ( | ||
<FormControlValidator | ||
label={t('ELECTRICITY_CONTROL_PANEL.ELECTRICITY_COST_LABEL')} | ||
value="0.22" | ||
onChange={(value) => handleElectricityCostChange(value)} | ||
validationRules={{ | ||
min: 1e-3, | ||
max: 2, | ||
required: true, | ||
isNumber: true, | ||
}} | ||
unit="CHF/kWh" | ||
/> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { expect, test } from '@playwright/test'; | ||
|
||
import { HousePage } from './HousePage'; | ||
|
||
test('should change electricity cost successfully', async ({ page }) => { | ||
const housePage = new HousePage(page); | ||
await housePage.goto(); | ||
await housePage.setElectricityCost('0.33'); | ||
}); | ||
|
||
test('should not accept text', async ({ page }) => { | ||
const housePage = new HousePage(page); | ||
await housePage.goto(); | ||
await housePage.electricityCost.click(); | ||
await housePage.electricityCost.pressSequentially('Not a number'); | ||
await expect(housePage.electricityCost).toHaveValue(''); | ||
}); | ||
|
||
test('should be required', async ({ page }) => { | ||
const housePage = new HousePage(page); | ||
await housePage.goto(); | ||
await housePage.electricityCost.click(); | ||
await housePage.electricityCost.clear(); | ||
await expect(housePage.electricityCost).toHaveValue(''); | ||
|
||
await housePage.checkErrorIsVisible('electricity-cost', 'Required'); | ||
}); | ||
|
||
test('should not be negative', async ({ page }) => { | ||
const housePage = new HousePage(page); | ||
await housePage.goto(); | ||
await housePage.setElectricityCost('-1'); | ||
|
||
await housePage.checkErrorIsVisible('electricity-cost', 'Min'); | ||
}); | ||
|
||
test('should not exceed max', async ({ page }) => { | ||
const housePage = new HousePage(page); | ||
await housePage.goto(); | ||
await housePage.setElectricityCost('5'); | ||
|
||
await housePage.checkErrorIsVisible('electricity-cost', 'Max'); | ||
}); |
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