fix: add empty answer to problem without answers (#235)

This commit is contained in:
Kristin Aoki
2023-02-07 20:04:16 -05:00
committed by GitHub
parent 613b8d16ae
commit 87aaa7f3ff
3 changed files with 35 additions and 1 deletions

View File

@@ -59,7 +59,15 @@ export class OLXParser {
let data = {};
const widget = _.get(this.problem, `${problemType}.${widgetName}`);
const choice = _.get(widget, option);
if (_.isArray(choice)) {
if (_.isEmpty(choice)) {
answers.push(
{
id: indexToLetterMap[answers.length],
title: '',
correct: true,
},
);
} else if (_.isArray(choice)) {
choice.forEach((element, index) => {
const title = element['#text'];
const correct = eval(element['@_correct'].toLowerCase());

View File

@@ -6,6 +6,7 @@ import {
numericInputWithFeedbackAndHintsOLX,
numericInputWithFeedbackAndHintsOLXException,
textInputWithFeedbackAndHintsOLX,
multipleChoiceWithoutAnswers,
multipleChoiceWithFeedbackAndHintsOLX,
textInputWithFeedbackAndHintsOLXWithMultipleAnswers,
advancedProblemOlX,
@@ -88,6 +89,11 @@ describe('Check OLXParser hints', () => {
});
describe('Check OLXParser for answer parsing', () => {
test('Test check single select with empty answers', () => {
const olxparser = new OLXParser(multipleChoiceWithoutAnswers.rawOLX);
const answer = olxparser.parseMultipleChoiceAnswers('multiplechoiceresponse', 'choicegroup', 'choice');
expect(answer).toEqual(multipleChoiceWithoutAnswers.data);
});
test('Test checkbox answer', () => {
const olxparser = new OLXParser(checkboxesOLXWithFeedbackAndHintsOLX.rawOLX);
const answer = olxparser.parseMultipleChoiceAnswers('choiceresponse', 'checkboxgroup', 'choice');

View File

@@ -138,6 +138,26 @@ an incorrect answer <choicehint selected="true">You can specify optional
export const checkboxesOLXWithFeedbackAndHintsOLX = getCheckboxesOLXWithFeedbackAndHintsOLX({});
export const multipleChoiceWithoutAnswers = {
rawOLX: `<problem>
<multiplechoiceresponse>
<choicegroup>
</choicegroup>
</multiplechoiceresponse>
<demandhint></demandhint>
<solution></solution>
</problem>`,
data: {
answers: [
{
id: 'A',
title: '',
correct: true,
},
],
},
};
export const dropdownOLXWithFeedbackAndHintsOLX = {
rawOLX: `<problem>
<optionresponse>