Skip to content

Commit

Permalink
Fix: Bug in using resfull service in dropdown in matrix dynamic + usi…
Browse files Browse the repository at this point in the history
…ng hasOther and setting old data #2854
  • Loading branch information
andrewtelnov committed May 8, 2021
1 parent 3654d55 commit 8301de7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/question_baseselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,19 @@ export class QuestionSelectBase extends Question {
this.setPropertyValue("renderedValue", val);
this.value = this.rendredValueToData(val);
}
protected setQuestionValue(newValue: any, updateIsAnswered: boolean = true) {
protected setQuestionValue(
newValue: any,
updateIsAnswered: boolean = true,
updateComment: boolean = true
) {
if (
this.isLoadingFromJson ||
Helpers.isTwoValueEquals(this.value, newValue)
)
return;
super.setQuestionValue(newValue, updateIsAnswered);
this.setPropertyValue("renderedValue", this.rendredValueFromData(newValue));
if (this.hasComment) return;
if (this.hasComment || !updateComment) return;
var isOtherSel = this.isOtherSelected;
if (isOtherSel && !!this.prevCommentValue) {
var oldComment = this.prevCommentValue;
Expand Down Expand Up @@ -905,7 +909,7 @@ export class QuestionSelectBase extends Question {
try {
if (!this.isValueEmpty(newValue.value)) {
this.allowNotifyValueChanged = false;
this.setQuestionValue(undefined);
this.setQuestionValue(undefined, true, false);
}
this.allowNotifyValueChanged = hasChanged;
if (hasChanged) {
Expand Down
40 changes: 37 additions & 3 deletions tests/choicesRestfultests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ class QuestionMatrixDynamicModelTester extends QuestionMatrixDynamicModel {
row: MatrixDropdownRowModelBase,
column: MatrixDropdownColumn
): Question {
var question = super.createQuestionCore(row, column);
if (column.cellType == "dropdown") {
var newQuestion = new QuestionDropdownModelTester(this.name);
var json = new JsonObject().toJsonObject(question);
newQuestion.hasItemsCallbackDelay = true;
var json = column.templateQuestion.toJSON();
new JsonObject().toObject(json, newQuestion);
newQuestion.setSurveyImpl(row);
return newQuestion;
}
return question;
return super.createQuestionCore(row, column);
}

processor: ITextProcessor;
Expand Down Expand Up @@ -1392,6 +1392,40 @@ QUnit.test("choicesByUrl + clear invsible values", function(assert) {
assert.equal(question2.isEmpty(), true, "Value is empty, locale choices");
});

QUnit.test("matrix dynamic and has other, Bug #2854", function(assert) {
var survey = new SurveyModel();
survey.addNewPage("1");
var question = new QuestionMatrixDynamicModelTester("q1");
question.rowCount = 1;
var column = question.addColumn("country");
column.cellType = "dropdown";
column.hasOther = true;
column["choicesByUrl"].url = "allcountries";
column["choicesByUrl"].path = "RestResponse;result";
survey.pages[0].addQuestion(question);
var data = {
q1: [{ country: "Afghanistan", "country-Comment": "Comment" }],
};
survey.data = data;
question.onSurveyLoad();
var rows = question.visibleRows;
var cellDropdown = <QuestionDropdownModelTester>rows[0].cells[0].question;
assert.equal(
cellDropdown.visibleChoices.length,
1,
"Choices are not loaded yet, 0 + other"
);
cellDropdown.doResultsCallback();
assert.equal(
cellDropdown.visibleChoices.length,
5 + 1,
"Choices are loaded, + hasOther"
);
assert.equal(cellDropdown.value, "Afghanistan");
survey.doComplete();
assert.deepEqual(survey.data, data, "value is not changed");
});

function getCACities() {
return ["Los Angeles", "San Francisco"];
}
Expand Down

0 comments on commit 8301de7

Please sign in to comment.