From 7f6ef9a702ea0687f071ab1fb6e93c01a6fd272b Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 3 Jun 2024 17:13:06 +0300 Subject: [PATCH] Multi-Select Matrix with a 'showInMultipleColumns' Checkboxes column produces a wrong response fix #8348 (#8353) --- src/question_matrixdropdowncolumn.ts | 14 ++++++-- tests/question_matrixdropdownbasetests.ts | 42 ++++++++++++++++++++++- tests/surveytests.ts | 2 +- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/question_matrixdropdowncolumn.ts b/src/question_matrixdropdowncolumn.ts index 4f588d7626..2975fe4964 100644 --- a/src/question_matrixdropdowncolumn.ts +++ b/src/question_matrixdropdowncolumn.ts @@ -786,6 +786,13 @@ export class MatrixDropdownColumn extends Base json[prop] = this.jsonObj[prop]; }); } + if(json["choicesOrder"] === "random") { + json["choicesOrder"] = "none"; + const visChoices = this.templateQuestion["visibleChoices"]; + if(Array.isArray(visChoices)) { + json["choices"] = visChoices; + } + } new JsonObject().toObject(json, question); question.isContentElement = this.templateQuestion.isContentElement; @@ -806,8 +813,11 @@ export class MatrixDropdownColumn extends Base this.updateIsRenderedRequired(newValue); } if (!this.colOwner || this.isLoadingFromJson) return; - if (this.isShowInMultipleColumns && ["visibleChoices", "choices"].indexOf(name) > -1) { - this.colOwner.onShowInMultipleColumnsChanged(this); + if (this.isShowInMultipleColumns) { + if(name === "choicesOrder") return; + if(["visibleChoices", "choices"].indexOf(name) > -1) { + this.colOwner.onShowInMultipleColumnsChanged(this); + } } if (!Serializer.hasOriginalProperty(this, name)) return; this.colOwner.onColumnPropertyChanged(this, name, newValue); diff --git a/tests/question_matrixdropdownbasetests.ts b/tests/question_matrixdropdownbasetests.ts index fd0a7c4774..7462947e8a 100644 --- a/tests/question_matrixdropdownbasetests.ts +++ b/tests/question_matrixdropdownbasetests.ts @@ -2,9 +2,10 @@ import { Serializer } from "../src/jsonobject"; import { QuestionDropdownModel } from "../src/question_dropdown"; import { QuestionMatrixDropdownModelBase } from "../src/question_matrixdropdownbase"; import { MatrixDropdownColumn } from "../src/question_matrixdropdowncolumn"; -import { QuestionMatrixDynamicModel } from "../src/question_matrixdynamic"; +import { MatrixDynamicRowModel, QuestionMatrixDynamicModel } from "../src/question_matrixdynamic"; import { QuestionTagboxModel } from "../src/question_tagbox"; import { SurveyModel } from "../src/survey"; +import { Helpers } from "../src/helpers"; export * from "../src/localization/german"; export default QUnit.module("Survey_QuestionMatrixDropdownBase"); @@ -1344,4 +1345,43 @@ QUnit.test("defaultValueExpression & using rowvalue in it", function (assert) { assert.equal(matrix.visibleRows[0].cells[1].question.value, 12, "Keep value in the standard cell"); assert.equal(matrix.visibleRows[0].cells[0].question.value, 30, "cell1 value #3"); assert.equal(matrix.visibleRows[1].cells[0].question.value, 50, "cell1 value #3"); +}); +QUnit.test("showInMultipleColumns & random choices, Bug#8348", function (assert) { + let index = 0; + class HelpTest { + public static randomizeArray(array: Array): Array { + if (array.length < 2) return array; + const el0 = array[index]; + array.splice(0, 1, array[array.length - index - 1]); + array.splice(array.length - index - 1, 1, el0); + index = (index + 1) % array.length; + return array; + } + } + const oldFunc = Helpers.randomizeArray; + Helpers.randomizeArray = HelpTest.randomizeArray; + const survey = new SurveyModel({ + elements: [ + { + "type": "matrixdropdown", + "name": "matrix", + "columns": [ + { + "name": "column", + "cellType": "radiogroup", + "showInMultipleColumns": true, + "choices": ["col1", "col2", "col3", "col4", "col5"], + "choicesOrder": "random" + } + ], + "rows": ["row1", "row2"] + } + ] + }); + const matrix = survey.getQuestionByName("matrix"); + const rows = matrix.renderedTable.rows; + assert.equal(matrix.columns[0].templateQuestion.visibleChoices[0].value, "col5", "column tempate"); + assert.equal(rows[0].cells[1].question.visibleChoices[0].value, "col5", "row1 question"); + assert.equal(rows[1].cells[1].question.visibleChoices[0].value, "col5", "row2 question"); + Helpers.randomizeArray = oldFunc; }); \ No newline at end of file diff --git a/tests/surveytests.ts b/tests/surveytests.ts index 9c339dd147..9d4ac151de 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -7507,7 +7507,7 @@ QUnit.test("Questions are randomized", function (assert) { class HelpTest { public static randomizeArray(array: Array): Array { - if (array.length < 2) return; + if (array.length < 2) return array; const el0 = array[0]; array.splice(0, 1, array[array.length - 1]); array.splice(array.length - 1, 1, el0);