diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js index a76fef422..dcac6cf8e 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js @@ -160,7 +160,7 @@ export const scoringCardHooks = (scoring, updateSettings, defaultValue) => { const handleWeightChange = (event) => { let weight = parseFloat(event.target.value); - if (_.isNaN(weight)) { + if (_.isNaN(weight) || weight < 0) { weight = 0; } updateSettings({ scoring: { ...scoring, weight } }); @@ -194,7 +194,7 @@ export const useAnswerSettings = (showAnswer, updateSettings) => { const handleAttemptsChange = (event) => { let attempts = parseInt(event.target.value); - if (_.isNaN(attempts)) { + if (_.isNaN(attempts) || attempts < 0) { attempts = 0; } updateSettings({ showAnswer: { ...showAnswer, afterAttempts: attempts } }); @@ -210,7 +210,7 @@ export const useAnswerSettings = (showAnswer, updateSettings) => { export const timerCardHooks = (updateSettings) => ({ handleChange: (event) => { let time = parseInt(event.target.value); - if (_.isNaN(time)) { + if (_.isNaN(time) || time < 0) { time = 0; } updateSettings({ timeBetween: time }); diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/ScoringCard.jsx b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/ScoringCard.jsx index 60dc03447..b65a473f5 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/ScoringCard.jsx +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/ScoringCard.jsx @@ -49,6 +49,8 @@ export const ScoringCard = ({ (event) => { if (!isAnswerRangeSet({ answers })) { - const newTolerance = { value: event.target.value, type: tolerance.type }; + let value = parseFloat(event.target.value); + if (value < 0) { + value = 0; + } + const newTolerance = { value, type: tolerance.type }; updateSettings({ tolerance: newTolerance }); } }; @@ -92,6 +96,8 @@ export const ToleranceCard = ({ { const { queryByTestId } = render(); expect(queryByTestId('input')).toBeFalsy(); }); - it('Renders with intial value of tolerance', async () => { + it('Renders with initial value of tolerance', async () => { const { queryByTestId } = render(); expect(queryByTestId('input')).toBeTruthy(); expect(screen.getByDisplayValue('0')).toBeTruthy(); @@ -146,7 +146,12 @@ describe('ToleranceCard', () => { it('Calls change function on change.', () => { const { queryByTestId } = render(); fireEvent.change(queryByTestId('input'), { target: { value: 52 } }); - expect(props.updateSettings).toHaveBeenCalledWith({ tolerance: { type: ToleranceTypes.number.type, value: '52' } }); + expect(props.updateSettings).toHaveBeenCalledWith({ tolerance: { type: ToleranceTypes.number.type, value: 52 } }); + }); + it('Resets negative value on change.', () => { + const { queryByTestId } = render(); + fireEvent.change(queryByTestId('input'), { target: { value: -52 } }); + expect(props.updateSettings).toHaveBeenCalledWith({ tolerance: { type: ToleranceTypes.number.type, value: 0 } }); }); }); }); diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/__snapshots__/ScoringCard.test.jsx.snap b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/__snapshots__/ScoringCard.test.jsx.snap index e6e044d92..9f07b02d0 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/__snapshots__/ScoringCard.test.jsx.snap +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/__snapshots__/ScoringCard.test.jsx.snap @@ -20,7 +20,9 @@ exports[`ScoringCard snapshot snapshot: scoring setting card 1`] = ` @@ -36,8 +38,10 @@ exports[`ScoringCard snapshot snapshot: scoring setting card 1`] = ` @@ -111,8 +117,10 @@ exports[`ScoringCard snapshot snapshot: scoring setting card max attempts 1`] = @@ -186,8 +196,10 @@ exports[`ScoringCard snapshot snapshot: scoring setting card zero zero weight 1`