diff --git a/src/question_boolean.ts b/src/question_boolean.ts index a457123cb6..2cb1e78d7e 100644 --- a/src/question_boolean.ts +++ b/src/question_boolean.ts @@ -63,7 +63,6 @@ export class QuestionBooleanModel extends Question { this.value = val == true ? this.getValueTrue() : this.getValueFalse(); this.booleanValueRendered = val; } - this.updateThumbMargin(); } public get defaultValue(): any { return this.getPropertyValue("defaultValue"); @@ -255,6 +254,16 @@ export class QuestionBooleanModel extends Question { .toString(); } + updateValueFromSurvey(newValue: any, clearData: boolean = false): void { + super.updateValueFromSurvey(newValue, clearData); + this.updateThumbMargin(); + } + + protected onValueChanged(): void { + super.onValueChanged(); + this.updateThumbMargin(); + } + public get svgIcon(): string { if (this.booleanValue && this.cssClasses.svgIconCheckedId) return this.cssClasses.svgIconCheckedId; if (!this.isDeterminated && this.cssClasses.svgIconIndId) return this.cssClasses.svgIconIndId; diff --git a/tests/questionBooleanTests.ts b/tests/questionBooleanTests.ts index 2b4380808c..41e8af78d3 100644 --- a/tests/questionBooleanTests.ts +++ b/tests/questionBooleanTests.ts @@ -1,6 +1,7 @@ import { SurveyModel } from "../src/survey"; import { QuestionBooleanModel } from "../src/question_boolean"; +import { QuestionRadiogroupModel } from "../src/question_radiogroup"; import { defaultV2Css } from "../src/defaultCss/defaultV2Css"; import { QuestionMatrixDynamicModel } from "../src/question_matrixdynamic"; @@ -279,4 +280,43 @@ QUnit.test("Boolean in matrix dynamic", function (assert) { const q = survey.getAllQuestions()[0]; q.visibleRows[0].cells[0].value = "abc"; assert.deepEqual(q.value, [{ col1: "abc" }], "there is not boolean value"); -}); \ No newline at end of file +}); + +QUnit.test("Invoke updateThumbMargin on value changed", function (assert) { + var survey = new SurveyModel({ + elements: [ + { + "type": "boolean", + "name": "question1", + "valueTrue": "yes", + "valueFalse": "no" + }, + { + "type": "radiogroup", + "name": "question1", + "choices": [ + { + "value": "yes", + "text": "Yes" + }, + { + "value": "no", + "text": "No" + } + ] + } + ] + }); + const q = survey.getAllQuestions()[0]; + const radio = survey.getAllQuestions()[1]; + const c = { counter: 0 }; + q.updateThumbMargin = () => c.counter++; + + assert.equal(c.counter, 0, "Initial"); + q.value = "yes"; + assert.equal(c.counter, 1, "Set value to question"); + radio.value = "no"; + assert.equal(c.counter, 2, "Set value to another question"); + survey.data = { question1: "yes" }; + assert.equal(c.counter, 4, "Set value to survey"); +});