Skip to content

Commit

Permalink
test: fix related test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Oct 9, 2023
1 parent c70679d commit 5df26bf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ exports[`ScoringCard snapshot snapshot: scoring setting card 1`] = `
<Form.Checkbox
checked={false}
className="mt-3 decoration-control-label"
disabled={true}
>
<div
className="x-small"
Expand Down Expand Up @@ -123,6 +124,7 @@ exports[`ScoringCard snapshot snapshot: scoring setting card max attempts 1`] =
<Form.Checkbox
checked={true}
className="mt-3 decoration-control-label"
disabled={true}
>
<div
className="x-small"
Expand Down Expand Up @@ -197,6 +199,7 @@ exports[`ScoringCard snapshot snapshot: scoring setting card zero zero weight 1`
<Form.Checkbox
checked={false}
className="mt-3 decoration-control-label"
disabled={true}
>
<div
className="x-small"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const parseScoringSettings = (metadata, defaultSettings) => {

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;
Expand Down
23 changes: 12 additions & 11 deletions src/editors/containers/ProblemEditor/data/SettingsParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -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({});
});
});

0 comments on commit 5df26bf

Please sign in to comment.