From e16688ee406992e7ca358308056df8b35840ac9e Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 14 Nov 2023 08:05:04 +0200 Subject: [PATCH] Expression question display defaultValue instead of empty string fix #7343 (#7344) --- src/question_expression.ts | 2 +- tests/question_expressiontests.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/question_expression.ts b/src/question_expression.ts index 159f185d1d..68c365f5af 100644 --- a/src/question_expression.ts +++ b/src/question_expression.ts @@ -139,7 +139,7 @@ export class QuestionExpressionModel extends Question { this.updateFormatedValue(); } protected getDisplayValueCore(keysAsText: boolean, value: any): any { - var val = this.isValueEmpty(value) ? this.defaultValue : value; + var val = value === undefined || value === null ? this.defaultValue : value; var res = ""; if (!this.isValueEmpty(val)) { var str = this.getValueAsStr(val); diff --git a/tests/question_expressiontests.ts b/tests/question_expressiontests.ts index 6bb44cf44c..63e9f1f535 100644 --- a/tests/question_expressiontests.ts +++ b/tests/question_expressiontests.ts @@ -296,3 +296,20 @@ QUnit.test("Custom function returns object&array, #7050", function (assert) { FunctionFactory.Instance.unregister("func1"); FunctionFactory.Instance.unregister("func2"); }); +QUnit.test("Default value and setValueExpression", function (assert) { + const survey = new SurveyModel({ + elements: [ + { type: "expression", name: "q1", setValueExpression: "iif({q2} = 1, 'b', ''", defaultValue: "a" }, + { type: "text", name: "q2" } + ] + }); + const q1 = survey.getQuestionByName("q1"); + assert.equal(q1.value, "a", "default value is 'a'"); + assert.equal(q1.formatedValue, "a", "formatedValue, default value is 'a'"); + survey.setValue("q2", 1); + assert.equal(q1.value, "b", "var1 = 1"); + assert.equal(q1.formatedValue, "b", "formatedValue, var1 = 1"); + survey.setValue("q2", 2); + assert.equal(q1.value, "", "var1 = 2"); + assert.equal(q1.formatedValue, "", "formatedValue, var1 = 2"); +});