From 4df44ab6cfee9ab3105e4e7b8947795148eb1c84 Mon Sep 17 00:00:00 2001 From: Max Sokolski Date: Thu, 18 Dec 2025 02:22:31 +0200 Subject: [PATCH] fix: coerce multiple choice text elements to String (#2752) --- .../ProblemEditor/data/OLXParser.js | 4 +-- .../ProblemEditor/data/OLXParser.test.js | 14 ++++++++ .../data/mockData/olxTestData.js | 32 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/editors/containers/ProblemEditor/data/OLXParser.js b/src/editors/containers/ProblemEditor/data/OLXParser.js index 63254cffd..5fbb69374 100644 --- a/src/editors/containers/ProblemEditor/data/OLXParser.js +++ b/src/editors/containers/ProblemEditor/data/OLXParser.js @@ -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); diff --git a/src/editors/containers/ProblemEditor/data/OLXParser.test.js b/src/editors/containers/ProblemEditor/data/OLXParser.test.js index fd9fad8e2..49cc87d1f 100644 --- a/src/editors/containers/ProblemEditor/data/OLXParser.test.js +++ b/src/editors/containers/ProblemEditor/data/OLXParser.test.js @@ -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', () => { diff --git a/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js b/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js index 4cd9856eb..e23adccfe 100644 --- a/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js +++ b/src/editors/containers/ProblemEditor/data/mockData/olxTestData.js @@ -945,6 +945,38 @@ export const numberParseTestOLX = { `, }; +export const numericTextCoercionTestOLX = { + rawOLX: ` + +

Select the correct number:

+ + 123 + 456 + 789 + +
+
`, + data: { + answers: [ + { + id: 'A', + title: '123', + correct: false, + }, + { + id: 'B', + title: '456', + correct: true, + }, + { + id: 'C', + title: '789', + correct: false, + }, + ], + }, +}; + export const solutionExplanationTest = { rawOLX: ` How 99 long is the array q after the following loop runs?