Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow to set Infinite as an expression value fix #6981 #6982

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/question_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export class QuestionExpressionModel extends Question {
this.setPropertyValue("precision", val);
}
private roundValue(val: any): any {
if(val === Infinity) return undefined;
if(this.precision < 0) return val;
if(!Helpers.isNumber(val)) return val;
return parseFloat(val.toFixed(this.precision));
Expand Down
33 changes: 33 additions & 0 deletions tests/question_expressiontests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,36 @@ QUnit.test("survey.onValueChanging, bug#6548", function (assert) {
assert.equal(q.value, 35, "correct value on survey.onValueChanging event, #2");
assert.deepEqual(survey.data, { weightKg: 100, heightCm: 169, bmi: 35 }, "survey.data is correct, #2");
});
QUnit.test("Handle Infinity", function (assert) {
const survey = new SurveyModel({
elements: [
{
"name": "q1",
"type": "text"
},
{
"name": "q2",
"type": "text",
"inputType": "number"
},
{
"name": "q3",
"type": "expression",
"expression": "({q1}^-1)*(2^{q2})",
}
]
});
let counter = 0;
survey.onValueChanged.add((sender, options) => {
if(options.name === "q3") counter ++;
});
const q3 = survey.getQuestionByName("q3");
survey.setValue("q2", 1);
assert.equal(counter, 0, "is not updated");
survey.setValue("q2", 3);
assert.equal(counter, 0, "is not updated yet");
assert.equal(q3.isEmpty(), true, "expression question is undefined");
survey.setValue("q1", 2);
assert.equal(counter, 1, "updated one time");
assert.equal(q3.value, 4, "calculated correctly");
});