From 390d620664631713d177b0bd0ba6b93021e17f1d Mon Sep 17 00:00:00 2001 From: Ken Clary Date: Wed, 11 Jan 2023 16:11:39 -0500 Subject: [PATCH] fix: match new frontend behavior with legacy behavior (zero problem attempts is zero attempts, null attempts is infinite); negative values disallowed and forced to zero. TNL-10324. --- .../EditProblemView/SettingsWidget/hooks.js | 8 +++--- .../SettingsWidget/hooks.test.js | 26 ++++++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js index 6ef3e3c76..47c88e6e7 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js @@ -113,14 +113,14 @@ export const resetCardHooks = (updateSettings) => { export const scoringCardHooks = (scoring, updateSettings) => { const handleMaxAttemptChange = (event) => { - let unlimitedAttempts = true; + let unlimitedAttempts = false; let attemptNumber = parseInt(event.target.value); if (_.isNaN(attemptNumber)) { + attemptNumber = null; + unlimitedAttempts = true; + } else if (attemptNumber < 0) { attemptNumber = 0; } - if (attemptNumber > 0) { - unlimitedAttempts = false; - } updateSettings({ scoring: { ...scoring, attempts: { number: attemptNumber, unlimited: unlimitedAttempts } } }); }; diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js index 0f7997e68..556004402 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js @@ -178,7 +178,31 @@ describe('Problem settings hooks', () => { const value = 0; output.handleMaxAttemptChange({ target: { value } }); expect(updateSettings) - .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: value, unlimited: true } } }); + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: value, unlimited: false } } }); + }); + test('test handleMaxAttemptChange set attempts to null value', () => { + const value = null; + output.handleMaxAttemptChange({ target: { value } }); + expect(updateSettings) + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: true } } }); + }); + test('test handleMaxAttemptChange set attempts to empty string', () => { + const value = ''; + output.handleMaxAttemptChange({ target: { value } }); + expect(updateSettings) + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: true } } }); + }); + test('test handleMaxAttemptChange set attempts to non-numeric value', () => { + const value = 'abc'; + output.handleMaxAttemptChange({ target: { value } }); + expect(updateSettings) + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: true } } }); + }); + test('test handleMaxAttemptChange set attempts to negative value', () => { + const value = -1; + output.handleMaxAttemptChange({ target: { value } }); + expect(updateSettings) + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: 0, unlimited: false } } }); }); test('test handleWeightChange', () => { const value = 2;