Skip to content

Commit

Permalink
fix: default setting not updating with updated default settings (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristinAoki authored Mar 28, 2024
1 parent 4fa1695 commit 905bea0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProblemTypeKeys } from '../../../../data/constants/problem';
import { ProblemTypeKeys, ShowAnswerTypesKeys } from '../../../../data/constants/problem';
import * as hooks from './hooks';
import { MockUseState } from '../../../../../testUtils';

Expand All @@ -7,26 +7,27 @@ const mockBuiltOLX = 'builtOLX';
const mockGetSettings = {
max_attempts: 1,
weight: 2,
showanswer: 'finished',
showanswer: ShowAnswerTypesKeys.AFTER_SOME_NUMBER_OF_ATTEMPTS,
show_reset_button: false,
rerandomize: 'never',
};
const mockParseRawOlxSettingsDiscrepancy = {
max_attempts: 1,
weight: 2,
showanswer: 'finished',
showanswer: ShowAnswerTypesKeys.AFTER_SOME_NUMBER_OF_ATTEMPTS,
show_reset_button: true,
rerandomize: 'never',
};
const mockParseRawOlxSettings = {
max_attempts: 1,
weight: 2,
showanswer: 'finished',
showanswer: ShowAnswerTypesKeys.AFTER_SOME_NUMBER_OF_ATTEMPTS,
show_reset_button: false,
rerandomize: 'never',
};
const problemState = {
problemType: ProblemTypeKeys.ADVANCED,
defaultSettings: {},
settings: {
randomization: null,
scoring: {
Expand All @@ -38,7 +39,7 @@ const problemState = {
},
timeBetween: 0,
showAnswer: {
on: 'finished',
on: ShowAnswerTypesKeys.AFTER_SOME_NUMBER_OF_ATTEMPTS,
afterAttempts: 0,
},
showResetButton: false,
Expand Down Expand Up @@ -281,7 +282,7 @@ describe('EditProblemView hooks parseState', () => {
const expectedSettings = {
max_attempts: '',
weight: 1,
showanswer: 'finished',
showanswer: ShowAnswerTypesKeys.AFTER_SOME_NUMBER_OF_ATTEMPTS,
show_reset_button: false,
submission_wait_seconds: 0,
attempts_before_showanswer_button: 0,
Expand All @@ -303,6 +304,34 @@ describe('EditProblemView hooks parseState', () => {
settings: expectedSettings,
});
});

it('returned parseState content.settings should not include default values', () => {
const problem = {
...problemState,
problemType: ProblemTypeKeys.NUMERIC,
answers: [{ id: 'A', title: 'problem', correct: true }],
defaultSettings: {
maxAttempts: '',
showanswer: ShowAnswerTypesKeys.AFTER_SOME_NUMBER_OF_ATTEMPTS,
showResetButton: false,
rerandomize: 'never',
},
};
const { settings } = hooks.getContent({
isAdvancedProblemType: false,
problemState: problem,
editorRef,
assets,
lmsEndpointUrl,
openSaveWarningModal,
});
expect(settings).toEqual({
attempts_before_showanswer_button: 0,
submission_wait_seconds: 0,
weight: 1,
});
});

it('default advanced save and returns parseState data', () => {
const content = hooks.getContent({
isAdvancedProblemType: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { XMLParser } from 'fast-xml-parser';
import _ from 'lodash-es';

import {
ShowAnswerTypesKeys,
} from '../../../data/constants/problem';
import { popuplateItem } from './SettingsParser';

const SETTING_KEYS = [
Expand All @@ -17,23 +22,30 @@ class ReactStateSettingsParser {

getSettings() {
let settings = {};
const { defaultSettings } = this.problem;
const stateSettings = this.problem.settings;

settings = popuplateItem(settings, 'number', 'max_attempts', stateSettings.scoring.attempts, true);
const numberOfAttemptsChoice = [
ShowAnswerTypesKeys.AFTER_SOME_NUMBER_OF_ATTEMPTS,
ShowAnswerTypesKeys.AFTER_ALL_ATTEMPTS,
ShowAnswerTypesKeys.AFTER_ALL_ATTEMPTS_OR_CORRECT,
];

settings = popuplateItem(settings, 'number', 'max_attempts', stateSettings.scoring.attempts, defaultSettings?.maxAttempts, true);
settings = popuplateItem(settings, 'weight', 'weight', stateSettings.scoring);
settings = popuplateItem(settings, 'on', 'showanswer', stateSettings.showAnswer);
settings = popuplateItem(settings, 'afterAttempts', 'attempts_before_showanswer_button', stateSettings.showAnswer);
settings = popuplateItem(settings, 'showResetButton', 'show_reset_button', stateSettings);
settings = popuplateItem(settings, 'on', 'showanswer', stateSettings.showAnswer, defaultSettings?.showanswer);
if (_.includes(numberOfAttemptsChoice, stateSettings.showAnswer.on)) {
settings = popuplateItem(settings, 'afterAttempts', 'attempts_before_showanswer_button', stateSettings.showAnswer);
}
settings = popuplateItem(settings, 'showResetButton', 'show_reset_button', stateSettings, defaultSettings?.showResetButton);
settings = popuplateItem(settings, 'timeBetween', 'submission_wait_seconds', stateSettings);
settings = popuplateItem(settings, 'randomization', 'rerandomize', stateSettings);
settings = popuplateItem(settings, 'randomization', 'rerandomize', stateSettings, defaultSettings?.rerandomize);

return settings;
}

parseRawOlxSettings() {
const rawOlxSettings = this.getSettings();
// console.log(rawOlxSettings);
// console.log(this.rawOLX);
const parserOptions = {
ignoreAttributes: false,
alwaysCreateTextNode: true,
Expand Down
5 changes: 3 additions & 2 deletions src/editors/containers/ProblemEditor/data/SettingsParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import _ from 'lodash-es';

import { ShowAnswerTypes, RandomizationTypesKeys } from '../../../data/constants/problem';

export const popuplateItem = (parentObject, itemName, statekey, metadata, allowNull = false) => {
export const popuplateItem = (parentObject, itemName, statekey, metadata, defaultValue = null, allowNull = false) => {
let parent = parentObject;
const item = _.get(metadata, itemName, null);
if (!_.isNil(item) || allowNull) {
const equalsDefault = item === defaultValue;
if ((!_.isNil(item) || allowNull) && !equalsDefault) {
parent = { ...parentObject, [statekey]: item };
}
return parent;
Expand Down

0 comments on commit 905bea0

Please sign in to comment.