From 5b9ec3c7b9f2e8ccb2df8737f032bc678a5e2641 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 7 Nov 2023 08:53:41 +0200 Subject: [PATCH] defaultValueExpression for question component stopped working fix #7280 (#7281) --- src/question.ts | 5 ++++- tests/question_customtests.ts | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/question.ts b/src/question.ts index 1315dcd7a4..b601589c27 100644 --- a/src/question.ts +++ b/src/question.ts @@ -2311,7 +2311,7 @@ export class Question extends SurveyElement protected setQuestionValue(newValue: any, updateIsAnswered: boolean = true): void { newValue = this.convertToCorrectValue(newValue); const isEqual = this.isTwoValueEquals(this.questionValue, newValue); - if (!isEqual && !this.isChangingViaDefaultValue) { + if (!isEqual && !this.isChangingViaDefaultValue && !this.isParentChangingViaDefaultValue) { this.setValueChangedDirectly(true); } this.questionValue = newValue; @@ -2322,6 +2322,9 @@ export class Question extends SurveyElement this.fireCallback(this.valueChangedCallback); if (updateIsAnswered) this.updateIsAnswered(); } + private get isParentChangingViaDefaultValue(): boolean { + return (this.data)?.isChangingViaDefaultValue === true; + } onSurveyValueChanged(newValue: any): void { } public setVisibleIndex(val: number): number { if ( diff --git a/tests/question_customtests.ts b/tests/question_customtests.ts index 1241a12222..793bdd4bd2 100644 --- a/tests/question_customtests.ts +++ b/tests/question_customtests.ts @@ -725,6 +725,44 @@ QUnit.test("Single: defaultValueExpession, bug#4836", function (assert) { assert.deepEqual(survey.data, { q1: 2, q2: 5 }, "set data into survey, #2"); ComponentCollection.Instance.clear(); }); +QUnit.test("Single: defaultValueExpession & operations, bug#7280", function (assert) { + ComponentCollection.Instance.add({ + name: "newquestion", + questionJSON: { + type: "text", + inputType: "number" + }, + }); + const survey = new SurveyModel({ + elements: [ + { type: "newquestion", name: "q1" }, + { type: "newquestion", name: "q2" }, + { type: "newquestion", name: "q3", defaultValueExpression: "{q1} + {q2}" }], + }); + const q1 = survey.getQuestionByName("q1"); + const q2 = survey.getQuestionByName("q2"); + const q3 = survey.getQuestionByName("q3"); + q1.contentQuestion.value = 1; + q2.contentQuestion.value = 2; + assert.equal(q3.contentQuestion.value, 3, "q3.contentQuestion.value"); + assert.equal(q3.value, 3, "q3.value"); + assert.deepEqual(survey.data, { q1: 1, q2: 2, q3: 3 }, "Survey data"); + q1.value = 3; + q2.value = 4; + assert.equal(q3.contentQuestion.value, 7, "q3.contentQuestion.value, #2"); + assert.equal(q3.value, 7, "q3.value, #2"); + assert.deepEqual(survey.data, { q1: 3, q2: 4, q3: 7 }, "Survey data, #2"); + q3.contentQuestion.value = 9; + assert.equal(q3.contentQuestion.value, 9, "q3.contentQuestion.value, #3"); + assert.equal(q3.value, 9, "q3.value, #3"); + assert.deepEqual(survey.data, { q1: 3, q2: 4, q3: 9 }, "Survey data, #3"); + q1.value = 5; + q2.value = 7; + assert.equal(q3.contentQuestion.value, 9, "q3.contentQuestion.value, #4"); + assert.equal(q3.value, 9, "q3.value, #4"); + assert.deepEqual(survey.data, { q1: 5, q2: 7, q3: 9 }, "Survey data, #4"); + ComponentCollection.Instance.clear(); +}); QUnit.test("Single: matrixdropdown expressions", function (assert) { var json = { name: "order",