From 5df26bf83b9fc3101c2d519a2bf6d2d29ef76c12 Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Mon, 9 Oct 2023 20:34:05 +0530 Subject: [PATCH] test: fix related test cases --- .../EditProblemView/SettingsWidget/hooks.js | 5 ++-- .../SettingsWidget/hooks.test.js | 16 ++++++------- .../__snapshots__/ScoringCard.test.jsx.snap | 3 +++ .../ProblemEditor/data/SettingsParser.js | 2 +- .../ProblemEditor/data/SettingsParser.test.js | 23 ++++++++++--------- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js index 23e02b052..4183d5bf4 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.js @@ -126,12 +126,11 @@ export const scoringCardHooks = (scoring, updateSettings, defaultValue) => { let attemptNumber = parseInt(event.target.value); const { value } = event.target; // TODO: impove below condition handling - if (_.isNaN(attemptNumber)) { + if (_.isNaN(attemptNumber) || _.isNil(attemptNumber)) { + attemptNumber = null; if (value === '' && !_.isNil(defaultValue)) { - attemptNumber = null; setAttemptDisplayValue(`${defaultValue} (Default)`); } else if (_.isNil(defaultValue)) { - attemptNumber = null; unlimitedAttempts = true; } } else if (attemptNumber <= 0) { 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 a93ddc294..49dc36170 100644 --- a/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js +++ b/src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/hooks.test.js @@ -155,13 +155,13 @@ describe('Problem settings hooks', () => { output.handleUnlimitedChange({ target: { checked: true } }); expect(state.setState[state.keys.attemptDisplayValue]).toHaveBeenCalledWith(''); expect(updateSettings) - .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: '', unlimited: true } } }); + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: true } } }); }); test('test handleUnlimitedChange sets attempts.unlimited to false when unchecked', () => { output.handleUnlimitedChange({ target: { checked: false } }); expect(state.setState[state.keys.attemptDisplayValue]).toHaveBeenCalledWith(`${defaultValue} (Default)`); expect(updateSettings) - .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: defaultValue, unlimited: false } } }); + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: false } } }); }); test('test handleMaxAttemptChange', () => { const value = 6; @@ -175,30 +175,30 @@ describe('Problem settings hooks', () => { expect(updateSettings) .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: value, unlimited: false } } }); }); - test('test handleMaxAttemptChange set attempts to null value', () => { + test('test handleMaxAttemptChange set attempts to null value when default max_attempts is present', () => { const value = null; output.handleMaxAttemptChange({ target: { value } }); expect(updateSettings) - .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: '', unlimited: true } } }); + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: false } } }); }); - test('test handleMaxAttemptChange set attempts to default value', () => { + test('test handleMaxAttemptChange set attempts to null when default value is inputted', () => { const value = '1 (Default)'; output.handleMaxAttemptChange({ target: { value } }); expect(updateSettings) - .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: 1, unlimited: false } } }); + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: false } } }); }); test('test handleMaxAttemptChange set attempts to non-numeric value', () => { const value = 'abc'; output.handleMaxAttemptChange({ target: { value } }); expect(updateSettings) - .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: '', unlimited: true } } }); + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: false } } }); }); test('test handleMaxAttemptChange set attempts to empty value', () => { const value = ''; output.handleMaxAttemptChange({ target: { value } }); expect(state.setState[state.keys.attemptDisplayValue]).toHaveBeenCalledWith(`${defaultValue} (Default)`); expect(updateSettings) - .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: 1, unlimited: false } } }); + .toHaveBeenCalledWith({ scoring: { ...scoring, attempts: { number: null, unlimited: false } } }); }); test('test handleMaxAttemptChange set attempts to negative value', () => { const value = -1; 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 b4d64a781..131399c82 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 @@ -49,6 +49,7 @@ exports[`ScoringCard snapshot snapshot: scoring setting card 1`] = `
{ let attempts = popuplateItem({}, 'max_attempts', 'number', metadata); // TODO: impove below condition handling - if ((_.isEmpty(attempts) || _.isNaN(attempts.number)) && _.isNaN(defaultSettings.max_attempts)) { + if ((_.isEmpty(attempts) || _.isNaN(attempts.number)) && (_.isEmpty(defaultSettings) || _.isNaN(defaultSettings.max_attempts))) { attempts = { unlimited: true, number: '' }; } else if (!_.isEmpty(attempts) && !_.isNaN(attempts.number)) { attempts.unlimited = false; diff --git a/src/editors/containers/ProblemEditor/data/SettingsParser.test.js b/src/editors/containers/ProblemEditor/data/SettingsParser.test.js index 2f01f3f38..c5278ae11 100644 --- a/src/editors/containers/ProblemEditor/data/SettingsParser.test.js +++ b/src/editors/containers/ProblemEditor/data/SettingsParser.test.js @@ -8,39 +8,40 @@ import { } from './mockData/problemTestData'; describe('Test Settings to State Parser', () => { + const defaultSettings = { max_attempts: 1 }; test('Test all fields populated', () => { - const settings = parseSettings(checklistWithFeebackHints.metadata); + const settings = parseSettings(checklistWithFeebackHints.metadata, defaultSettings); const { hints, ...settingsPayload } = checklistWithFeebackHints.state.settings; expect(settings).toStrictEqual(settingsPayload); }); test('Test score settings', () => { - const scoreSettings = parseScoringSettings(checklistWithFeebackHints.metadata); + const scoreSettings = parseScoringSettings(checklistWithFeebackHints.metadata, defaultSettings); expect(scoreSettings).toStrictEqual(checklistWithFeebackHints.state.settings.scoring); }); test('Test score settings zero attempts', () => { - const scoreSettings = parseScoringSettings(numericWithHints.metadata); + const scoreSettings = parseScoringSettings(numericWithHints.metadata, defaultSettings); expect(scoreSettings).toStrictEqual(numericWithHints.state.settings.scoring); }); - test('Test score settings attempts missing', () => { - const scoreSettings = parseScoringSettings(singleSelectWithHints.metadata); + test('Test score settings attempts missing with no default max_attempts', () => { + const scoreSettings = parseScoringSettings(singleSelectWithHints.metadata, {}); expect(scoreSettings.attempts).toStrictEqual(singleSelectWithHints.state.settings.scoring.attempts); }); test('Test negative attempts in score', () => { - const scoreSettings = parseScoringSettings(negativeAttempts.metadata); + const scoreSettings = parseScoringSettings(negativeAttempts.metadata, defaultSettings); expect(scoreSettings.attempts).toStrictEqual(negativeAttempts.state.settings.scoring.attempts); }); - test('Test score settings missing', () => { - const settings = parseSettings(singleSelectWithHints.metadata); + test('Test score settings missing with no default', () => { + const settings = parseSettings(singleSelectWithHints.metadata, {}); expect(settings.scoring).toStrictEqual(singleSelectWithHints.state.settings.scoring); }); test('Test invalid randomization', () => { - const settings = parseSettings(numericWithHints.metadata); + const settings = parseSettings(numericWithHints.metadata, defaultSettings); expect(settings.randomization).toBeUndefined(); }); @@ -55,12 +56,12 @@ describe('Test Settings to State Parser', () => { }); test('Test empty metadata', () => { - const scoreSettings = parseSettings({}); + const scoreSettings = parseSettings({}, defaultSettings); expect(scoreSettings).toStrictEqual({}); }); test('Test null metadata', () => { - const scoreSettings = parseSettings(null); + const scoreSettings = parseSettings(null, defaultSettings); expect(scoreSettings).toStrictEqual({}); }); });