fix: coerce multiple choice text elements to String (#2752)

This commit is contained in:
Max Sokolski
2025-12-18 02:22:31 +02:00
committed by GitHub
parent 0405156f91
commit 4df44ab6cf
3 changed files with 48 additions and 2 deletions

View File

@@ -252,7 +252,7 @@ export class OLXParser {
choice.forEach((element, index) => {
const preservedAnswer = preservedAnswers[index].filter(answer => !Object.keys(answer).includes(`${option}hint`));
const preservedFeedback = preservedAnswers[index].filter(answer => Object.keys(answer).includes(`${option}hint`));
let title = element['#text'];
let title = String(element['#text']);
if (isComplexAnswer && preservedAnswer) {
title = this.richTextBuilder.build(preservedAnswer);
@@ -272,7 +272,7 @@ export class OLXParser {
} else {
const preservedAnswer = preservedAnswers[0].filter(answer => !Object.keys(answer).includes(`${option}hint`));
const preservedFeedback = preservedAnswers[0].filter(answer => Object.keys(answer).includes(`${option}hint`));
let title = choice['#text'];
let title = String(choice['#text']);
if (isComplexAnswer && preservedAnswer) {
title = this.richTextBuilder.build(preservedAnswer);

View File

@@ -23,6 +23,7 @@ import {
labelDescriptionQuestionOLX,
htmlEntityTestOLX,
numberParseTestOLX,
numericTextCoercionTestOLX,
numericalProblemPartialCredit,
solutionExplanationTest,
solutionExplanationWithoutDivTest,
@@ -266,6 +267,19 @@ describe('OLXParser', () => {
expect(answers).toHaveLength(3);
});
});
describe('given multiple choice olx with numeric text that needs coercion', () => {
const olxparser = new OLXParser(numericTextCoercionTestOLX.rawOLX);
const { answers } = olxparser.parseMultipleChoiceAnswers('multiplechoiceresponse', 'choicegroup', 'choice');
it('should coerce numeric #text values to strings', () => {
expect(answers).toEqual(numericTextCoercionTestOLX.data.answers);
answers.forEach((answer) => {
expect(typeof answer.title).toBe('string');
});
});
it('should equal an array of objects with length three', () => {
expect(answers).toHaveLength(3);
});
});
});
describe('parseStringResponse()', () => {
// describe('given a problem with no answers', () => {

View File

@@ -945,6 +945,38 @@ export const numberParseTestOLX = {
</problem>`,
};
export const numericTextCoercionTestOLX = {
rawOLX: `<problem>
<multiplechoiceresponse>
<p>Select the correct number:</p>
<choicegroup type="MultipleChoice">
<choice correct="false">123</choice>
<choice correct="true">456</choice>
<choice correct="false">789</choice>
</choicegroup>
</multiplechoiceresponse>
</problem>`,
data: {
answers: [
{
id: 'A',
title: '123',
correct: false,
},
{
id: 'B',
title: '456',
correct: true,
},
{
id: 'C',
title: '789',
correct: false,
},
],
},
};
export const solutionExplanationTest = {
rawOLX: `<problem>
How <code class="lang-matlab">99</code> long is the array <code class="lang-matlab">q</code> after the following loop runs?